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