xref: /openbmc/u-boot/cmd/log.c (revision 8ee59472)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2017 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <command.h>
9 #include <dm.h>
10 #include <log.h>
11 
12 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
13 
14 static int do_log_level(cmd_tbl_t *cmdtp, int flag, int argc,
15 			char * const argv[])
16 {
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 
25 static int do_log_format(cmd_tbl_t *cmdtp, int flag, int argc,
26 			 char * const argv[])
27 {
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 
61 static int do_log_rec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
62 {
63 	enum log_category_t cat;
64 	enum log_level_t level;
65 	const char *file;
66 	uint line;
67 	const char *func;
68 	const char *msg;
69 	char *end;
70 
71 	if (argc < 7)
72 		return CMD_RET_USAGE;
73 	cat = log_get_cat_by_name(argv[1]);
74 	level = simple_strtoul(argv[2], &end, 10);
75 	if (end == argv[2]) {
76 		level = log_get_level_by_name(argv[2]);
77 
78 		if (level == LOGL_NONE) {
79 			printf("Invalid log level '%s'\n", argv[2]);
80 			return CMD_RET_USAGE;
81 		}
82 	}
83 	if (level >= LOGL_MAX) {
84 		printf("Invalid log level %u\n", level);
85 		return CMD_RET_USAGE;
86 	}
87 	file = argv[3];
88 	line = simple_strtoul(argv[4], NULL, 10);
89 	func = argv[5];
90 	msg = argv[6];
91 	if (_log(cat, level, file, line, func, "%s\n", msg))
92 		return CMD_RET_FAILURE;
93 
94 	return 0;
95 }
96 
97 static cmd_tbl_t log_sub[] = {
98 	U_BOOT_CMD_MKENT(level, CONFIG_SYS_MAXARGS, 1, do_log_level, "", ""),
99 #ifdef CONFIG_LOG_TEST
100 	U_BOOT_CMD_MKENT(test, 2, 1, do_log_test, "", ""),
101 #endif
102 	U_BOOT_CMD_MKENT(format, CONFIG_SYS_MAXARGS, 1, do_log_format, "", ""),
103 	U_BOOT_CMD_MKENT(rec, CONFIG_SYS_MAXARGS, 1, do_log_rec, "", ""),
104 };
105 
106 static int do_log(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
107 {
108 	cmd_tbl_t *cp;
109 
110 	if (argc < 2)
111 		return CMD_RET_USAGE;
112 
113 	/* drop initial "log" arg */
114 	argc--;
115 	argv++;
116 
117 	cp = find_cmd_tbl(argv[0], log_sub, ARRAY_SIZE(log_sub));
118 	if (cp)
119 		return cp->cmd(cmdtp, flag, argc, argv);
120 
121 	return CMD_RET_USAGE;
122 }
123 
124 #ifdef CONFIG_SYS_LONGHELP
125 static char log_help_text[] =
126 	"level - get/set log level\n"
127 #ifdef CONFIG_LOG_TEST
128 	"log test - run log tests\n"
129 #endif
130 	"log format <fmt> - set log output format. <fmt> is a string where\n"
131 	"\teach letter indicates something that should be displayed:\n"
132 	"\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
133 	"\tor 'default', equivalent to 'fm', or 'all' for all\n"
134 	"log rec <category> <level> <file> <line> <func> <message> - "
135 		"output a log record"
136 	;
137 #endif
138 
139 U_BOOT_CMD(
140 	log, CONFIG_SYS_MAXARGS, 1, do_log,
141 	"log system", log_help_text
142 );
143