xref: /openbmc/u-boot/cmd/log.c (revision 990d06b0)
1 /*
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 
13 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
14 
15 static 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 
26 static 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 
62 static 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 
98 static cmd_tbl_t log_sub[] = {
99 	U_BOOT_CMD_MKENT(level, CONFIG_SYS_MAXARGS, 1, do_log_level, "", ""),
100 #ifdef CONFIG_LOG_TEST
101 	U_BOOT_CMD_MKENT(test, 2, 1, do_log_test, "", ""),
102 #endif
103 	U_BOOT_CMD_MKENT(format, CONFIG_SYS_MAXARGS, 1, do_log_format, "", ""),
104 	U_BOOT_CMD_MKENT(rec, CONFIG_SYS_MAXARGS, 1, do_log_rec, "", ""),
105 };
106 
107 static 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
126 static char log_help_text[] =
127 	"level - get/set log level\n"
128 #ifdef CONFIG_LOG_TEST
129 	"log test - run log tests\n"
130 #endif
131 	"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"
134 	"\tor 'default', equivalent to 'fm', or 'all' for all\n"
135 	"log rec <category> <level> <file> <line> <func> <message> - "
136 		"output a log record"
137 	;
138 #endif
139 
140 U_BOOT_CMD(
141 	log, CONFIG_SYS_MAXARGS, 1, do_log,
142 	"log system", log_help_text
143 );
144