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(cmd_tbl_t *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(cmd_tbl_t *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(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
63 enum log_category_t cat;
64 enum log_level_t level;
73 cat = log_get_cat_by_name(argv[1]);
74 level = simple_strtoul(argv[2], &end, 10);
76 level = log_get_level_by_name(argv[2]);
78 if (level == LOGL_NONE) {
79 printf("Invalid log level '%s'\n", argv[2]);
83 if (level >= LOGL_MAX) {
84 printf("Invalid log level %u\n", level);
88 line = simple_strtoul(argv[4], NULL, 10);
91 if (_log(cat, level, file, line, func, "%s\n", msg))
92 return CMD_RET_FAILURE;
97 static cmd_tbl_t log_sub[] = {
98 U_BOOT_CMD_MKENT(level, CONFIG_SYS_MAXARGS, 1, do_log_level, "", ""),
99 #ifdef CONFIG_LOG_TEST
100 U_BOOT_CMD_MKENT(test, 2, 1, do_log_test, "", ""),
102 U_BOOT_CMD_MKENT(format, CONFIG_SYS_MAXARGS, 1, do_log_format, "", ""),
103 U_BOOT_CMD_MKENT(rec, CONFIG_SYS_MAXARGS, 1, do_log_rec, "", ""),
106 static int do_log(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
111 return CMD_RET_USAGE;
113 /* drop initial "log" arg */
117 cp = find_cmd_tbl(argv[0], log_sub, ARRAY_SIZE(log_sub));
119 return cp->cmd(cmdtp, flag, argc, argv);
121 return CMD_RET_USAGE;
124 #ifdef CONFIG_SYS_LONGHELP
125 static char log_help_text[] =
126 "level - get/set log level\n"
127 #ifdef CONFIG_LOG_TEST
128 "log test - run log tests\n"
130 "log format <fmt> - set log output format. <fmt> is a string where\n"
131 "\teach letter indicates something that should be displayed:\n"
132 "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
133 "\tor 'default', equivalent to 'fm', or 'all' for all\n"
134 "log rec <category> <level> <file> <line> <func> <message> - "
135 "output a log record"
140 log, CONFIG_SYS_MAXARGS, 1, do_log,
141 "log system", log_help_text