1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
12 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
14 static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
18 gd->default_log_level = simple_strtol(argv[1], NULL, 10);
20 printf("Default log level: %d\n", gd->default_log_level);
25 static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
31 const char *str = argv[1];
33 if (!strcmp(str, "default")) {
34 gd->log_fmt = LOGF_DEFAULT;
35 } else if (!strcmp(str, "all")) {
36 gd->log_fmt = LOGF_ALL;
40 char *ptr = strchr(log_fmt_chars, *str);
43 printf("Invalid log char '%c'\n", *str);
44 return CMD_RET_FAILURE;
46 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
50 printf("Log format: ");
51 for (i = 0; i < LOGF_COUNT; i++) {
52 if (gd->log_fmt & (1 << i))
53 printf("%c", log_fmt_chars[i]);
61 static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
64 enum log_category_t cat;
65 enum log_level_t level;
74 cat = log_get_cat_by_name(argv[1]);
75 level = simple_strtoul(argv[2], &end, 10);
77 level = log_get_level_by_name(argv[2]);
79 if (level == LOGL_NONE) {
80 printf("Invalid log level '%s'\n", argv[2]);
84 if (level >= LOGL_MAX) {
85 printf("Invalid log level %u\n", level);
89 line = simple_strtoul(argv[4], NULL, 10);
92 if (_log(cat, level, file, line, func, "%s\n", msg))
93 return CMD_RET_FAILURE;
98 static struct cmd_tbl log_sub[] = {
99 U_BOOT_CMD_MKENT(level, CONFIG_SYS_MAXARGS, 1, do_log_level, "", ""),
100 #ifdef CONFIG_LOG_TEST
101 U_BOOT_CMD_MKENT(test, 2, 1, do_log_test, "", ""),
103 U_BOOT_CMD_MKENT(format, CONFIG_SYS_MAXARGS, 1, do_log_format, "", ""),
104 U_BOOT_CMD_MKENT(rec, CONFIG_SYS_MAXARGS, 1, do_log_rec, "", ""),
107 static int do_log(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
112 return CMD_RET_USAGE;
114 /* drop initial "log" arg */
118 cp = find_cmd_tbl(argv[0], log_sub, ARRAY_SIZE(log_sub));
120 return cp->cmd(cmdtp, flag, argc, argv);
122 return CMD_RET_USAGE;
125 #ifdef CONFIG_SYS_LONGHELP
126 static char log_help_text[] =
127 "level - get/set log level\n"
128 #ifdef CONFIG_LOG_TEST
129 "log test - run log tests\n"
131 "log format <fmt> - set log output format. <fmt> is a string where\n"
132 "\teach letter indicates something that should be displayed:\n"
133 "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
134 "\tor 'default', equivalent to 'fm', or 'all' for all\n"
135 "log rec <category> <level> <file> <line> <func> <message> - "
136 "output a log record"
141 log, CONFIG_SYS_MAXARGS, 1, do_log,
142 "log system", log_help_text