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> |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 12 | #include <test/log.h> |
| 13 | #include <test/ut.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 16 | |
| 17 | /* emit some sample log records in different ways, for testing */ |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 18 | static int do_log_run(int cat, const char *file) |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 19 | { |
| 20 | int i; |
| 21 | |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 22 | gd->log_fmt = LOGF_TEST; |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 23 | debug("debug\n"); |
| 24 | for (i = LOGL_FIRST; i < LOGL_COUNT; i++) { |
| 25 | log(cat, i, "log %d\n", i); |
| 26 | _log(log_uc_cat(cat), i, file, 100 + i, "func", "_log %d\n", |
| 27 | i); |
| 28 | } |
| 29 | |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 30 | gd->log_fmt = log_get_default_format(); |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 31 | return 0; |
| 32 | } |
| 33 | |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 34 | #define log_run_cat(cat) do_log_run(cat, "file") |
| 35 | #define log_run_file(file) do_log_run(UCLASS_SPI, file) |
| 36 | #define log_run() do_log_run(UCLASS_SPI, "file") |
| 37 | |
| 38 | #define EXPECT_LOG BIT(0) |
| 39 | #define EXPECT_DIRECT BIT(1) |
| 40 | #define EXPECT_EXTRA BIT(2) |
| 41 | |
| 42 | static int do_check_log_entries(struct unit_test_state *uts, int flags, int min, |
| 43 | int max) |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 44 | { |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 45 | int i; |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 46 | |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 47 | for (i = min; i <= max; i++) { |
| 48 | if (flags & EXPECT_LOG) |
| 49 | ut_assert_nextline("do_log_run() log %d", i); |
| 50 | if (flags & EXPECT_DIRECT) |
| 51 | ut_assert_nextline("func() _log %d", i); |
| 52 | } |
| 53 | if (flags & EXPECT_EXTRA) |
| 54 | for (; i <= LOGL_MAX ; i++) |
| 55 | ut_assert_nextline("func() _log %d", i); |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 56 | |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 57 | ut_assert_console_end(); |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 61 | #define check_log_entries_flags_levels(flags, min, max) do {\ |
| 62 | int ret = do_check_log_entries(uts, flags, min, max); \ |
| 63 | if (ret) \ |
| 64 | return ret; \ |
| 65 | } while (0) |
| 66 | |
| 67 | #define check_log_entries_flags(flags) \ |
| 68 | check_log_entries_flags_levels(flags, LOGL_FIRST, _LOG_MAX_LEVEL) |
| 69 | #define check_log_entries() check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT) |
| 70 | #define check_log_entries_extra() \ |
| 71 | check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_EXTRA) |
| 72 | #define check_log_entries_none() check_log_entries_flags(0) |
| 73 | |
| 74 | /* Check a category filter using the first category */ |
| 75 | int log_test_00(struct unit_test_state *uts) |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 76 | { |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 77 | enum log_category_t cat_list[] = { |
| 78 | log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI), |
| 79 | LOGC_NONE, LOGC_END |
| 80 | }; |
| 81 | int filt; |
| 82 | |
| 83 | filt = log_add_filter("console", cat_list, LOGL_MAX, NULL); |
| 84 | ut_assert(filt >= 0); |
| 85 | |
| 86 | ut_assertok(console_record_reset_enable()); |
| 87 | log_run_cat(UCLASS_MMC); |
| 88 | check_log_entries_extra(); |
| 89 | |
| 90 | ut_assertok(console_record_reset_enable()); |
| 91 | log_run_cat(UCLASS_SPI); |
| 92 | check_log_entries_extra(); |
| 93 | |
| 94 | ut_assertok(log_remove_filter("console", filt)); |
| 95 | return 0; |
| 96 | } |
| 97 | LOG_TEST_FLAGS(log_test_00, UT_TESTF_CONSOLE_REC); |
| 98 | |
| 99 | /* Check a category filter that should block log entries */ |
| 100 | int log_test_02(struct unit_test_state *uts) |
| 101 | { |
| 102 | enum log_category_t cat_list[] = { |
| 103 | log_uc_cat(UCLASS_MMC), LOGC_NONE, LOGC_END |
| 104 | }; |
| 105 | int filt; |
| 106 | |
| 107 | filt = log_add_filter("console", cat_list, LOGL_MAX, NULL); |
| 108 | ut_assert(filt >= 0); |
| 109 | |
| 110 | ut_assertok(console_record_reset_enable()); |
| 111 | log_run_cat(UCLASS_SPI); |
| 112 | check_log_entries_none(); |
| 113 | |
| 114 | ut_assertok(log_remove_filter("console", filt)); |
| 115 | return 0; |
| 116 | } |
| 117 | LOG_TEST_FLAGS(log_test_02, UT_TESTF_CONSOLE_REC); |
| 118 | |
| 119 | /* Check passing and failing file filters */ |
| 120 | int log_test_03(struct unit_test_state *uts) |
| 121 | { |
| 122 | int filt; |
| 123 | |
| 124 | filt = log_add_filter("console", NULL, LOGL_MAX, "file"); |
| 125 | ut_assert(filt >= 0); |
| 126 | |
| 127 | ut_assertok(console_record_reset_enable()); |
| 128 | log_run_file("file"); |
| 129 | check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA); |
| 130 | |
| 131 | ut_assertok(console_record_reset_enable()); |
| 132 | log_run_file("file2"); |
| 133 | check_log_entries_none(); |
| 134 | |
| 135 | ut_assertok(log_remove_filter("console", filt)); |
| 136 | return 0; |
| 137 | } |
| 138 | LOG_TEST_FLAGS(log_test_03, UT_TESTF_CONSOLE_REC); |
| 139 | |
| 140 | /* Check a passing file filter (second in list) */ |
| 141 | int log_test_05(struct unit_test_state *uts) |
| 142 | { |
| 143 | int filt; |
| 144 | |
| 145 | filt = log_add_filter("console", NULL, LOGL_MAX, "file,file2"); |
| 146 | ut_assert(filt >= 0); |
| 147 | |
| 148 | ut_assertok(console_record_reset_enable()); |
| 149 | log_run_file("file2"); |
| 150 | check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA); |
| 151 | |
| 152 | ut_assertok(log_remove_filter("console", filt)); |
| 153 | return 0; |
| 154 | } |
| 155 | LOG_TEST_FLAGS(log_test_05, UT_TESTF_CONSOLE_REC); |
| 156 | |
| 157 | /* Check a passing file filter (middle of list) */ |
| 158 | int log_test_06(struct unit_test_state *uts) |
| 159 | { |
| 160 | int filt; |
| 161 | |
| 162 | filt = log_add_filter("console", NULL, LOGL_MAX, |
| 163 | "file,file2,log/log_test.c"); |
| 164 | ut_assert(filt >= 0); |
| 165 | |
| 166 | ut_assertok(console_record_reset_enable()); |
| 167 | log_run_file("file2"); |
| 168 | check_log_entries_extra(); |
| 169 | |
| 170 | ut_assertok(log_remove_filter("console", filt)); |
| 171 | return 0; |
| 172 | } |
| 173 | LOG_TEST_FLAGS(log_test_06, UT_TESTF_CONSOLE_REC); |
| 174 | |
| 175 | /* Check a log level filter */ |
| 176 | int log_test_07(struct unit_test_state *uts) |
| 177 | { |
| 178 | int filt; |
| 179 | |
| 180 | filt = log_add_filter("console", NULL, LOGL_WARNING, NULL); |
| 181 | ut_assert(filt >= 0); |
| 182 | |
| 183 | ut_assertok(console_record_reset_enable()); |
| 184 | log_run(); |
| 185 | check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT, LOGL_FIRST, |
| 186 | LOGL_WARNING); |
| 187 | |
| 188 | ut_assertok(log_remove_filter("console", filt)); |
| 189 | return 0; |
| 190 | } |
| 191 | LOG_TEST_FLAGS(log_test_07, UT_TESTF_CONSOLE_REC); |
| 192 | |
| 193 | /* Check two filters, one of which passes everything */ |
| 194 | int log_test_08(struct unit_test_state *uts) |
| 195 | { |
| 196 | int filt1, filt2; |
| 197 | |
| 198 | filt1 = log_add_filter("console", NULL, LOGL_WARNING, NULL); |
| 199 | ut_assert(filt1 >= 0); |
| 200 | filt2 = log_add_filter("console", NULL, LOGL_MAX, NULL); |
| 201 | ut_assert(filt2 >= 0); |
| 202 | |
| 203 | ut_assertok(console_record_reset_enable()); |
| 204 | log_run(); |
| 205 | check_log_entries_extra(); |
| 206 | |
| 207 | ut_assertok(log_remove_filter("console", filt1)); |
| 208 | ut_assertok(log_remove_filter("console", filt2)); |
| 209 | return 0; |
| 210 | } |
| 211 | LOG_TEST_FLAGS(log_test_08, UT_TESTF_CONSOLE_REC); |
| 212 | |
| 213 | /* Check three filters, which together pass everything */ |
| 214 | int log_test_09(struct unit_test_state *uts) |
| 215 | { |
| 216 | int filt1, filt2, filt3; |
| 217 | |
| 218 | filt1 = log_add_filter("console", NULL, LOGL_MAX, "file)"); |
| 219 | ut_assert(filt1 >= 0); |
| 220 | filt2 = log_add_filter("console", NULL, LOGL_MAX, "file2"); |
| 221 | ut_assert(filt2 >= 0); |
| 222 | filt3 = log_add_filter("console", NULL, LOGL_MAX, "log/log_test.c"); |
| 223 | ut_assert(filt3 >= 0); |
| 224 | |
| 225 | ut_assertok(console_record_reset_enable()); |
| 226 | log_run_file("file2"); |
| 227 | check_log_entries_extra(); |
| 228 | |
| 229 | ut_assertok(log_remove_filter("console", filt1)); |
| 230 | ut_assertok(log_remove_filter("console", filt2)); |
| 231 | ut_assertok(log_remove_filter("console", filt3)); |
| 232 | return 0; |
| 233 | } |
| 234 | LOG_TEST_FLAGS(log_test_09, UT_TESTF_CONSOLE_REC); |
| 235 | |
| 236 | int do_log_test_10(struct unit_test_state *uts) |
| 237 | { |
| 238 | int i; |
| 239 | |
| 240 | ut_assertok(console_record_reset_enable()); |
| 241 | log_err("level %d\n", LOGL_EMERG); |
| 242 | log_err("level %d\n", LOGL_ALERT); |
| 243 | log_err("level %d\n", LOGL_CRIT); |
| 244 | log_err("level %d\n", LOGL_ERR); |
| 245 | log_warning("level %d\n", LOGL_WARNING); |
| 246 | log_notice("level %d\n", LOGL_NOTICE); |
| 247 | log_info("level %d\n", LOGL_INFO); |
| 248 | log_debug("level %d\n", LOGL_DEBUG); |
| 249 | log_content("level %d\n", LOGL_DEBUG_CONTENT); |
| 250 | log_io("level %d\n", LOGL_DEBUG_IO); |
| 251 | |
| 252 | for (i = LOGL_EMERG; i <= _LOG_MAX_LEVEL; i++) |
| 253 | ut_assert_nextline("%s() level %d", __func__, i); |
| 254 | ut_assert_console_end(); |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | int log_test_10(struct unit_test_state *uts) |
| 259 | { |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 260 | int ret; |
| 261 | |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 262 | gd->log_fmt = LOGF_TEST; |
| 263 | ret = do_log_test_10(uts); |
| 264 | gd->log_fmt = log_get_default_format(); |
| 265 | return ret; |
Simon Glass | ef11ed8 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 266 | } |
Sean Anderson | 62ef818 | 2020-10-27 19:55:27 -0400 | [diff] [blame^] | 267 | LOG_TEST_FLAGS(log_test_10, UT_TESTF_CONSOLE_REC); |
| 268 | |
| 269 | int do_log_test_11(struct unit_test_state *uts) |
| 270 | { |
| 271 | ut_assertok(console_record_reset_enable()); |
| 272 | log_err("default\n"); |
| 273 | ut_assert_nextline("%s() default", __func__); |
| 274 | |
| 275 | ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), false)); |
| 276 | log_err("disabled\n"); |
| 277 | |
| 278 | ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), true)); |
| 279 | log_err("enabled\n"); |
| 280 | ut_assert_nextline("%s() enabled", __func__); |
| 281 | ut_assert_console_end(); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | int log_test_11(struct unit_test_state *uts) |
| 286 | { |
| 287 | int ret; |
| 288 | |
| 289 | gd->log_fmt = LOGF_TEST; |
| 290 | ret = do_log_test_10(uts); |
| 291 | gd->log_fmt = log_get_default_format(); |
| 292 | return ret; |
| 293 | } |
| 294 | LOG_TEST_FLAGS(log_test_11, UT_TESTF_CONSOLE_REC); |