blob: 651e50358c001eb030db897c8da14bf5fd3f14a5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass5684f8f2017-12-04 13:48:26 -07002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass5684f8f2017-12-04 13:48:26 -07005 */
6
7#include <common.h>
8#include <command.h>
9#include <dm.h>
10#include <log.h>
11
Simon Glassea0049b2017-12-28 13:14:19 -070012static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
13
Sean Anderson0a3b6942020-10-27 19:55:33 -040014static enum log_level_t parse_log_level(char *const arg)
15{
16 enum log_level_t ret;
17 ulong level;
18
19 if (!strict_strtoul(arg, 10, &level)) {
20 if (level > _LOG_MAX_LEVEL) {
21 printf("Only log levels <= %d are supported\n",
22 _LOG_MAX_LEVEL);
23 return LOGL_NONE;
24 }
25 return level;
26 }
27
28 ret = log_get_level_by_name(arg);
29 if (ret == LOGL_NONE)
30 printf("Unknown log level \"%s\"\n", arg);
31 return ret;
32}
33
Simon Glassed38aef2020-05-10 11:40:03 -060034static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
35 char *const argv[])
Simon Glass5684f8f2017-12-04 13:48:26 -070036{
Heinrich Schuchardt276bf922020-05-31 13:44:51 +000037 if (argc > 1) {
Sean Anderson0a3b6942020-10-27 19:55:33 -040038 enum log_level_t log_level = parse_log_level(argv[1]);
Heinrich Schuchardt276bf922020-05-31 13:44:51 +000039
Sean Anderson0a3b6942020-10-27 19:55:33 -040040 if (log_level == LOGL_NONE)
Heinrich Schuchardt276bf922020-05-31 13:44:51 +000041 return CMD_RET_FAILURE;
Heinrich Schuchardt276bf922020-05-31 13:44:51 +000042 gd->default_log_level = log_level;
43 } else {
Simon Glass5684f8f2017-12-04 13:48:26 -070044 printf("Default log level: %d\n", gd->default_log_level);
Heinrich Schuchardt276bf922020-05-31 13:44:51 +000045 }
Simon Glass5684f8f2017-12-04 13:48:26 -070046
Sean Anderson0a3b6942020-10-27 19:55:33 -040047 return CMD_RET_SUCCESS;
Simon Glass5684f8f2017-12-04 13:48:26 -070048}
49
Simon Glassed38aef2020-05-10 11:40:03 -060050static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
51 char *const argv[])
Simon Glassea0049b2017-12-28 13:14:19 -070052{
53 int i;
54
55 if (argc > 1) {
56 const char *str = argv[1];
57
58 if (!strcmp(str, "default")) {
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +020059 gd->log_fmt = log_get_default_format();
Simon Glassea0049b2017-12-28 13:14:19 -070060 } else if (!strcmp(str, "all")) {
61 gd->log_fmt = LOGF_ALL;
62 } else {
63 gd->log_fmt = 0;
64 for (; *str; str++) {
65 char *ptr = strchr(log_fmt_chars, *str);
66
67 if (!ptr) {
68 printf("Invalid log char '%c'\n", *str);
69 return CMD_RET_FAILURE;
70 }
71 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
72 }
73 }
74 } else {
75 printf("Log format: ");
76 for (i = 0; i < LOGF_COUNT; i++) {
77 if (gd->log_fmt & (1 << i))
78 printf("%c", log_fmt_chars[i]);
79 }
80 printf("\n");
81 }
82
83 return 0;
84}
85
Simon Glassed38aef2020-05-10 11:40:03 -060086static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
87 char *const argv[])
Simon Glassdf12ab52017-12-28 13:14:20 -070088{
89 enum log_category_t cat;
90 enum log_level_t level;
91 const char *file;
92 uint line;
93 const char *func;
94 const char *msg;
95 char *end;
96
97 if (argc < 7)
98 return CMD_RET_USAGE;
99 cat = log_get_cat_by_name(argv[1]);
100 level = simple_strtoul(argv[2], &end, 10);
101 if (end == argv[2]) {
102 level = log_get_level_by_name(argv[2]);
103
104 if (level == LOGL_NONE) {
105 printf("Invalid log level '%s'\n", argv[2]);
106 return CMD_RET_USAGE;
107 }
108 }
109 if (level >= LOGL_MAX) {
110 printf("Invalid log level %u\n", level);
111 return CMD_RET_USAGE;
112 }
113 file = argv[3];
114 line = simple_strtoul(argv[4], NULL, 10);
115 func = argv[5];
116 msg = argv[6];
117 if (_log(cat, level, file, line, func, "%s\n", msg))
118 return CMD_RET_FAILURE;
119
120 return 0;
121}
122
Simon Glass5684f8f2017-12-04 13:48:26 -0700123#ifdef CONFIG_SYS_LONGHELP
124static char log_help_text[] =
125 "level - get/set log level\n"
Simon Glassea0049b2017-12-28 13:14:19 -0700126 "log format <fmt> - set log output format. <fmt> is a string where\n"
127 "\teach letter indicates something that should be displayed:\n"
128 "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200129 "\tor 'default', or 'all' for all\n"
Simon Glassdf12ab52017-12-28 13:14:20 -0700130 "log rec <category> <level> <file> <line> <func> <message> - "
131 "output a log record"
Simon Glass5684f8f2017-12-04 13:48:26 -0700132 ;
133#endif
134
Sean Andersondcabb4b2020-10-27 19:55:32 -0400135U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text,
136 U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level),
137 U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format),
138 U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec),
Simon Glass5684f8f2017-12-04 13:48:26 -0700139);