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