blob: d29b559410fbdc0658caa9e717680c88765a089f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassef11ed82017-12-04 13:48:27 -07002/*
3 * Logging support test program
4 *
5 * Copyright (c) 2017 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glassef11ed82017-12-04 13:48:27 -07007 */
8
9#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060010#include <command.h>
Simon Glassef11ed82017-12-04 13:48:27 -070011
12/* emit some sample log records in different ways, for testing */
13static int log_run(enum uclass_id cat, const char *file)
14{
15 int i;
16
17 debug("debug\n");
18 for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
19 log(cat, i, "log %d\n", i);
20 _log(log_uc_cat(cat), i, file, 100 + i, "func", "_log %d\n",
21 i);
22 }
23
24 return 0;
25}
26
27static int log_test(int testnum)
28{
29 int ret;
30
31 printf("test %d\n", testnum);
32 switch (testnum) {
33 case 0: {
34 /* Check a category filter using the first category */
35 enum log_category_t cat_list[] = {
36 log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI),
37 LOGC_NONE, LOGC_END
38 };
39
40 ret = log_add_filter("console", cat_list, LOGL_MAX, NULL);
41 if (ret < 0)
42 return ret;
43 log_run(UCLASS_MMC, "file");
44 ret = log_remove_filter("console", ret);
45 if (ret < 0)
46 return ret;
47 break;
48 }
49 case 1: {
50 /* Check a category filter using the second category */
51 enum log_category_t cat_list[] = {
52 log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI), LOGC_END
53 };
54
55 ret = log_add_filter("console", cat_list, LOGL_MAX, NULL);
56 if (ret < 0)
57 return ret;
58 log_run(UCLASS_SPI, "file");
59 ret = log_remove_filter("console", ret);
60 if (ret < 0)
61 return ret;
62 break;
63 }
64 case 2: {
65 /* Check a category filter that should block log entries */
66 enum log_category_t cat_list[] = {
67 log_uc_cat(UCLASS_MMC), LOGC_NONE, LOGC_END
68 };
69
70 ret = log_add_filter("console", cat_list, LOGL_MAX, NULL);
71 if (ret < 0)
72 return ret;
73 log_run(UCLASS_SPI, "file");
74 ret = log_remove_filter("console", ret);
75 if (ret < 0)
76 return ret;
77 break;
78 }
79 case 3: {
80 /* Check a passing file filter */
81 ret = log_add_filter("console", NULL, LOGL_MAX, "file");
82 if (ret < 0)
83 return ret;
84 log_run(UCLASS_SPI, "file");
85 ret = log_remove_filter("console", ret);
86 if (ret < 0)
87 return ret;
88 break;
89 }
90 case 4: {
91 /* Check a failing file filter */
92 ret = log_add_filter("console", NULL, LOGL_MAX, "file");
93 if (ret < 0)
94 return ret;
95 log_run(UCLASS_SPI, "file2");
96 ret = log_remove_filter("console", ret);
97 if (ret < 0)
98 return ret;
99 break;
100 }
101 case 5: {
102 /* Check a passing file filter (second in list) */
103 ret = log_add_filter("console", NULL, LOGL_MAX, "file,file2");
104 if (ret < 0)
105 return ret;
106 log_run(UCLASS_SPI, "file2");
107 ret = log_remove_filter("console", ret);
108 if (ret < 0)
109 return ret;
110 break;
111 }
112 case 6: {
113 /* Check a passing file filter */
114 ret = log_add_filter("console", NULL, LOGL_MAX,
115 "file,file2,log/log_test.c");
116 if (ret < 0)
117 return ret;
118 log_run(UCLASS_SPI, "file2");
119 ret = log_remove_filter("console", ret);
120 if (ret < 0)
121 return ret;
122 break;
123 }
124 case 7: {
125 /* Check a log level filter */
126 ret = log_add_filter("console", NULL, LOGL_WARNING, NULL);
127 if (ret < 0)
128 return ret;
129 log_run(UCLASS_SPI, "file");
130 ret = log_remove_filter("console", ret);
131 if (ret < 0)
132 return ret;
133 break;
134 }
135 case 8: {
136 /* Check two filters, one of which passes everything */
137 int filt1, filt2;
138
139 ret = log_add_filter("console", NULL, LOGL_WARNING, NULL);
140 if (ret < 0)
141 return ret;
142 filt1 = ret;
143 ret = log_add_filter("console", NULL, LOGL_MAX, NULL);
144 if (ret < 0)
145 return ret;
146 filt2 = ret;
147 log_run(UCLASS_SPI, "file");
148 ret = log_remove_filter("console", filt1);
149 if (ret < 0)
150 return ret;
151 ret = log_remove_filter("console", filt2);
152 if (ret < 0)
153 return ret;
154 break;
155 }
156 case 9: {
157 /* Check three filters, which together pass everything */
158 int filt1, filt2, filt3;
159
160 ret = log_add_filter("console", NULL, LOGL_MAX, "file)");
161 if (ret < 0)
162 return ret;
163 filt1 = ret;
164 ret = log_add_filter("console", NULL, LOGL_MAX, "file2");
165 if (ret < 0)
166 return ret;
167 filt2 = ret;
168 ret = log_add_filter("console", NULL, LOGL_MAX,
169 "log/log_test.c");
170 if (ret < 0)
171 return ret;
172 filt3 = ret;
173 log_run(UCLASS_SPI, "file2");
174 ret = log_remove_filter("console", filt1);
175 if (ret < 0)
176 return ret;
177 ret = log_remove_filter("console", filt2);
178 if (ret < 0)
179 return ret;
180 ret = log_remove_filter("console", filt3);
181 if (ret < 0)
182 return ret;
183 break;
184 }
Simon Glasscdd140a2018-10-01 11:55:06 -0600185 case 10: {
186 log_err("level %d\n", LOGL_EMERG);
187 log_err("level %d\n", LOGL_ALERT);
188 log_err("level %d\n", LOGL_CRIT);
189 log_err("level %d\n", LOGL_ERR);
190 log_warning("level %d\n", LOGL_WARNING);
191 log_notice("level %d\n", LOGL_NOTICE);
192 log_info("level %d\n", LOGL_INFO);
193 log_debug("level %d\n", LOGL_DEBUG);
194 log_content("level %d\n", LOGL_DEBUG_CONTENT);
195 log_io("level %d\n", LOGL_DEBUG_IO);
196 break;
197 }
Simon Glassef11ed82017-12-04 13:48:27 -0700198 }
199
200 return 0;
201}
202
203#ifdef CONFIG_LOG_TEST
Simon Glass09140112020-05-10 11:40:03 -0600204int do_log_test(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glassef11ed82017-12-04 13:48:27 -0700205{
206 int testnum = 0;
207 int ret;
208
209 if (argc > 1)
210 testnum = simple_strtoul(argv[1], NULL, 10);
211
212 ret = log_test(testnum);
213 if (ret)
214 printf("Test failure (err=%d)\n", ret);
215
216 return ret ? CMD_RET_FAILURE : 0;
217}
218#endif