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