blob: 12bac0e03c54ede4409e48f7112df04f6bc2fbb0 [file] [log] [blame]
Simon Glass5684f8f2017-12-04 13:48:26 -07001/*
2 * Copyright (c) 2017 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <command.h>
10#include <dm.h>
11#include <log.h>
12
Simon Glassea0049b2017-12-28 13:14:19 -070013static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
14
Simon Glass5684f8f2017-12-04 13:48:26 -070015static int do_log_level(cmd_tbl_t *cmdtp, int flag, int argc,
16 char * const argv[])
17{
18 if (argc > 1)
19 gd->default_log_level = simple_strtol(argv[1], NULL, 10);
20 else
21 printf("Default log level: %d\n", gd->default_log_level);
22
23 return 0;
24}
25
Simon Glassea0049b2017-12-28 13:14:19 -070026static int do_log_format(cmd_tbl_t *cmdtp, int flag, int argc,
27 char * const argv[])
28{
29 int i;
30
31 if (argc > 1) {
32 const char *str = argv[1];
33
34 if (!strcmp(str, "default")) {
35 gd->log_fmt = LOGF_DEFAULT;
36 } else if (!strcmp(str, "all")) {
37 gd->log_fmt = LOGF_ALL;
38 } else {
39 gd->log_fmt = 0;
40 for (; *str; str++) {
41 char *ptr = strchr(log_fmt_chars, *str);
42
43 if (!ptr) {
44 printf("Invalid log char '%c'\n", *str);
45 return CMD_RET_FAILURE;
46 }
47 gd->log_fmt |= 1 << (ptr - log_fmt_chars);
48 }
49 }
50 } else {
51 printf("Log format: ");
52 for (i = 0; i < LOGF_COUNT; i++) {
53 if (gd->log_fmt & (1 << i))
54 printf("%c", log_fmt_chars[i]);
55 }
56 printf("\n");
57 }
58
59 return 0;
60}
61
Simon Glassdf12ab52017-12-28 13:14:20 -070062static int do_log_rec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
63{
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 Glass5684f8f2017-12-04 13:48:26 -070098static cmd_tbl_t log_sub[] = {
99 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
107static int do_log(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
108{
109 cmd_tbl_t *cp;
110
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);