1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
13 #include <asm/global_data.h>
15 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
17 static enum log_level_t parse_log_level(char *const arg)
22 if (!strict_strtoul(arg, 10, &level)) {
23 if (level > _LOG_MAX_LEVEL) {
24 printf("Only log levels <= %d are supported\n",
31 ret = log_get_level_by_name(arg);
33 printf("Unknown log level \"%s\"\n", arg);
37 static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
40 enum log_level_t log_level;
43 log_level = parse_log_level(argv[1]);
45 if (log_level == LOGL_NONE)
46 return CMD_RET_FAILURE;
47 gd->default_log_level = log_level;
49 for (log_level = LOGL_FIRST; log_level <= _LOG_MAX_LEVEL;
51 printf("%s%s\n", log_get_level_name(log_level),
52 log_level == gd->default_log_level ?
56 return CMD_RET_SUCCESS;
59 static int do_log_categories(struct cmd_tbl *cmdtp, int flag, int argc,
62 enum log_category_t cat;
65 for (cat = LOGC_FIRST; cat < LOGC_COUNT; cat++) {
66 name = log_get_cat_name(cat);
68 * Invalid category names (e.g. <invalid> or <missing>) begin
76 return CMD_RET_SUCCESS;
79 static int do_log_drivers(struct cmd_tbl *cmdtp, int flag, int argc,
82 struct log_device *ldev;
84 list_for_each_entry(ldev, &gd->log_head, sibling_node)
85 printf("%s\n", ldev->drv->name);
87 return CMD_RET_SUCCESS;
90 static int do_log_filter_list(struct cmd_tbl *cmdtp, int flag, int argc,
94 const char *drv_name = "console";
95 struct getopt_state gs;
96 struct log_filter *filt;
97 struct log_device *ldev;
99 getopt_init_state(&gs);
100 while ((opt = getopt(&gs, argc, argv, "d:")) > 0) {
106 return CMD_RET_USAGE;
110 if (gs.index != argc)
111 return CMD_RET_USAGE;
113 ldev = log_device_find_by_name(drv_name);
115 printf("Could not find log device for \"%s\"\n", drv_name);
116 return CMD_RET_FAILURE;
119 /* <3> < 6 > <2+1 + 7 > < 16 > < unbounded... */
120 printf("num policy level categories files\n");
121 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
122 printf("%3d %6.6s %s %-7.7s ", filt->filter_num,
123 filt->flags & LOGFF_DENY ? "deny" : "allow",
124 filt->flags & LOGFF_LEVEL_MIN ? ">=" : "<=",
125 log_get_level_name(filt->level));
127 if (filt->flags & LOGFF_HAS_CAT) {
130 if (filt->cat_list[0] != LOGC_END)
131 printf("%16.16s %s\n",
132 log_get_cat_name(filt->cat_list[0]),
133 filt->file_list ? filt->file_list : "");
135 for (i = 1; i < LOGF_MAX_CATEGORIES &&
136 filt->cat_list[i] != LOGC_END; i++)
137 printf("%21c %16.16s\n", ' ',
138 log_get_cat_name(filt->cat_list[i]));
140 printf("%16c %s\n", ' ',
141 filt->file_list ? filt->file_list : "");
145 return CMD_RET_SUCCESS;
148 static int do_log_filter_add(struct cmd_tbl *cmdtp, int flag, int argc,
151 bool level_set = false;
152 bool print_num = false;
153 bool type_set = false;
154 char *file_list = NULL;
155 const char *drv_name = "console";
159 enum log_category_t cat_list[LOGF_MAX_CATEGORIES + 1];
160 enum log_level_t level = LOGL_MAX;
161 struct getopt_state gs;
163 getopt_init_state(&gs);
164 while ((opt = getopt(&gs, argc, argv, "Ac:d:Df:l:L:p")) > 0) {
167 #define do_type() do { \
169 printf("Allow or deny set twice\n"); \
170 return CMD_RET_USAGE; \
177 enum log_category_t cat;
179 if (cat_count >= LOGF_MAX_CATEGORIES) {
180 printf("Too many categories\n");
181 return CMD_RET_FAILURE;
184 cat = log_get_cat_by_name(gs.arg);
185 if (cat == LOGC_NONE) {
186 printf("Unknown category \"%s\"\n", gs.arg);
187 return CMD_RET_FAILURE;
190 cat_list[cat_count++] = cat;
204 #define do_level() do { \
206 printf("Log level set twice\n"); \
207 return CMD_RET_USAGE; \
209 level = parse_log_level(gs.arg); \
210 if (level == LOGL_NONE) \
211 return CMD_RET_FAILURE; \
218 flags |= LOGFF_LEVEL_MIN;
224 return CMD_RET_USAGE;
228 if (gs.index != argc)
229 return CMD_RET_USAGE;
231 cat_list[cat_count] = LOGC_END;
232 err = log_add_filter_flags(drv_name, cat_count ? cat_list : NULL, level,
235 printf("Could not add filter (err = %d)\n", err);
236 return CMD_RET_FAILURE;
237 } else if (print_num) {
241 return CMD_RET_SUCCESS;
244 static int do_log_filter_remove(struct cmd_tbl *cmdtp, int flag, int argc,
250 const char *drv_name = "console";
251 struct getopt_state gs;
253 getopt_init_state(&gs);
254 while ((opt = getopt(&gs, argc, argv, "ad:")) > 0) {
263 return CMD_RET_USAGE;
268 struct log_filter *filt, *tmp_filt;
269 struct log_device *ldev;
271 if (gs.index != argc)
272 return CMD_RET_USAGE;
274 ldev = log_device_find_by_name(drv_name);
276 printf("Could not find log device for \"%s\"\n",
278 return CMD_RET_FAILURE;
281 list_for_each_entry_safe(filt, tmp_filt, &ldev->filter_head,
283 list_del(&filt->sibling_node);
287 if (gs.index + 1 != argc)
288 return CMD_RET_USAGE;
290 if (strict_strtoul(argv[gs.index], 10, &filter_num)) {
291 printf("Invalid filter number \"%s\"\n", argv[gs.index]);
292 return CMD_RET_FAILURE;
295 err = log_remove_filter(drv_name, filter_num);
297 printf("Could not remove filter (err = %d)\n", err);
298 return CMD_RET_FAILURE;
302 return CMD_RET_SUCCESS;
305 static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
311 const char *str = argv[1];
313 if (!strcmp(str, "default")) {
314 gd->log_fmt = log_get_default_format();
315 } else if (!strcmp(str, "all")) {
316 gd->log_fmt = LOGF_ALL;
319 for (; *str; str++) {
320 char *ptr = strchr(log_fmt_chars, *str);
323 printf("Invalid log char '%c'\n", *str);
324 return CMD_RET_FAILURE;
326 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
330 printf("Log format: ");
331 for (i = 0; i < LOGF_COUNT; i++) {
332 if (gd->log_fmt & (1 << i))
333 printf("%c", log_fmt_chars[i]);
341 static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
344 enum log_category_t cat;
345 enum log_level_t level;
353 return CMD_RET_USAGE;
354 cat = log_get_cat_by_name(argv[1]);
355 level = dectoul(argv[2], &end);
356 if (end == argv[2]) {
357 level = log_get_level_by_name(argv[2]);
359 if (level == LOGL_NONE) {
360 printf("Invalid log level '%s'\n", argv[2]);
361 return CMD_RET_USAGE;
364 if (level >= LOGL_MAX) {
365 printf("Invalid log level %u\n", level);
366 return CMD_RET_USAGE;
369 line = dectoul(argv[4], NULL);
372 if (_log(cat, level, file, line, func, "%s\n", msg))
373 return CMD_RET_FAILURE;
378 #ifdef CONFIG_SYS_LONGHELP
379 static char log_help_text[] =
380 "level [<level>] - get/set log level\n"
381 "categories - list log categories\n"
382 "drivers - list log drivers\n"
383 "log filter-list [OPTIONS] - list all filters for a log driver\n"
384 "\t-d <driver> - Specify the log driver to list filters from; defaults\n"
386 "log filter-add [OPTIONS] - add a new filter to a driver\n"
387 "\t-A - Allow messages matching this filter; mutually exclusive with -D\n"
388 "\t This is the default.\n"
389 "\t-c <category> - Category to match; may be specified multiple times\n"
390 "\t-d <driver> - Specify the log driver to add the filter to; defaults\n"
392 "\t-D - Deny messages matching this filter; mutually exclusive with -A\n"
393 "\t-f <files_list> - A comma-separated list of files to match\n"
394 "\t-l <level> - Match log levels less than or equal to <level>;\n"
395 "\t mutually-exclusive with -L\n"
396 "\t-L <level> - Match log levels greather than or equal to <level>;\n"
397 "\t mutually-exclusive with -l\n"
398 "\t-p - Print the filter number on success\n"
399 "log filter-remove [OPTIONS] [<num>] - Remove filter number <num>\n"
400 "\t-a - Remove ALL filters\n"
401 "\t-d <driver> - Specify the log driver to remove the filter from;\n"
402 "\t defaults to console\n"
403 "log format <fmt> - set log output format. <fmt> is a string where\n"
404 "\teach letter indicates something that should be displayed:\n"
405 "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
406 "\tor 'default', or 'all' for all\n"
407 "log rec <category> <level> <file> <line> <func> <message> - "
408 "output a log record"
412 U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text,
413 U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level),
414 U_BOOT_SUBCMD_MKENT(categories, 1, 1, do_log_categories),
415 U_BOOT_SUBCMD_MKENT(drivers, 1, 1, do_log_drivers),
416 U_BOOT_SUBCMD_MKENT(filter-list, 3, 1, do_log_filter_list),
417 U_BOOT_SUBCMD_MKENT(filter-add, CONFIG_SYS_MAXARGS, 1,
419 U_BOOT_SUBCMD_MKENT(filter-remove, 4, 1, do_log_filter_remove),
420 U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format),
421 U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec),