2 * Copyright (c) 2017 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
13 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
15 static int do_log_level(cmd_tbl_t *cmdtp, int flag, int argc,
19 gd->default_log_level = simple_strtol(argv[1], NULL, 10);
21 printf("Default log level: %d\n", gd->default_log_level);
26 static int do_log_format(cmd_tbl_t *cmdtp, int flag, int argc,
32 const char *str = argv[1];
34 if (!strcmp(str, "default")) {
35 gd->log_fmt = LOGF_DEFAULT;
36 } else if (!strcmp(str, "all")) {
37 gd->log_fmt = LOGF_ALL;
41 char *ptr = strchr(log_fmt_chars, *str);
44 printf("Invalid log char '%c'\n", *str);
45 return CMD_RET_FAILURE;
47 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
51 printf("Log format: ");
52 for (i = 0; i < LOGF_COUNT; i++) {
53 if (gd->log_fmt & (1 << i))
54 printf("%c", log_fmt_chars[i]);
62 static int do_log_rec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
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 cmd_tbl_t 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(cmd_tbl_t *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