]> Git Repo - J-u-boot.git/blame - cmd/log.c
Merge tag 'u-boot-imx-master-20250127' of https://gitlab.denx.de/u-boot/custodians...
[J-u-boot.git] / cmd / log.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
d5f61f27
SG
2/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <[email protected]>
d5f61f27
SG
5 */
6
d5f61f27
SG
7#include <command.h>
8#include <dm.h>
3e40976a 9#include <getopt.h>
d5f61f27 10#include <log.h>
3e40976a 11#include <malloc.h>
401d1c4f 12#include <asm/global_data.h>
d5f61f27 13
ad0e5039
SG
14static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
15
fed9c2fb
SA
16static enum log_level_t parse_log_level(char *const arg)
17{
18 enum log_level_t ret;
19 ulong level;
20
21 if (!strict_strtoul(arg, 10, &level)) {
22 if (level > _LOG_MAX_LEVEL) {
23 printf("Only log levels <= %d are supported\n",
24 _LOG_MAX_LEVEL);
25 return LOGL_NONE;
26 }
27 return level;
28 }
29
30 ret = log_get_level_by_name(arg);
31 if (ret == LOGL_NONE)
32 printf("Unknown log level \"%s\"\n", arg);
33 return ret;
34}
35
09140113
SG
36static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
37 char *const argv[])
d5f61f27 38{
018aad87
SA
39 enum log_level_t log_level;
40
77007f95 41 if (argc > 1) {
018aad87 42 log_level = parse_log_level(argv[1]);
77007f95 43
fed9c2fb 44 if (log_level == LOGL_NONE)
77007f95 45 return CMD_RET_FAILURE;
77007f95
HS
46 gd->default_log_level = log_level;
47 } else {
018aad87
SA
48 for (log_level = LOGL_FIRST; log_level <= _LOG_MAX_LEVEL;
49 log_level++)
50 printf("%s%s\n", log_get_level_name(log_level),
51 log_level == gd->default_log_level ?
52 " (default)" : "");
77007f95 53 }
d5f61f27 54
fed9c2fb 55 return CMD_RET_SUCCESS;
d5f61f27
SG
56}
57
1c593a10
SA
58static int do_log_categories(struct cmd_tbl *cmdtp, int flag, int argc,
59 char *const argv[])
60{
61 enum log_category_t cat;
62 const char *name;
63
64 for (cat = LOGC_FIRST; cat < LOGC_COUNT; cat++) {
65 name = log_get_cat_name(cat);
66 /*
67 * Invalid category names (e.g. <invalid> or <missing>) begin
68 * with '<'.
69 */
70 if (name[0] == '<')
71 continue;
72 printf("%s\n", name);
73 }
74
75 return CMD_RET_SUCCESS;
76}
77
78static int do_log_drivers(struct cmd_tbl *cmdtp, int flag, int argc,
79 char *const argv[])
80{
81 struct log_device *ldev;
82
83 list_for_each_entry(ldev, &gd->log_head, sibling_node)
84 printf("%s\n", ldev->drv->name);
85
86 return CMD_RET_SUCCESS;
87}
88
3e40976a
SA
89static int do_log_filter_list(struct cmd_tbl *cmdtp, int flag, int argc,
90 char *const argv[])
91{
92 int opt;
93 const char *drv_name = "console";
94 struct getopt_state gs;
95 struct log_filter *filt;
96 struct log_device *ldev;
97
98 getopt_init_state(&gs);
99 while ((opt = getopt(&gs, argc, argv, "d:")) > 0) {
100 switch (opt) {
101 case 'd':
102 drv_name = gs.arg;
103 break;
104 default:
105 return CMD_RET_USAGE;
106 }
107 }
108
109 if (gs.index != argc)
110 return CMD_RET_USAGE;
111
112 ldev = log_device_find_by_name(drv_name);
113 if (!ldev) {
114 printf("Could not find log device for \"%s\"\n", drv_name);
115 return CMD_RET_FAILURE;
116 }
117
3e40976a 118 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
cb43e3e4
HS
119 printf("%-3d: %s %s %s\n", filt->filter_num,
120 filt->flags & LOGFF_DENY ? "DENY" : "ALLOW",
3e40976a
SA
121 filt->flags & LOGFF_LEVEL_MIN ? ">=" : "<=",
122 log_get_level_name(filt->level));
123
124 if (filt->flags & LOGFF_HAS_CAT) {
cb43e3e4
HS
125 printf(" Categories:");
126 for (int i = 0;
127 i < LOGF_MAX_CATEGORIES &&
128 filt->cat_list[i] != LOGC_END;
129 ++i) {
130 printf(" %s",
3e40976a 131 log_get_cat_name(filt->cat_list[i]));
cb43e3e4
HS
132 }
133 printf("\n");
3e40976a 134 }
cb43e3e4
HS
135 if (filt->file_list)
136 printf(" Files: %s\n", filt->file_list);
137 if (filt->func_list)
138 printf(" Functions: %s\n", filt->func_list);
3e40976a
SA
139 }
140
141 return CMD_RET_SUCCESS;
142}
143
144static int do_log_filter_add(struct cmd_tbl *cmdtp, int flag, int argc,
145 char *const argv[])
146{
147 bool level_set = false;
148 bool print_num = false;
149 bool type_set = false;
150 char *file_list = NULL;
cb43e3e4 151 char *func_list = NULL;
3e40976a
SA
152 const char *drv_name = "console";
153 int opt, err;
154 int cat_count = 0;
155 int flags = 0;
156 enum log_category_t cat_list[LOGF_MAX_CATEGORIES + 1];
157 enum log_level_t level = LOGL_MAX;
158 struct getopt_state gs;
159
160 getopt_init_state(&gs);
cb43e3e4 161 while ((opt = getopt(&gs, argc, argv, "Ac:d:Df:F:l:L:p")) > 0) {
3e40976a
SA
162 switch (opt) {
163 case 'A':
164#define do_type() do { \
165 if (type_set) { \
166 printf("Allow or deny set twice\n"); \
167 return CMD_RET_USAGE; \
168 } \
169 type_set = true; \
170} while (0)
171 do_type();
172 break;
173 case 'c': {
174 enum log_category_t cat;
175
176 if (cat_count >= LOGF_MAX_CATEGORIES) {
177 printf("Too many categories\n");
178 return CMD_RET_FAILURE;
179 }
180
181 cat = log_get_cat_by_name(gs.arg);
182 if (cat == LOGC_NONE) {
183 printf("Unknown category \"%s\"\n", gs.arg);
184 return CMD_RET_FAILURE;
185 }
186
187 cat_list[cat_count++] = cat;
188 break;
189 }
190 case 'd':
191 drv_name = gs.arg;
192 break;
193 case 'D':
194 do_type();
195 flags |= LOGFF_DENY;
196 break;
197 case 'f':
198 file_list = gs.arg;
199 break;
cb43e3e4
HS
200 case 'F':
201 func_list = gs.arg;
202 break;
3e40976a
SA
203 case 'l':
204#define do_level() do { \
205 if (level_set) { \
206 printf("Log level set twice\n"); \
207 return CMD_RET_USAGE; \
208 } \
209 level = parse_log_level(gs.arg); \
210 if (level == LOGL_NONE) \
211 return CMD_RET_FAILURE; \
212 level_set = true; \
213} while (0)
214 do_level();
215 break;
216 case 'L':
217 do_level();
218 flags |= LOGFF_LEVEL_MIN;
219 break;
220 case 'p':
221 print_num = true;
222 break;
223 default:
224 return CMD_RET_USAGE;
225 }
226 }
227
228 if (gs.index != argc)
229 return CMD_RET_USAGE;
230
231 cat_list[cat_count] = LOGC_END;
232 err = log_add_filter_flags(drv_name, cat_count ? cat_list : NULL, level,
cb43e3e4 233 file_list, func_list, flags);
3e40976a
SA
234 if (err < 0) {
235 printf("Could not add filter (err = %d)\n", err);
236 return CMD_RET_FAILURE;
237 } else if (print_num) {
238 printf("%d\n", err);
239 }
240
241 return CMD_RET_SUCCESS;
242}
243
244static int do_log_filter_remove(struct cmd_tbl *cmdtp, int flag, int argc,
245 char *const argv[])
246{
247 bool all = false;
248 int opt, err;
249 ulong filter_num;
250 const char *drv_name = "console";
251 struct getopt_state gs;
252
253 getopt_init_state(&gs);
254 while ((opt = getopt(&gs, argc, argv, "ad:")) > 0) {
255 switch (opt) {
256 case 'a':
257 all = true;
258 break;
259 case 'd':
260 drv_name = gs.arg;
261 break;
262 default:
263 return CMD_RET_USAGE;
264 }
265 }
266
267 if (all) {
268 struct log_filter *filt, *tmp_filt;
269 struct log_device *ldev;
270
271 if (gs.index != argc)
272 return CMD_RET_USAGE;
273
274 ldev = log_device_find_by_name(drv_name);
275 if (!ldev) {
276 printf("Could not find log device for \"%s\"\n",
277 drv_name);
278 return CMD_RET_FAILURE;
279 }
280
281 list_for_each_entry_safe(filt, tmp_filt, &ldev->filter_head,
282 sibling_node) {
283 list_del(&filt->sibling_node);
284 free(filt);
285 }
286 } else {
287 if (gs.index + 1 != argc)
288 return CMD_RET_USAGE;
289
290 if (strict_strtoul(argv[gs.index], 10, &filter_num)) {
291 printf("Invalid filter number \"%s\"\n", argv[gs.index]);
292 return CMD_RET_FAILURE;
293 }
294
295 err = log_remove_filter(drv_name, filter_num);
296 if (err) {
297 printf("Could not remove filter (err = %d)\n", err);
298 return CMD_RET_FAILURE;
299 }
300 }
301
302 return CMD_RET_SUCCESS;
303}
304
09140113
SG
305static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
306 char *const argv[])
ad0e5039
SG
307{
308 int i;
309
310 if (argc > 1) {
311 const char *str = argv[1];
312
313 if (!strcmp(str, "default")) {
3c21d773 314 gd->log_fmt = log_get_default_format();
ad0e5039
SG
315 } else if (!strcmp(str, "all")) {
316 gd->log_fmt = LOGF_ALL;
317 } else {
318 gd->log_fmt = 0;
319 for (; *str; str++) {
320 char *ptr = strchr(log_fmt_chars, *str);
321
322 if (!ptr) {
323 printf("Invalid log char '%c'\n", *str);
324 return CMD_RET_FAILURE;
325 }
326 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
327 }
328 }
329 } else {
330 printf("Log format: ");
331 for (i = 0; i < LOGF_COUNT; i++) {
332 if (gd->log_fmt & (1 << i))
333 printf("%c", log_fmt_chars[i]);
334 }
335 printf("\n");
336 }
337
338 return 0;
339}
340
09140113
SG
341static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
342 char *const argv[])
3fd24fa9
SG
343{
344 enum log_category_t cat;
345 enum log_level_t level;
346 const char *file;
347 uint line;
348 const char *func;
349 const char *msg;
350 char *end;
351
352 if (argc < 7)
353 return CMD_RET_USAGE;
354 cat = log_get_cat_by_name(argv[1]);
0b1284eb 355 level = dectoul(argv[2], &end);
3fd24fa9
SG
356 if (end == argv[2]) {
357 level = log_get_level_by_name(argv[2]);
358
359 if (level == LOGL_NONE) {
360 printf("Invalid log level '%s'\n", argv[2]);
361 return CMD_RET_USAGE;
362 }
363 }
364 if (level >= LOGL_MAX) {
365 printf("Invalid log level %u\n", level);
366 return CMD_RET_USAGE;
367 }
368 file = argv[3];
0b1284eb 369 line = dectoul(argv[4], NULL);
3fd24fa9
SG
370 func = argv[5];
371 msg = argv[6];
372 if (_log(cat, level, file, line, func, "%s\n", msg))
373 return CMD_RET_FAILURE;
374
375 return 0;
376}
377
3616218b 378U_BOOT_LONGHELP(log,
018aad87 379 "level [<level>] - get/set log level\n"
1c593a10
SA
380 "categories - list log categories\n"
381 "drivers - list log drivers\n"
3e40976a
SA
382 "log filter-list [OPTIONS] - list all filters for a log driver\n"
383 "\t-d <driver> - Specify the log driver to list filters from; defaults\n"
384 "\t to console\n"
385 "log filter-add [OPTIONS] - add a new filter to a driver\n"
386 "\t-A - Allow messages matching this filter; mutually exclusive with -D\n"
387 "\t This is the default.\n"
388 "\t-c <category> - Category to match; may be specified multiple times\n"
389 "\t-d <driver> - Specify the log driver to add the filter to; defaults\n"
390 "\t to console\n"
391 "\t-D - Deny messages matching this filter; mutually exclusive with -A\n"
cb43e3e4
HS
392 "\t-f <file_list> - A comma-separated list of files to match\n"
393 "\t-F <func_list> - A comma-separated list of functions to match\n"
3e40976a
SA
394 "\t-l <level> - Match log levels less than or equal to <level>;\n"
395 "\t mutually-exclusive with -L\n"
396 "\t-L <level> - Match log levels greather than or equal to <level>;\n"
397 "\t mutually-exclusive with -l\n"
398 "\t-p - Print the filter number on success\n"
399 "log filter-remove [OPTIONS] [<num>] - Remove filter number <num>\n"
400 "\t-a - Remove ALL filters\n"
401 "\t-d <driver> - Specify the log driver to remove the filter from;\n"
402 "\t defaults to console\n"
ad0e5039
SG
403 "log format <fmt> - set log output format. <fmt> is a string where\n"
404 "\teach letter indicates something that should be displayed:\n"
405 "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
3c21d773 406 "\tor 'default', or 'all' for all\n"
3fd24fa9 407 "log rec <category> <level> <file> <line> <func> <message> - "
3616218b 408 "output a log record");
d5f61f27 409
f48b5b56
SA
410U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text,
411 U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level),
1c593a10
SA
412 U_BOOT_SUBCMD_MKENT(categories, 1, 1, do_log_categories),
413 U_BOOT_SUBCMD_MKENT(drivers, 1, 1, do_log_drivers),
3e40976a
SA
414 U_BOOT_SUBCMD_MKENT(filter-list, 3, 1, do_log_filter_list),
415 U_BOOT_SUBCMD_MKENT(filter-add, CONFIG_SYS_MAXARGS, 1,
416 do_log_filter_add),
417 U_BOOT_SUBCMD_MKENT(filter-remove, 4, 1, do_log_filter_remove),
f48b5b56
SA
418 U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format),
419 U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec),
d5f61f27 420);
This page took 0.249256 seconds and 5 git commands to generate.