/src/barebox/commands/dmesg.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | // SPDX-FileCopyrightText: © 2014 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix |
3 | | |
4 | | /* dmesg.c - barebox logbuffer handling */ |
5 | | |
6 | | #include <common.h> |
7 | | #include <malloc.h> |
8 | | #include <command.h> |
9 | | #include <globalvar.h> |
10 | | #include <environment.h> |
11 | | #include <getopt.h> |
12 | | #include <clock.h> |
13 | | |
14 | | static int str_to_loglevel(const char *str) |
15 | 0 | { |
16 | 0 | int ret; |
17 | 0 | unsigned long level; |
18 | |
|
19 | 0 | ret = kstrtoul(str, 10, &level); |
20 | 0 | if (!ret) { |
21 | 0 | if (level > MSG_VDEBUG) |
22 | 0 | goto unknown; |
23 | 0 | return level; |
24 | 0 | } |
25 | | |
26 | 0 | if (!strcmp(str, "vdebug")) |
27 | 0 | return MSG_VDEBUG; |
28 | 0 | if (!strcmp(str, "debug")) |
29 | 0 | return MSG_DEBUG; |
30 | 0 | if (!strcmp(str, "info")) |
31 | 0 | return MSG_INFO; |
32 | 0 | if (!strcmp(str, "notice")) |
33 | 0 | return MSG_NOTICE; |
34 | 0 | if (!strcmp(str, "warn")) |
35 | 0 | return MSG_WARNING; |
36 | 0 | if (!strcmp(str, "err")) |
37 | 0 | return MSG_ERR; |
38 | 0 | if (!strcmp(str, "crit")) |
39 | 0 | return MSG_CRIT; |
40 | 0 | if (!strcmp(str, "alert")) |
41 | 0 | return MSG_ALERT; |
42 | 0 | if (!strcmp(str, "emerg")) |
43 | 0 | return MSG_EMERG; |
44 | 0 | unknown: |
45 | 0 | printf("dmesg: unknown loglevel %s\n", str); |
46 | |
|
47 | 0 | return -EINVAL; |
48 | 0 | } |
49 | | |
50 | | static unsigned dmesg_get_levels(const char *__args) |
51 | 0 | { |
52 | 0 | char *args = xstrdup(__args); |
53 | 0 | char *str, *levels = args; |
54 | 0 | unsigned flags = 0; |
55 | |
|
56 | 0 | while (1) { |
57 | 0 | int level; |
58 | |
|
59 | 0 | str = strsep(&levels, ","); |
60 | 0 | if (!str) |
61 | 0 | break; |
62 | | |
63 | 0 | level = str_to_loglevel(str); |
64 | 0 | if (level < 0) { |
65 | 0 | flags = 0; |
66 | 0 | break; |
67 | 0 | } |
68 | | |
69 | 0 | flags |= BIT(level); |
70 | 0 | } |
71 | |
|
72 | 0 | free(args); |
73 | |
|
74 | 0 | return flags; |
75 | 0 | } |
76 | | |
77 | | static int do_dmesg(int argc, char *argv[]) |
78 | 0 | { |
79 | 0 | int emit = 0; |
80 | 0 | int opt, ret, i; |
81 | 0 | unsigned int keep_lines = UINT_MAX; |
82 | 0 | unsigned int flags = 0, levels = 0; |
83 | 0 | char *set = NULL; |
84 | |
|
85 | 0 | while ((opt = getopt(argc, argv, "c::tderl:p:n:")) > 0) { |
86 | 0 | switch (opt) { |
87 | 0 | case 'c': |
88 | 0 | keep_lines = 0; |
89 | 0 | if (optarg && kstrtouint(optarg, 10, &keep_lines)) |
90 | 0 | return COMMAND_ERROR_USAGE; |
91 | 0 | break; |
92 | 0 | case 't': |
93 | 0 | flags |= BAREBOX_LOG_PRINT_TIME; |
94 | 0 | break; |
95 | 0 | case 'd': |
96 | 0 | flags |= BAREBOX_LOG_DIFF_TIME; |
97 | 0 | break; |
98 | 0 | case 'e': |
99 | 0 | emit = 1; |
100 | 0 | break; |
101 | 0 | case 'l': |
102 | 0 | case 'p': |
103 | 0 | levels = dmesg_get_levels(optarg); |
104 | 0 | if (!levels) |
105 | 0 | return COMMAND_ERROR; |
106 | 0 | if (opt == 'p') { |
107 | 0 | unsigned maxbit = fls(levels) - 1; |
108 | 0 | if (levels & ~BIT(maxbit)) |
109 | 0 | return COMMAND_ERROR_USAGE; |
110 | 0 | levels = GENMASK(maxbit, 0); |
111 | 0 | } |
112 | 0 | break; |
113 | 0 | case 'r': |
114 | 0 | flags |= BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME; |
115 | 0 | break; |
116 | 0 | case 'n': |
117 | 0 | set = optarg; |
118 | 0 | break; |
119 | 0 | default: |
120 | 0 | return COMMAND_ERROR_USAGE; |
121 | 0 | } |
122 | 0 | } |
123 | | |
124 | 0 | if (set) { |
125 | 0 | int level = str_to_loglevel(set); |
126 | |
|
127 | 0 | if (level < 0) |
128 | 0 | return COMMAND_ERROR; |
129 | | |
130 | 0 | barebox_loglevel = level; |
131 | |
|
132 | 0 | return 0; |
133 | 0 | } |
134 | | |
135 | 0 | if (emit) { |
136 | 0 | char *buf; |
137 | 0 | int len = 0; |
138 | |
|
139 | 0 | for (i = optind; i < argc; i++) |
140 | 0 | len += strlen(argv[i]) + 1; |
141 | |
|
142 | 0 | buf = malloc(len + 2); |
143 | 0 | if (!buf) |
144 | 0 | return -ENOMEM; |
145 | | |
146 | 0 | len = 0; |
147 | |
|
148 | 0 | for (i = optind; i < argc; i++) |
149 | 0 | len += sprintf(buf + len, "%s ", argv[i]); |
150 | |
|
151 | 0 | *(buf + len) = '\n'; |
152 | 0 | *(buf + len + 1) = 0; |
153 | |
|
154 | 0 | pr_info("%s", buf); |
155 | |
|
156 | 0 | free(buf); |
157 | |
|
158 | 0 | return 0; |
159 | 0 | } |
160 | | |
161 | 0 | ret = log_print(CONSOLE_DEV_STDOUT, flags, levels); |
162 | 0 | if (ret) |
163 | 0 | return 1; |
164 | | |
165 | 0 | if (keep_lines < UINT_MAX) |
166 | 0 | log_clean(keep_lines); |
167 | |
|
168 | 0 | return 0; |
169 | 0 | } |
170 | | |
171 | | |
172 | | BAREBOX_CMD_HELP_START(dmesg) |
173 | | BAREBOX_CMD_HELP_TEXT("print or control the barebox message buffer") |
174 | | BAREBOX_CMD_HELP_TEXT("Loglevels can be specified as number (0=emerg, 8=vdebug)") |
175 | | BAREBOX_CMD_HELP_TEXT("Known debug loglevels are: emerg, alert, crit, err, warn, notice, info, debug,") |
176 | | BAREBOX_CMD_HELP_TEXT("vdebug") |
177 | | BAREBOX_CMD_HELP_TEXT("") |
178 | | BAREBOX_CMD_HELP_TEXT("Options:") |
179 | | BAREBOX_CMD_HELP_OPT ("-c [<num>]", "Delete messages after printing them, optionally keep <num> lines") |
180 | | BAREBOX_CMD_HELP_OPT ("-d", "Show a time delta to the last message") |
181 | | BAREBOX_CMD_HELP_OPT ("-e <msg>", "Emit a log message") |
182 | | BAREBOX_CMD_HELP_OPT ("-l <loglevel>", "Restrict output to the given (comma-separated) list of levels") |
183 | | BAREBOX_CMD_HELP_OPT ("-p <loglevel>", "Restrict output to specified log level at most") |
184 | | BAREBOX_CMD_HELP_OPT ("-n <loglevel>", "Set level at which printing of messages is done to the console") |
185 | | BAREBOX_CMD_HELP_OPT ("-r", "Print timestamp and log-level prefixes") |
186 | | BAREBOX_CMD_HELP_OPT ("-t", "Show timestamp information") |
187 | | BAREBOX_CMD_HELP_END |
188 | | |
189 | | BAREBOX_CMD_START(dmesg) |
190 | | .cmd = do_dmesg, |
191 | | BAREBOX_CMD_DESC("Print or control log messages") |
192 | | BAREBOX_CMD_OPTS("[-cdelnprt]") |
193 | | BAREBOX_CMD_GROUP(CMD_GRP_INFO) |
194 | | BAREBOX_CMD_HELP(cmd_dmesg_help) |
195 | | BAREBOX_CMD_END |