blob: 6afe6ead251b817a9915aac2382c1d0b79d620d1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd5f61f22017-12-04 13:48:26 -07002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassd5f61f22017-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 Glassad0e5032017-12-28 13:14:19 -070012static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
13
Simon Glass09140112020-05-10 11:40:03 -060014static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
15 char *const argv[])
Simon Glassd5f61f22017-12-04 13:48:26 -070016{
Heinrich Schuchardt77007f92020-05-31 13:44:51 +000017 if (argc > 1) {
18 long log_level = simple_strtol(argv[1], NULL, 10);
19
20 if (log_level < 0 || log_level > _LOG_MAX_LEVEL) {
21 printf("Only log levels <= %d are supported\n",
22 _LOG_MAX_LEVEL);
23 return CMD_RET_FAILURE;
24 }
25 gd->default_log_level = log_level;
26 } else {
Simon Glassd5f61f22017-12-04 13:48:26 -070027 printf("Default log level: %d\n", gd->default_log_level);
Heinrich Schuchardt77007f92020-05-31 13:44:51 +000028 }
Simon Glassd5f61f22017-12-04 13:48:26 -070029
30 return 0;
31}
32
Simon Glass09140112020-05-10 11:40:03 -060033static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
34 char *const argv[])
Simon Glassad0e5032017-12-28 13:14:19 -070035{
36 int i;
37
38 if (argc > 1) {
39 const char *str = argv[1];
40
41 if (!strcmp(str, "default")) {
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +020042 gd->log_fmt = log_get_default_format();
Simon Glassad0e5032017-12-28 13:14:19 -070043 } else if (!strcmp(str, "all")) {
44 gd->log_fmt = LOGF_ALL;
45 } else {
46 gd->log_fmt = 0;
47 for (; *str; str++) {
48 char *ptr = strchr(log_fmt_chars, *str);
49
50 if (!ptr) {
51 printf("Invalid log char '%c'\n", *str);
52 return CMD_RET_FAILURE;
53 }
54 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
55 }
56 }
57 } else {
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]);
62 }
63 printf("\n");
64 }
65
66 return 0;
67}
68
Simon Glass09140112020-05-10 11:40:03 -060069static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
70 char *const argv[])
Simon Glass3fd24fa2017-12-28 13:14:20 -070071{
72 enum log_category_t cat;
73 enum log_level_t level;
74 const char *file;
75 uint line;
76 const char *func;
77 const char *msg;
78 char *end;
79
80 if (argc < 7)
81 return CMD_RET_USAGE;
82 cat = log_get_cat_by_name(argv[1]);
83 level = simple_strtoul(argv[2], &end, 10);
84 if (end == argv[2]) {
85 level = log_get_level_by_name(argv[2]);
86
87 if (level == LOGL_NONE) {
88 printf("Invalid log level '%s'\n", argv[2]);
89 return CMD_RET_USAGE;
90 }
91 }
92 if (level >= LOGL_MAX) {
93 printf("Invalid log level %u\n", level);
94 return CMD_RET_USAGE;
95 }
96 file = argv[3];
97 line = simple_strtoul(argv[4], NULL, 10);
98 func = argv[5];
99 msg = argv[6];
100 if (_log(cat, level, file, line, func, "%s\n", msg))
101 return CMD_RET_FAILURE;
102
103 return 0;
104}
105
Simon Glass09140112020-05-10 11:40:03 -0600106static struct cmd_tbl log_sub[] = {
Simon Glassd5f61f22017-12-04 13:48:26 -0700107 U_BOOT_CMD_MKENT(level, CONFIG_SYS_MAXARGS, 1, do_log_level, "", ""),
Simon Glassef11ed82017-12-04 13:48:27 -0700108#ifdef CONFIG_LOG_TEST
109 U_BOOT_CMD_MKENT(test, 2, 1, do_log_test, "", ""),
110#endif
Simon Glassad0e5032017-12-28 13:14:19 -0700111 U_BOOT_CMD_MKENT(format, CONFIG_SYS_MAXARGS, 1, do_log_format, "", ""),
Simon Glass3fd24fa2017-12-28 13:14:20 -0700112 U_BOOT_CMD_MKENT(rec, CONFIG_SYS_MAXARGS, 1, do_log_rec, "", ""),
Simon Glassd5f61f22017-12-04 13:48:26 -0700113};
114
Simon Glass09140112020-05-10 11:40:03 -0600115static int do_log(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glassd5f61f22017-12-04 13:48:26 -0700116{
Simon Glass09140112020-05-10 11:40:03 -0600117 struct cmd_tbl *cp;
Simon Glassd5f61f22017-12-04 13:48:26 -0700118
119 if (argc < 2)
120 return CMD_RET_USAGE;
121
122 /* drop initial "log" arg */
123 argc--;
124 argv++;
125
126 cp = find_cmd_tbl(argv[0], log_sub, ARRAY_SIZE(log_sub));
127 if (cp)
128 return cp->cmd(cmdtp, flag, argc, argv);
129
130 return CMD_RET_USAGE;
131}
132
133#ifdef CONFIG_SYS_LONGHELP
134static char log_help_text[] =
135 "level - get/set log level\n"
Simon Glassef11ed82017-12-04 13:48:27 -0700136#ifdef CONFIG_LOG_TEST
137 "log test - run log tests\n"
138#endif
Simon Glassad0e5032017-12-28 13:14:19 -0700139 "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"
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200142 "\tor 'default', or 'all' for all\n"
Simon Glass3fd24fa2017-12-28 13:14:20 -0700143 "log rec <category> <level> <file> <line> <func> <message> - "
144 "output a log record"
Simon Glassd5f61f22017-12-04 13:48:26 -0700145 ;
146#endif
147
148U_BOOT_CMD(
149 log, CONFIG_SYS_MAXARGS, 1, do_log,
150 "log system", log_help_text
151);