1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Google, Inc
12 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
14 static enum log_level_t parse_log_level(char *const arg)
19 if (!strict_strtoul(arg, 10, &level)) {
20 if (level > _LOG_MAX_LEVEL) {
21 printf("Only log levels <= %d are supported\n",
28 ret = log_get_level_by_name(arg);
30 printf("Unknown log level \"%s\"\n", arg);
34 static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
38 enum log_level_t log_level = parse_log_level(argv[1]);
40 if (log_level == LOGL_NONE)
41 return CMD_RET_FAILURE;
42 gd->default_log_level = log_level;
44 printf("Default log level: %d\n", gd->default_log_level);
47 return CMD_RET_SUCCESS;
50 static int do_log_categories(struct cmd_tbl *cmdtp, int flag, int argc,
53 enum log_category_t cat;
56 for (cat = LOGC_FIRST; cat < LOGC_COUNT; cat++) {
57 name = log_get_cat_name(cat);
59 * Invalid category names (e.g. <invalid> or <missing>) begin
67 return CMD_RET_SUCCESS;
70 static int do_log_drivers(struct cmd_tbl *cmdtp, int flag, int argc,
73 struct log_device *ldev;
75 list_for_each_entry(ldev, &gd->log_head, sibling_node)
76 printf("%s\n", ldev->drv->name);
78 return CMD_RET_SUCCESS;
81 static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
87 const char *str = argv[1];
89 if (!strcmp(str, "default")) {
90 gd->log_fmt = log_get_default_format();
91 } else if (!strcmp(str, "all")) {
92 gd->log_fmt = LOGF_ALL;
96 char *ptr = strchr(log_fmt_chars, *str);
99 printf("Invalid log char '%c'\n", *str);
100 return CMD_RET_FAILURE;
102 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
106 printf("Log format: ");
107 for (i = 0; i < LOGF_COUNT; i++) {
108 if (gd->log_fmt & (1 << i))
109 printf("%c", log_fmt_chars[i]);
117 static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
120 enum log_category_t cat;
121 enum log_level_t level;
129 return CMD_RET_USAGE;
130 cat = log_get_cat_by_name(argv[1]);
131 level = simple_strtoul(argv[2], &end, 10);
132 if (end == argv[2]) {
133 level = log_get_level_by_name(argv[2]);
135 if (level == LOGL_NONE) {
136 printf("Invalid log level '%s'\n", argv[2]);
137 return CMD_RET_USAGE;
140 if (level >= LOGL_MAX) {
141 printf("Invalid log level %u\n", level);
142 return CMD_RET_USAGE;
145 line = simple_strtoul(argv[4], NULL, 10);
148 if (_log(cat, level, file, line, func, "%s\n", msg))
149 return CMD_RET_FAILURE;
154 #ifdef CONFIG_SYS_LONGHELP
155 static char log_help_text[] =
156 "level - get/set log level\n"
157 "categories - list log categories\n"
158 "drivers - list log drivers\n"
159 "log format <fmt> - set log output format. <fmt> is a string where\n"
160 "\teach letter indicates something that should be displayed:\n"
161 "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
162 "\tor 'default', or 'all' for all\n"
163 "log rec <category> <level> <file> <line> <func> <message> - "
164 "output a log record"
168 U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text,
169 U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level),
170 U_BOOT_SUBCMD_MKENT(categories, 1, 1, do_log_categories),
171 U_BOOT_SUBCMD_MKENT(drivers, 1, 1, do_log_drivers),
172 U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format),
173 U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec),