Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 14c8a06 | 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 | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 9 | #include <command.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Sean Anderson | c182182 | 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 | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 16 | |
| 17 | /* emit some sample log records in different ways, for testing */ |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 18 | static int do_log_run(struct unit_test_state *uts, int cat, const char *file) |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 19 | { |
| 20 | int i; |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 21 | int ret, expected_ret; |
| 22 | |
| 23 | if (gd->flags & GD_FLG_LOG_READY) |
| 24 | expected_ret = 0; |
| 25 | else |
| 26 | expected_ret = -ENOSYS; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 27 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 28 | gd->log_fmt = LOGF_TEST; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 29 | debug("debug\n"); |
| 30 | for (i = LOGL_FIRST; i < LOGL_COUNT; i++) { |
| 31 | log(cat, i, "log %d\n", i); |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 32 | ret = _log(log_uc_cat(cat), i, file, 100 + i, |
| 33 | "func", "_log %d\n", i); |
| 34 | ut_asserteq(ret, expected_ret); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 35 | } |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 36 | /* test with LOGL_COUNT flag */ |
| 37 | for (i = LOGL_FIRST; i < LOGL_COUNT; i++) { |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 38 | ret = _log(log_uc_cat(cat), i | LOGL_FORCE_DEBUG, file, 100 + i, |
| 39 | "func", "_log force %d\n", i); |
| 40 | ut_asserteq(ret, expected_ret); |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 41 | } |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 42 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 43 | gd->log_fmt = log_get_default_format(); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 47 | #define log_run_cat(cat) do_log_run(uts, cat, "file") |
| 48 | #define log_run_file(file) do_log_run(uts, UCLASS_SPI, file) |
| 49 | #define log_run() do_log_run(uts, UCLASS_SPI, "file") |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 50 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 51 | #define EXPECT_LOG BIT(0) |
| 52 | #define EXPECT_DIRECT BIT(1) |
| 53 | #define EXPECT_EXTRA BIT(2) |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 54 | #define EXPECT_FORCE BIT(3) |
Patrick Delaunay | 293798c | 2020-11-27 11:20:58 +0100 | [diff] [blame] | 55 | #define EXPECT_DEBUG BIT(4) |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 56 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 57 | static int do_check_log_entries(struct unit_test_state *uts, int flags, int min, |
| 58 | int max) |
| 59 | { |
| 60 | int i; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 61 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 62 | for (i = min; i <= max; i++) { |
| 63 | if (flags & EXPECT_LOG) |
Simon Glass | 81cd040 | 2021-07-05 16:33:00 -0600 | [diff] [blame] | 64 | ut_assert_nextline(" do_log_run() log %d", i); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 65 | if (flags & EXPECT_DIRECT) |
Simon Glass | 81cd040 | 2021-07-05 16:33:00 -0600 | [diff] [blame] | 66 | ut_assert_nextline(" func() _log %d", i); |
Patrick Delaunay | 293798c | 2020-11-27 11:20:58 +0100 | [diff] [blame] | 67 | if (flags & EXPECT_DEBUG) { |
| 68 | ut_assert_nextline("log %d", i); |
| 69 | ut_assert_nextline("_log %d", i); |
| 70 | } |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 71 | } |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 72 | if (flags & EXPECT_EXTRA) |
| 73 | for (; i <= LOGL_MAX ; i++) |
Simon Glass | 81cd040 | 2021-07-05 16:33:00 -0600 | [diff] [blame] | 74 | ut_assert_nextline(" func() _log %d", i); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 75 | |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 76 | for (i = LOGL_FIRST; i < LOGL_COUNT; i++) { |
| 77 | if (flags & EXPECT_FORCE) |
Simon Glass | 81cd040 | 2021-07-05 16:33:00 -0600 | [diff] [blame] | 78 | ut_assert_nextline(" func() _log force %d", |
| 79 | i); |
Patrick Delaunay | 293798c | 2020-11-27 11:20:58 +0100 | [diff] [blame] | 80 | if (flags & EXPECT_DEBUG) |
| 81 | ut_assert_nextline("_log force %d", i); |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 84 | ut_assert_console_end(); |
| 85 | return 0; |
| 86 | } |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 87 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 88 | #define check_log_entries_flags_levels(flags, min, max) do {\ |
| 89 | int ret = do_check_log_entries(uts, flags, min, max); \ |
| 90 | if (ret) \ |
| 91 | return ret; \ |
| 92 | } while (0) |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 93 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 94 | #define check_log_entries_flags(flags) \ |
| 95 | check_log_entries_flags_levels(flags, LOGL_FIRST, _LOG_MAX_LEVEL) |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 96 | #define check_log_entries() check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 97 | #define check_log_entries_extra() \ |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 98 | check_log_entries_flags(EXPECT_LOG | EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE) |
| 99 | #define check_log_entries_none() check_log_entries_flags(EXPECT_FORCE) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 100 | |
| 101 | /* Check a category filter using the first category */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 102 | int log_test_cat_allow(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 103 | { |
| 104 | enum log_category_t cat_list[] = { |
| 105 | log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI), |
| 106 | LOGC_NONE, LOGC_END |
| 107 | }; |
| 108 | int filt; |
| 109 | |
| 110 | filt = log_add_filter("console", cat_list, LOGL_MAX, NULL); |
| 111 | ut_assert(filt >= 0); |
| 112 | |
| 113 | ut_assertok(console_record_reset_enable()); |
| 114 | log_run_cat(UCLASS_MMC); |
| 115 | check_log_entries_extra(); |
| 116 | |
| 117 | ut_assertok(console_record_reset_enable()); |
| 118 | log_run_cat(UCLASS_SPI); |
| 119 | check_log_entries_extra(); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 120 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 121 | ut_assertok(log_remove_filter("console", filt)); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 124 | LOG_TEST_FLAGS(log_test_cat_allow, UT_TESTF_CONSOLE_REC); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 125 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 126 | /* Check a category filter that should block log entries */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 127 | int log_test_cat_deny_implicit(struct unit_test_state *uts) |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 128 | { |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 129 | enum log_category_t cat_list[] = { |
| 130 | log_uc_cat(UCLASS_MMC), LOGC_NONE, LOGC_END |
| 131 | }; |
| 132 | int filt; |
| 133 | |
| 134 | filt = log_add_filter("console", cat_list, LOGL_MAX, NULL); |
| 135 | ut_assert(filt >= 0); |
| 136 | |
| 137 | ut_assertok(console_record_reset_enable()); |
| 138 | log_run_cat(UCLASS_SPI); |
| 139 | check_log_entries_none(); |
| 140 | |
| 141 | ut_assertok(log_remove_filter("console", filt)); |
| 142 | return 0; |
| 143 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 144 | LOG_TEST_FLAGS(log_test_cat_deny_implicit, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 145 | |
| 146 | /* Check passing and failing file filters */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 147 | int log_test_file(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 148 | { |
| 149 | int filt; |
| 150 | |
| 151 | filt = log_add_filter("console", NULL, LOGL_MAX, "file"); |
| 152 | ut_assert(filt >= 0); |
| 153 | |
| 154 | ut_assertok(console_record_reset_enable()); |
| 155 | log_run_file("file"); |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 156 | check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 157 | |
| 158 | ut_assertok(console_record_reset_enable()); |
| 159 | log_run_file("file2"); |
| 160 | check_log_entries_none(); |
| 161 | |
| 162 | ut_assertok(log_remove_filter("console", filt)); |
| 163 | return 0; |
| 164 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 165 | LOG_TEST_FLAGS(log_test_file, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 166 | |
| 167 | /* Check a passing file filter (second in list) */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 168 | int log_test_file_second(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 169 | { |
| 170 | int filt; |
| 171 | |
| 172 | filt = log_add_filter("console", NULL, LOGL_MAX, "file,file2"); |
| 173 | ut_assert(filt >= 0); |
| 174 | |
| 175 | ut_assertok(console_record_reset_enable()); |
| 176 | log_run_file("file2"); |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 177 | check_log_entries_flags(EXPECT_DIRECT | EXPECT_EXTRA | EXPECT_FORCE); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 178 | |
| 179 | ut_assertok(log_remove_filter("console", filt)); |
| 180 | return 0; |
| 181 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 182 | LOG_TEST_FLAGS(log_test_file_second, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 183 | |
| 184 | /* Check a passing file filter (middle of list) */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 185 | int log_test_file_mid(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 186 | { |
| 187 | int filt; |
| 188 | |
| 189 | filt = log_add_filter("console", NULL, LOGL_MAX, |
| 190 | "file,file2,log/log_test.c"); |
| 191 | ut_assert(filt >= 0); |
| 192 | |
| 193 | ut_assertok(console_record_reset_enable()); |
| 194 | log_run_file("file2"); |
| 195 | check_log_entries_extra(); |
| 196 | |
| 197 | ut_assertok(log_remove_filter("console", filt)); |
| 198 | return 0; |
| 199 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 200 | LOG_TEST_FLAGS(log_test_file_mid, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 201 | |
| 202 | /* Check a log level filter */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 203 | int log_test_level(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 204 | { |
| 205 | int filt; |
| 206 | |
| 207 | filt = log_add_filter("console", NULL, LOGL_WARNING, NULL); |
| 208 | ut_assert(filt >= 0); |
| 209 | |
| 210 | ut_assertok(console_record_reset_enable()); |
| 211 | log_run(); |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 212 | check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE, |
| 213 | LOGL_FIRST, LOGL_WARNING); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 214 | |
| 215 | ut_assertok(log_remove_filter("console", filt)); |
| 216 | return 0; |
| 217 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 218 | LOG_TEST_FLAGS(log_test_level, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 219 | |
| 220 | /* Check two filters, one of which passes everything */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 221 | int log_test_double(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 222 | { |
| 223 | int filt1, filt2; |
| 224 | |
| 225 | filt1 = log_add_filter("console", NULL, LOGL_WARNING, NULL); |
| 226 | ut_assert(filt1 >= 0); |
| 227 | filt2 = log_add_filter("console", NULL, LOGL_MAX, NULL); |
| 228 | ut_assert(filt2 >= 0); |
| 229 | |
| 230 | ut_assertok(console_record_reset_enable()); |
| 231 | log_run(); |
| 232 | check_log_entries_extra(); |
| 233 | |
| 234 | ut_assertok(log_remove_filter("console", filt1)); |
| 235 | ut_assertok(log_remove_filter("console", filt2)); |
| 236 | return 0; |
| 237 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 238 | LOG_TEST_FLAGS(log_test_double, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 239 | |
| 240 | /* Check three filters, which together pass everything */ |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 241 | int log_test_triple(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 242 | { |
| 243 | int filt1, filt2, filt3; |
| 244 | |
| 245 | filt1 = log_add_filter("console", NULL, LOGL_MAX, "file)"); |
| 246 | ut_assert(filt1 >= 0); |
| 247 | filt2 = log_add_filter("console", NULL, LOGL_MAX, "file2"); |
| 248 | ut_assert(filt2 >= 0); |
| 249 | filt3 = log_add_filter("console", NULL, LOGL_MAX, "log/log_test.c"); |
| 250 | ut_assert(filt3 >= 0); |
| 251 | |
| 252 | ut_assertok(console_record_reset_enable()); |
| 253 | log_run_file("file2"); |
| 254 | check_log_entries_extra(); |
| 255 | |
| 256 | ut_assertok(log_remove_filter("console", filt1)); |
| 257 | ut_assertok(log_remove_filter("console", filt2)); |
| 258 | ut_assertok(log_remove_filter("console", filt3)); |
| 259 | return 0; |
| 260 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 261 | LOG_TEST_FLAGS(log_test_triple, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 262 | |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 263 | int do_log_test_helpers(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 264 | { |
| 265 | int i; |
| 266 | |
| 267 | ut_assertok(console_record_reset_enable()); |
| 268 | log_err("level %d\n", LOGL_EMERG); |
| 269 | log_err("level %d\n", LOGL_ALERT); |
| 270 | log_err("level %d\n", LOGL_CRIT); |
| 271 | log_err("level %d\n", LOGL_ERR); |
| 272 | log_warning("level %d\n", LOGL_WARNING); |
| 273 | log_notice("level %d\n", LOGL_NOTICE); |
| 274 | log_info("level %d\n", LOGL_INFO); |
| 275 | log_debug("level %d\n", LOGL_DEBUG); |
| 276 | log_content("level %d\n", LOGL_DEBUG_CONTENT); |
| 277 | log_io("level %d\n", LOGL_DEBUG_IO); |
| 278 | |
Heinrich Schuchardt | 3f700dc | 2022-07-11 08:21:30 +0200 | [diff] [blame] | 279 | for (i = LOGL_EMERG; i <= gd->default_log_level; i++) |
Simon Glass | 81cd040 | 2021-07-05 16:33:00 -0600 | [diff] [blame] | 280 | ut_assert_nextline("%*s() level %d", CONFIG_LOGF_FUNC_PAD, |
| 281 | __func__, i); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 282 | ut_assert_console_end(); |
| 283 | return 0; |
| 284 | } |
| 285 | |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 286 | int log_test_helpers(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 287 | { |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 288 | int ret; |
| 289 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 290 | gd->log_fmt = LOGF_TEST; |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 291 | ret = do_log_test_helpers(uts); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 292 | gd->log_fmt = log_get_default_format(); |
| 293 | return ret; |
| 294 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 295 | LOG_TEST_FLAGS(log_test_helpers, UT_TESTF_CONSOLE_REC); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 296 | |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 297 | int do_log_test_disable(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 298 | { |
| 299 | ut_assertok(console_record_reset_enable()); |
| 300 | log_err("default\n"); |
Simon Glass | 81cd040 | 2021-07-05 16:33:00 -0600 | [diff] [blame] | 301 | ut_assert_nextline("%*s() default", CONFIG_LOGF_FUNC_PAD, __func__); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 302 | |
| 303 | ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), false)); |
| 304 | log_err("disabled\n"); |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 305 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 306 | ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), true)); |
| 307 | log_err("enabled\n"); |
Simon Glass | 81cd040 | 2021-07-05 16:33:00 -0600 | [diff] [blame] | 308 | ut_assert_nextline("%*s() enabled", CONFIG_LOGF_FUNC_PAD, __func__); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 309 | ut_assert_console_end(); |
| 310 | return 0; |
| 311 | } |
| 312 | |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 313 | int log_test_disable(struct unit_test_state *uts) |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 314 | { |
| 315 | int ret; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 316 | |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 317 | gd->log_fmt = LOGF_TEST; |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 318 | ret = do_log_test_disable(uts); |
Sean Anderson | c182182 | 2020-10-27 19:55:27 -0400 | [diff] [blame] | 319 | gd->log_fmt = log_get_default_format(); |
| 320 | return ret; |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 321 | } |
Sean Anderson | ba95667 | 2020-10-27 19:55:28 -0400 | [diff] [blame] | 322 | LOG_TEST_FLAGS(log_test_disable, UT_TESTF_CONSOLE_REC); |
Sean Anderson | 14e9986 | 2020-10-27 19:55:29 -0400 | [diff] [blame] | 323 | |
| 324 | /* Check denying based on category */ |
| 325 | int log_test_cat_deny(struct unit_test_state *uts) |
| 326 | { |
| 327 | int filt1, filt2; |
| 328 | enum log_category_t cat_list[] = { |
| 329 | log_uc_cat(UCLASS_SPI), LOGC_END |
| 330 | }; |
| 331 | |
| 332 | filt1 = log_add_filter("console", cat_list, LOGL_MAX, NULL); |
| 333 | ut_assert(filt1 >= 0); |
| 334 | filt2 = log_add_filter_flags("console", cat_list, LOGL_MAX, NULL, |
| 335 | LOGFF_DENY); |
| 336 | ut_assert(filt2 >= 0); |
| 337 | |
| 338 | ut_assertok(console_record_reset_enable()); |
| 339 | log_run_cat(UCLASS_SPI); |
| 340 | check_log_entries_none(); |
| 341 | |
| 342 | ut_assertok(log_remove_filter("console", filt1)); |
| 343 | ut_assertok(log_remove_filter("console", filt2)); |
| 344 | return 0; |
| 345 | } |
| 346 | LOG_TEST_FLAGS(log_test_cat_deny, UT_TESTF_CONSOLE_REC); |
| 347 | |
| 348 | /* Check denying based on file */ |
| 349 | int log_test_file_deny(struct unit_test_state *uts) |
| 350 | { |
| 351 | int filt1, filt2; |
| 352 | |
| 353 | filt1 = log_add_filter("console", NULL, LOGL_MAX, "file"); |
| 354 | ut_assert(filt1 >= 0); |
| 355 | filt2 = log_add_filter_flags("console", NULL, LOGL_MAX, "file", |
| 356 | LOGFF_DENY); |
| 357 | ut_assert(filt2 >= 0); |
| 358 | |
| 359 | ut_assertok(console_record_reset_enable()); |
| 360 | log_run_file("file"); |
| 361 | check_log_entries_none(); |
| 362 | |
| 363 | ut_assertok(log_remove_filter("console", filt1)); |
| 364 | ut_assertok(log_remove_filter("console", filt2)); |
| 365 | return 0; |
| 366 | } |
| 367 | LOG_TEST_FLAGS(log_test_file_deny, UT_TESTF_CONSOLE_REC); |
| 368 | |
| 369 | /* Check denying based on level */ |
| 370 | int log_test_level_deny(struct unit_test_state *uts) |
| 371 | { |
| 372 | int filt1, filt2; |
| 373 | |
| 374 | filt1 = log_add_filter("console", NULL, LOGL_INFO, NULL); |
| 375 | ut_assert(filt1 >= 0); |
| 376 | filt2 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL, |
| 377 | LOGFF_DENY); |
| 378 | ut_assert(filt2 >= 0); |
| 379 | |
| 380 | ut_assertok(console_record_reset_enable()); |
| 381 | log_run(); |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 382 | check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE, |
Heinrich Schuchardt | 3f700dc | 2022-07-11 08:21:30 +0200 | [diff] [blame] | 383 | LOGL_WARNING + 1, |
| 384 | min(gd->default_log_level, LOGL_INFO)); |
Sean Anderson | 14e9986 | 2020-10-27 19:55:29 -0400 | [diff] [blame] | 385 | |
| 386 | ut_assertok(log_remove_filter("console", filt1)); |
| 387 | ut_assertok(log_remove_filter("console", filt2)); |
| 388 | return 0; |
| 389 | } |
| 390 | LOG_TEST_FLAGS(log_test_level_deny, UT_TESTF_CONSOLE_REC); |
Sean Anderson | 4147317 | 2020-10-27 19:55:31 -0400 | [diff] [blame] | 391 | |
| 392 | /* Check matching based on minimum level */ |
| 393 | int log_test_min(struct unit_test_state *uts) |
| 394 | { |
| 395 | int filt1, filt2; |
| 396 | |
| 397 | filt1 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL, |
| 398 | LOGFF_LEVEL_MIN); |
| 399 | ut_assert(filt1 >= 0); |
| 400 | filt2 = log_add_filter_flags("console", NULL, LOGL_INFO, NULL, |
| 401 | LOGFF_DENY | LOGFF_LEVEL_MIN); |
| 402 | ut_assert(filt2 >= 0); |
| 403 | |
| 404 | ut_assertok(console_record_reset_enable()); |
| 405 | log_run(); |
Patrick Delaunay | 529e46f | 2020-11-27 11:20:51 +0100 | [diff] [blame] | 406 | check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT | EXPECT_FORCE, |
Sean Anderson | 4147317 | 2020-10-27 19:55:31 -0400 | [diff] [blame] | 407 | LOGL_WARNING, LOGL_INFO - 1); |
| 408 | |
| 409 | ut_assertok(log_remove_filter("console", filt1)); |
| 410 | ut_assertok(log_remove_filter("console", filt2)); |
| 411 | return 0; |
| 412 | } |
| 413 | LOG_TEST_FLAGS(log_test_min, UT_TESTF_CONSOLE_REC); |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 414 | |
| 415 | /* Check dropped traces */ |
| 416 | int log_test_dropped(struct unit_test_state *uts) |
| 417 | { |
| 418 | /* force LOG not ready */ |
| 419 | gd->flags &= ~(GD_FLG_LOG_READY); |
| 420 | gd->log_drop_count = 0; |
| 421 | |
| 422 | ut_assertok(console_record_reset_enable()); |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 423 | |
Heinrich Schuchardt | 3f700dc | 2022-07-11 08:21:30 +0200 | [diff] [blame] | 424 | log_run(); |
| 425 | ut_asserteq(2 * (LOGL_COUNT - LOGL_FIRST) + |
| 426 | _LOG_MAX_LEVEL - LOGL_FIRST + 1, |
| 427 | gd->log_drop_count); |
Patrick Delaunay | 293798c | 2020-11-27 11:20:58 +0100 | [diff] [blame] | 428 | check_log_entries_flags_levels(EXPECT_DEBUG, LOGL_FIRST, CONFIG_LOG_DEFAULT_LEVEL); |
Patrick Delaunay | cc03497 | 2020-11-27 11:20:53 +0100 | [diff] [blame] | 429 | |
| 430 | gd->flags |= GD_FLG_LOG_READY; |
| 431 | gd->log_drop_count = 0; |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | LOG_TEST_FLAGS(log_test_dropped, UT_TESTF_CONSOLE_REC); |
Simon Glass | 7fb9759 | 2021-05-08 07:00:06 -0600 | [diff] [blame] | 436 | |
| 437 | /* Check log_buffer() */ |
| 438 | int log_test_buffer(struct unit_test_state *uts) |
| 439 | { |
| 440 | u8 *buf; |
| 441 | int i; |
| 442 | |
| 443 | buf = malloc(0x20); |
| 444 | ut_assertnonnull(buf); |
| 445 | memset(buf, '\0', 0x20); |
| 446 | for (i = 0; i < 0x11; i++) |
| 447 | buf[i] = i * 0x11; |
| 448 | |
| 449 | ut_assertok(console_record_reset_enable()); |
| 450 | log_buffer(LOGC_BOOT, LOGL_INFO, 0, buf, 1, 0x12, 0); |
| 451 | |
| 452 | /* This one should product no output due to the debug level */ |
| 453 | log_buffer(LOGC_BOOT, LOGL_DEBUG, 0, buf, 1, 0x12, 0); |
| 454 | |
| 455 | ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........"); |
| 456 | ut_assert_nextline("00000010: 10 00 .."); |
| 457 | ut_assert_console_end(); |
| 458 | free(buf); |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | LOG_TEST_FLAGS(log_test_buffer, UT_TESTF_CONSOLE_REC); |