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