blob: 664f7bd7ac4ed7a0e9eadc88ef53fc7552112b70 [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
Simon Glassed38aef2020-05-10 11:40:03 -060014static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
15 char *const argv[])
Simon Glass5684f8f2017-12-04 13:48:26 -070016{
17 if (argc > 1)
18 gd->default_log_level = simple_strtol(argv[1], NULL, 10);
19 else
20 printf("Default log level: %d\n", gd->default_log_level);
21
22 return 0;
23}
24
Simon Glassed38aef2020-05-10 11:40:03 -060025static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
26 char *const argv[])
Simon Glassea0049b2017-12-28 13:14:19 -070027{
28 int i;
29
30 if (argc > 1) {
31 const char *str = argv[1];
32
33 if (!strcmp(str, "default")) {
34 gd->log_fmt = LOGF_DEFAULT;
35 } else if (!strcmp(str, "all")) {
36 gd->log_fmt = LOGF_ALL;
37 } else {
38 gd->log_fmt = 0;
39 for (; *str; str++) {
40 char *ptr = strchr(log_fmt_chars, *str);
41
42 if (!ptr) {
43 printf("Invalid log char '%c'\n", *str);
44 return CMD_RET_FAILURE;
45 }
46 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
47 }
48 }
49 } else {
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]);
54 }
55 printf("\n");
56 }
57
58 return 0;
59}
60
Simon Glassed38aef2020-05-10 11:40:03 -060061static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
62 char *const argv[])
Simon Glassdf12ab52017-12-28 13:14:20 -070063{
64 enum log_category_t cat;
65 enum log_level_t level;
66 const char *file;
67 uint line;
68 const char *func;
69 const char *msg;
70 char *end;
71
72 if (argc < 7)
73 return CMD_RET_USAGE;
74 cat = log_get_cat_by_name(argv[1]);
75 level = simple_strtoul(argv[2], &end, 10);
76 if (end == argv[2]) {
77 level = log_get_level_by_name(argv[2]);
78
79 if (level == LOGL_NONE) {
80 printf("Invalid log level '%s'\n", argv[2]);
81 return CMD_RET_USAGE;
82 }
83 }
84 if (level >= LOGL_MAX) {
85 printf("Invalid log level %u\n", level);
86 return CMD_RET_USAGE;
87 }
88 file = argv[3];
89 line = simple_strtoul(argv[4], NULL, 10);
90 func = argv[5];
91 msg = argv[6];
92 if (_log(cat, level, file, line, func, "%s\n", msg))
93 return CMD_RET_FAILURE;
94
95 return 0;
96}
97
Simon Glassed38aef2020-05-10 11:40:03 -060098static struct cmd_tbl log_sub[] = {
Simon Glass5684f8f2017-12-04 13:48:26 -070099 U_BOOT_CMD_MKENT(level, CONFIG_SYS_MAXARGS, 1, do_log_level, "", ""),
Simon Glass14c8a062017-12-04 13:48:27 -0700100#ifdef CONFIG_LOG_TEST
101 U_BOOT_CMD_MKENT(test, 2, 1, do_log_test, "", ""),
102#endif
Simon Glassea0049b2017-12-28 13:14:19 -0700103 U_BOOT_CMD_MKENT(format, CONFIG_SYS_MAXARGS, 1, do_log_format, "", ""),
Simon Glassdf12ab52017-12-28 13:14:20 -0700104 U_BOOT_CMD_MKENT(rec, CONFIG_SYS_MAXARGS, 1, do_log_rec, "", ""),
Simon Glass5684f8f2017-12-04 13:48:26 -0700105};
106
Simon Glassed38aef2020-05-10 11:40:03 -0600107static int do_log(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glass5684f8f2017-12-04 13:48:26 -0700108{
Simon Glassed38aef2020-05-10 11:40:03 -0600109 struct cmd_tbl *cp;
Simon Glass5684f8f2017-12-04 13:48:26 -0700110
111 if (argc < 2)
112 return CMD_RET_USAGE;
113
114 /* drop initial "log" arg */
115 argc--;
116 argv++;
117
118 cp = find_cmd_tbl(argv[0], log_sub, ARRAY_SIZE(log_sub));
119 if (cp)
120 return cp->cmd(cmdtp, flag, argc, argv);
121
122 return CMD_RET_USAGE;
123}
124
125#ifdef CONFIG_SYS_LONGHELP
126static char log_help_text[] =
127 "level - get/set log level\n"
Simon Glass14c8a062017-12-04 13:48:27 -0700128#ifdef CONFIG_LOG_TEST
129 "log test - run log tests\n"
130#endif
Simon Glassea0049b2017-12-28 13:14:19 -0700131 "log format <fmt> - set log output format. <fmt> is a string where\n"
132 "\teach letter indicates something that should be displayed:\n"
133 "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
Simon Glassdf12ab52017-12-28 13:14:20 -0700134 "\tor 'default', equivalent to 'fm', or 'all' for all\n"
135 "log rec <category> <level> <file> <line> <func> <message> - "
136 "output a log record"
Simon Glass5684f8f2017-12-04 13:48:26 -0700137 ;
138#endif
139
140U_BOOT_CMD(
141 log, CONFIG_SYS_MAXARGS, 1, do_log,
142 "log system", log_help_text
143);