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 long log_level = simple_strtol(argv[1], NULL, 10);
20 if (log_level < 0 || log_level > _LOG_MAX_LEVEL) {
21 printf("Only log levels <= %d are supported\n",
23 return CMD_RET_FAILURE;
25 gd->default_log_level = log_level;
27 printf("Default log level: %d\n", gd->default_log_level);
33 static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
39 const char *str = argv[1];
41 if (!strcmp(str, "default")) {
42 gd->log_fmt = LOGF_DEFAULT;
43 } else if (!strcmp(str, "all")) {
44 gd->log_fmt = LOGF_ALL;
48 char *ptr = strchr(log_fmt_chars, *str);
51 printf("Invalid log char '%c'\n", *str);
52 return CMD_RET_FAILURE;
54 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
58 printf("Log format: ");
59 for (i = 0; i < LOGF_COUNT; i++) {
60 if (gd->log_fmt & (1 << i))
61 printf("%c", log_fmt_chars[i]);
69 static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
72 enum log_category_t cat;
73 enum log_level_t level;
82 cat = log_get_cat_by_name(argv[1]);
83 level = simple_strtoul(argv[2], &end, 10);
85 level = log_get_level_by_name(argv[2]);
87 if (level == LOGL_NONE) {
88 printf("Invalid log level '%s'\n", argv[2]);
92 if (level >= LOGL_MAX) {
93 printf("Invalid log level %u\n", level);
97 line = simple_strtoul(argv[4], NULL, 10);
100 if (_log(cat, level, file, line, func, "%s\n", msg))
101 return CMD_RET_FAILURE;
106 static struct cmd_tbl log_sub[] = {
107 U_BOOT_CMD_MKENT(level, CONFIG_SYS_MAXARGS, 1, do_log_level, "", ""),
108 #ifdef CONFIG_LOG_TEST
109 U_BOOT_CMD_MKENT(test, 2, 1, do_log_test, "", ""),
111 U_BOOT_CMD_MKENT(format, CONFIG_SYS_MAXARGS, 1, do_log_format, "", ""),
112 U_BOOT_CMD_MKENT(rec, CONFIG_SYS_MAXARGS, 1, do_log_rec, "", ""),
115 static int do_log(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
120 return CMD_RET_USAGE;
122 /* drop initial "log" arg */
126 cp = find_cmd_tbl(argv[0], log_sub, ARRAY_SIZE(log_sub));
128 return cp->cmd(cmdtp, flag, argc, argv);
130 return CMD_RET_USAGE;
133 #ifdef CONFIG_SYS_LONGHELP
134 static char log_help_text[] =
135 "level - get/set log level\n"
136 #ifdef CONFIG_LOG_TEST
137 "log test - run log tests\n"
139 "log format <fmt> - set log output format. <fmt> is a string where\n"
140 "\teach letter indicates something that should be displayed:\n"
141 "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
142 "\tor 'default', equivalent to 'fm', or 'all' for all\n"
143 "log rec <category> <level> <file> <line> <func> <message> - "
144 "output a log record"
149 log, CONFIG_SYS_MAXARGS, 1, do_log,
150 "log system", log_help_text