Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Logging support |
| 4 | * |
| 5 | * Copyright (c) 2017 Google, Inc |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 9 | #include <display_options.h> |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 10 | #include <log.h> |
| 11 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Simon Glass | 4f0fdb7 | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 13 | #include <dm/uclass.h> |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Sean Anderson | 3215fbb | 2020-10-27 19:55:22 -0400 | [diff] [blame] | 17 | static const char *const log_cat_name[] = { |
Simon Glass | 4f0fdb7 | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 18 | "none", |
| 19 | "arch", |
| 20 | "board", |
| 21 | "core", |
| 22 | "driver-model", |
| 23 | "device-tree", |
Heinrich Schuchardt | 90979f9 | 2018-01-22 20:10:45 +0100 | [diff] [blame] | 24 | "efi", |
Simon Glass | 31120e1 | 2020-09-27 18:46:13 -0600 | [diff] [blame] | 25 | "alloc", |
| 26 | "sandbox", |
| 27 | "bloblist", |
| 28 | "devres", |
| 29 | "acpi", |
Simon Glass | 842ff44 | 2020-11-04 09:59:13 -0700 | [diff] [blame] | 30 | "boot", |
Simon Glass | aa4bce9 | 2022-03-04 08:43:00 -0700 | [diff] [blame] | 31 | "event", |
Simon Glass | 468247d | 2023-01-28 15:00:16 -0700 | [diff] [blame] | 32 | "fs", |
Simon Glass | fe4c1e2 | 2023-06-01 10:22:43 -0600 | [diff] [blame] | 33 | "expo", |
Simon Glass | 5e392f1 | 2024-09-01 16:26:18 -0600 | [diff] [blame] | 34 | "console", |
Simon Glass | 563eb3d | 2024-10-28 13:47:52 +0100 | [diff] [blame] | 35 | "test", |
Simon Glass | 4f0fdb7 | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Heinrich Schuchardt | 7e7041d | 2020-10-23 13:00:01 +0200 | [diff] [blame] | 38 | _Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE, |
| 39 | "log_cat_name size"); |
| 40 | |
Sean Anderson | 3215fbb | 2020-10-27 19:55:22 -0400 | [diff] [blame] | 41 | static const char *const log_level_name[] = { |
Simon Glass | 4f0fdb7 | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 42 | "EMERG", |
| 43 | "ALERT", |
| 44 | "CRIT", |
| 45 | "ERR", |
| 46 | "WARNING", |
| 47 | "NOTICE", |
| 48 | "INFO", |
| 49 | "DEBUG", |
| 50 | "CONTENT", |
| 51 | "IO", |
| 52 | }; |
| 53 | |
Heinrich Schuchardt | 7e7041d | 2020-10-23 13:00:01 +0200 | [diff] [blame] | 54 | _Static_assert(ARRAY_SIZE(log_level_name) == LOGL_COUNT, "log_level_name size"); |
| 55 | |
| 56 | /* All error responses MUST begin with '<' */ |
Simon Glass | 4f0fdb7 | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 57 | const char *log_get_cat_name(enum log_category_t cat) |
| 58 | { |
Simon Glass | 8909656 | 2018-06-12 00:04:55 -0600 | [diff] [blame] | 59 | const char *name; |
| 60 | |
| 61 | if (cat < 0 || cat >= LOGC_COUNT) |
| 62 | return "<invalid>"; |
Simon Glass | 4f0fdb7 | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 63 | if (cat >= LOGC_NONE) |
| 64 | return log_cat_name[cat - LOGC_NONE]; |
| 65 | |
Heinrich Schuchardt | 60075ea | 2020-06-08 18:04:22 +0200 | [diff] [blame] | 66 | #if CONFIG_IS_ENABLED(DM) |
Simon Glass | 8909656 | 2018-06-12 00:04:55 -0600 | [diff] [blame] | 67 | name = uclass_get_name((enum uclass_id)cat); |
Heinrich Schuchardt | 60075ea | 2020-06-08 18:04:22 +0200 | [diff] [blame] | 68 | #else |
| 69 | name = NULL; |
| 70 | #endif |
Simon Glass | 8909656 | 2018-06-12 00:04:55 -0600 | [diff] [blame] | 71 | |
| 72 | return name ? name : "<missing>"; |
Simon Glass | 4f0fdb7 | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | enum log_category_t log_get_cat_by_name(const char *name) |
| 76 | { |
| 77 | enum uclass_id id; |
| 78 | int i; |
| 79 | |
| 80 | for (i = LOGC_NONE; i < LOGC_COUNT; i++) |
| 81 | if (!strcmp(name, log_cat_name[i - LOGC_NONE])) |
| 82 | return i; |
| 83 | id = uclass_get_by_name(name); |
| 84 | if (id != UCLASS_INVALID) |
| 85 | return (enum log_category_t)id; |
| 86 | |
| 87 | return LOGC_NONE; |
| 88 | } |
| 89 | |
| 90 | const char *log_get_level_name(enum log_level_t level) |
| 91 | { |
| 92 | if (level >= LOGL_COUNT) |
| 93 | return "INVALID"; |
| 94 | return log_level_name[level]; |
| 95 | } |
| 96 | |
| 97 | enum log_level_t log_get_level_by_name(const char *name) |
| 98 | { |
| 99 | int i; |
| 100 | |
| 101 | for (i = 0; i < LOGL_COUNT; i++) { |
| 102 | if (!strcasecmp(log_level_name[i], name)) |
| 103 | return i; |
| 104 | } |
| 105 | |
| 106 | return LOGL_NONE; |
| 107 | } |
| 108 | |
Sean Anderson | cc4960d | 2020-10-27 19:55:24 -0400 | [diff] [blame] | 109 | struct log_device *log_device_find_by_name(const char *drv_name) |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 110 | { |
| 111 | struct log_device *ldev; |
| 112 | |
| 113 | list_for_each_entry(ldev, &gd->log_head, sibling_node) { |
| 114 | if (!strcmp(drv_name, ldev->drv->name)) |
| 115 | return ldev; |
| 116 | } |
| 117 | |
| 118 | return NULL; |
| 119 | } |
| 120 | |
Sean Anderson | cc4960d | 2020-10-27 19:55:24 -0400 | [diff] [blame] | 121 | bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat) |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 122 | { |
| 123 | int i; |
| 124 | |
| 125 | for (i = 0; i < LOGF_MAX_CATEGORIES && cat_list[i] != LOGC_END; i++) { |
| 126 | if (cat_list[i] == cat) |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | return false; |
| 131 | } |
| 132 | |
Sean Anderson | cc4960d | 2020-10-27 19:55:24 -0400 | [diff] [blame] | 133 | bool log_has_file(const char *file_list, const char *file) |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 134 | { |
| 135 | int file_len = strlen(file); |
| 136 | const char *s, *p; |
| 137 | int substr_len; |
| 138 | |
| 139 | for (s = file_list; *s; s = p + (*p != '\0')) { |
| 140 | p = strchrnul(s, ','); |
| 141 | substr_len = p - s; |
| 142 | if (file_len >= substr_len && |
| 143 | !strncmp(file + file_len - substr_len, s, substr_len)) |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * log_passes_filters() - check if a log record passes the filters for a device |
| 152 | * |
| 153 | * @ldev: Log device to check |
| 154 | * @rec: Log record to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 155 | * Return: true if @rec is not blocked by the filters in @ldev, false if it is |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 156 | */ |
| 157 | static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec) |
| 158 | { |
| 159 | struct log_filter *filt; |
| 160 | |
Simon Glass | f080343 | 2021-01-20 20:10:52 -0700 | [diff] [blame] | 161 | if (rec->flags & LOGRECF_FORCE_DEBUG) |
Simon Glass | 9c83cac | 2020-09-12 11:13:34 -0600 | [diff] [blame] | 162 | return true; |
| 163 | |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 164 | /* If there are no filters, filter on the default log level */ |
| 165 | if (list_empty(&ldev->filter_head)) { |
| 166 | if (rec->level > gd->default_log_level) |
| 167 | return false; |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | list_for_each_entry(filt, &ldev->filter_head, sibling_node) { |
Sean Anderson | 34dc804 | 2020-10-27 19:55:30 -0400 | [diff] [blame] | 172 | if (filt->flags & LOGFF_LEVEL_MIN) { |
| 173 | if (rec->level < filt->level) |
| 174 | continue; |
| 175 | } else if (rec->level > filt->level) { |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 176 | continue; |
Sean Anderson | 34dc804 | 2020-10-27 19:55:30 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 179 | if ((filt->flags & LOGFF_HAS_CAT) && |
| 180 | !log_has_cat(filt->cat_list, rec->cat)) |
| 181 | continue; |
Sean Anderson | 34dc804 | 2020-10-27 19:55:30 -0400 | [diff] [blame] | 182 | |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 183 | if (filt->file_list && |
| 184 | !log_has_file(filt->file_list, rec->file)) |
| 185 | continue; |
Sean Anderson | d503212 | 2020-10-27 19:55:26 -0400 | [diff] [blame] | 186 | |
| 187 | if (filt->flags & LOGFF_DENY) |
| 188 | return false; |
| 189 | else |
| 190 | return true; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * log_dispatch() - Send a log record to all log devices for processing |
| 198 | * |
| 199 | * The log record is sent to each log device in turn, skipping those which have |
Heinrich Schuchardt | 0fc9f4f | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 200 | * filters which block the record. |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 201 | * |
Heinrich Schuchardt | 0fc9f4f | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 202 | * All log messages created while processing log record @rec are ignored. |
| 203 | * |
| 204 | * @rec: log record to dispatch |
| 205 | * Return: 0 msg sent, 1 msg not sent while already dispatching another msg |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 206 | */ |
Patrick Delaunay | 03593f3 | 2020-11-27 11:20:59 +0100 | [diff] [blame] | 207 | static int log_dispatch(struct log_rec *rec, const char *fmt, va_list args) |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 208 | { |
| 209 | struct log_device *ldev; |
Patrick Delaunay | 03593f3 | 2020-11-27 11:20:59 +0100 | [diff] [blame] | 210 | char buf[CONFIG_SYS_CBSIZE]; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 211 | |
Heinrich Schuchardt | b5ecfca | 2020-09-14 10:01:00 +0200 | [diff] [blame] | 212 | /* |
| 213 | * When a log driver writes messages (e.g. via the network stack) this |
| 214 | * may result in further generated messages. We cannot process them here |
| 215 | * as this might result in infinite recursion. |
| 216 | */ |
Heinrich Schuchardt | fdf5599 | 2020-10-17 14:31:57 +0200 | [diff] [blame] | 217 | if (gd->processing_msg) |
Heinrich Schuchardt | 0fc9f4f | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 218 | return 1; |
Heinrich Schuchardt | b5ecfca | 2020-09-14 10:01:00 +0200 | [diff] [blame] | 219 | |
| 220 | /* Emit message */ |
Heinrich Schuchardt | fdf5599 | 2020-10-17 14:31:57 +0200 | [diff] [blame] | 221 | gd->processing_msg = true; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 222 | list_for_each_entry(ldev, &gd->log_head, sibling_node) { |
Simon Glass | 885cf5f | 2020-09-12 12:28:47 -0600 | [diff] [blame] | 223 | if ((ldev->flags & LOGDF_ENABLE) && |
Patrick Delaunay | 03593f3 | 2020-11-27 11:20:59 +0100 | [diff] [blame] | 224 | log_passes_filters(ldev, rec)) { |
| 225 | if (!rec->msg) { |
Simon Glass | 5fc47e3 | 2021-01-20 20:10:53 -0700 | [diff] [blame] | 226 | int len; |
| 227 | |
| 228 | len = vsnprintf(buf, sizeof(buf), fmt, args); |
Patrick Delaunay | 03593f3 | 2020-11-27 11:20:59 +0100 | [diff] [blame] | 229 | rec->msg = buf; |
Simon Glass | 5fc47e3 | 2021-01-20 20:10:53 -0700 | [diff] [blame] | 230 | gd->log_cont = len && buf[len - 1] != '\n'; |
Patrick Delaunay | 03593f3 | 2020-11-27 11:20:59 +0100 | [diff] [blame] | 231 | } |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 232 | ldev->drv->emit(ldev, rec); |
Patrick Delaunay | 03593f3 | 2020-11-27 11:20:59 +0100 | [diff] [blame] | 233 | } |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 234 | } |
Heinrich Schuchardt | fdf5599 | 2020-10-17 14:31:57 +0200 | [diff] [blame] | 235 | gd->processing_msg = false; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | int _log(enum log_category_t cat, enum log_level_t level, const char *file, |
| 240 | int line, const char *func, const char *fmt, ...) |
| 241 | { |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 242 | struct log_rec rec; |
| 243 | va_list args; |
| 244 | |
Patrick Delaunay | fd70f9a | 2020-11-27 11:20:52 +0100 | [diff] [blame] | 245 | if (!gd) |
| 246 | return -ENOSYS; |
| 247 | |
Heinrich Schuchardt | 0fc9f4f | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 248 | /* Check for message continuation */ |
| 249 | if (cat == LOGC_CONT) |
| 250 | cat = gd->logc_prev; |
| 251 | if (level == LOGL_CONT) |
| 252 | level = gd->logl_prev; |
| 253 | |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 254 | rec.cat = cat; |
Simon Glass | 9c83cac | 2020-09-12 11:13:34 -0600 | [diff] [blame] | 255 | rec.level = level & LOGL_LEVEL_MASK; |
Simon Glass | f080343 | 2021-01-20 20:10:52 -0700 | [diff] [blame] | 256 | rec.flags = 0; |
| 257 | if (level & LOGL_FORCE_DEBUG) |
| 258 | rec.flags |= LOGRECF_FORCE_DEBUG; |
Simon Glass | 5fc47e3 | 2021-01-20 20:10:53 -0700 | [diff] [blame] | 259 | if (gd->log_cont) |
| 260 | rec.flags |= LOGRECF_CONT; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 261 | rec.file = file; |
| 262 | rec.line = line; |
| 263 | rec.func = func; |
Patrick Delaunay | 03593f3 | 2020-11-27 11:20:59 +0100 | [diff] [blame] | 264 | rec.msg = NULL; |
Patrick Delaunay | fd70f9a | 2020-11-27 11:20:52 +0100 | [diff] [blame] | 265 | |
| 266 | if (!(gd->flags & GD_FLG_LOG_READY)) { |
| 267 | gd->log_drop_count++; |
Patrick Delaunay | 9d36aca | 2020-11-27 11:20:54 +0100 | [diff] [blame] | 268 | |
| 269 | /* display dropped traces with console puts and DEBUG_UART */ |
Simon Glass | f080343 | 2021-01-20 20:10:52 -0700 | [diff] [blame] | 270 | if (rec.level <= CONFIG_LOG_DEFAULT_LEVEL || |
| 271 | rec.flags & LOGRECF_FORCE_DEBUG) { |
Patrick Delaunay | 03593f3 | 2020-11-27 11:20:59 +0100 | [diff] [blame] | 272 | char buf[CONFIG_SYS_CBSIZE]; |
| 273 | |
Patrick Delaunay | 9d36aca | 2020-11-27 11:20:54 +0100 | [diff] [blame] | 274 | va_start(args, fmt); |
| 275 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 276 | puts(buf); |
| 277 | va_end(args); |
| 278 | } |
| 279 | |
Patrick Delaunay | fd70f9a | 2020-11-27 11:20:52 +0100 | [diff] [blame] | 280 | return -ENOSYS; |
| 281 | } |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 282 | va_start(args, fmt); |
Patrick Delaunay | 03593f3 | 2020-11-27 11:20:59 +0100 | [diff] [blame] | 283 | if (!log_dispatch(&rec, fmt, args)) { |
Heinrich Schuchardt | 0fc9f4f | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 284 | gd->logc_prev = cat; |
| 285 | gd->logl_prev = level; |
| 286 | } |
Patrick Delaunay | 03593f3 | 2020-11-27 11:20:59 +0100 | [diff] [blame] | 287 | va_end(args); |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
Simon Glass | 7fb9759 | 2021-05-08 07:00:06 -0600 | [diff] [blame] | 292 | #define MAX_LINE_LENGTH_BYTES 64 |
| 293 | #define DEFAULT_LINE_LENGTH_BYTES 16 |
| 294 | |
| 295 | int _log_buffer(enum log_category_t cat, enum log_level_t level, |
| 296 | const char *file, int line, const char *func, ulong addr, |
| 297 | const void *data, uint width, uint count, uint linelen) |
| 298 | { |
| 299 | if (linelen * width > MAX_LINE_LENGTH_BYTES) |
| 300 | linelen = MAX_LINE_LENGTH_BYTES / width; |
| 301 | if (linelen < 1) |
| 302 | linelen = DEFAULT_LINE_LENGTH_BYTES / width; |
| 303 | |
| 304 | while (count) { |
| 305 | uint thislinelen; |
| 306 | char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)]; |
| 307 | |
| 308 | thislinelen = hexdump_line(addr, data, width, count, linelen, |
| 309 | buf, sizeof(buf)); |
| 310 | assert(thislinelen >= 0); |
| 311 | _log(cat, level, file, line, func, "%s\n", buf); |
| 312 | |
| 313 | /* update references */ |
| 314 | data += thislinelen * width; |
| 315 | addr += thislinelen * width; |
| 316 | count -= thislinelen; |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Sean Anderson | 8c41018 | 2020-10-27 19:55:25 -0400 | [diff] [blame] | 322 | int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[], |
Sean Anderson | 34dc804 | 2020-10-27 19:55:30 -0400 | [diff] [blame] | 323 | enum log_level_t level, const char *file_list, |
Sean Anderson | 8c41018 | 2020-10-27 19:55:25 -0400 | [diff] [blame] | 324 | int flags) |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 325 | { |
| 326 | struct log_filter *filt; |
| 327 | struct log_device *ldev; |
Simon Glass | f14f393 | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 328 | int ret; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 329 | int i; |
| 330 | |
| 331 | ldev = log_device_find_by_name(drv_name); |
| 332 | if (!ldev) |
| 333 | return -ENOENT; |
Heinrich Schuchardt | be9ab32 | 2020-04-19 10:48:36 +0200 | [diff] [blame] | 334 | filt = calloc(1, sizeof(*filt)); |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 335 | if (!filt) |
| 336 | return -ENOMEM; |
| 337 | |
Sean Anderson | 8c41018 | 2020-10-27 19:55:25 -0400 | [diff] [blame] | 338 | filt->flags = flags; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 339 | if (cat_list) { |
| 340 | filt->flags |= LOGFF_HAS_CAT; |
| 341 | for (i = 0; ; i++) { |
Simon Glass | f14f393 | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 342 | if (i == ARRAY_SIZE(filt->cat_list)) { |
| 343 | ret = -ENOSPC; |
| 344 | goto err; |
| 345 | } |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 346 | filt->cat_list[i] = cat_list[i]; |
| 347 | if (cat_list[i] == LOGC_END) |
| 348 | break; |
| 349 | } |
| 350 | } |
Sean Anderson | 34dc804 | 2020-10-27 19:55:30 -0400 | [diff] [blame] | 351 | filt->level = level; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 352 | if (file_list) { |
| 353 | filt->file_list = strdup(file_list); |
Simon Glass | f14f393 | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 354 | if (!filt->file_list) { |
Sean Anderson | 638b65a | 2020-10-27 19:55:20 -0400 | [diff] [blame] | 355 | ret = -ENOMEM; |
Simon Glass | f14f393 | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 356 | goto err; |
| 357 | } |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 358 | } |
| 359 | filt->filter_num = ldev->next_filter_num++; |
Sean Anderson | d503212 | 2020-10-27 19:55:26 -0400 | [diff] [blame] | 360 | /* Add deny filters to the beginning of the list */ |
| 361 | if (flags & LOGFF_DENY) |
| 362 | list_add(&filt->sibling_node, &ldev->filter_head); |
| 363 | else |
| 364 | list_add_tail(&filt->sibling_node, &ldev->filter_head); |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 365 | |
| 366 | return filt->filter_num; |
| 367 | |
Simon Glass | f14f393 | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 368 | err: |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 369 | free(filt); |
Simon Glass | f14f393 | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 370 | return ret; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | int log_remove_filter(const char *drv_name, int filter_num) |
| 374 | { |
| 375 | struct log_filter *filt; |
| 376 | struct log_device *ldev; |
| 377 | |
| 378 | ldev = log_device_find_by_name(drv_name); |
| 379 | if (!ldev) |
| 380 | return -ENOENT; |
| 381 | |
| 382 | list_for_each_entry(filt, &ldev->filter_head, sibling_node) { |
| 383 | if (filt->filter_num == filter_num) { |
| 384 | list_del(&filt->sibling_node); |
| 385 | free(filt); |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | return -ENOENT; |
| 392 | } |
| 393 | |
Simon Glass | 133350a | 2020-09-12 12:28:49 -0600 | [diff] [blame] | 394 | /** |
| 395 | * log_find_device_by_drv() - Find a device by its driver |
| 396 | * |
| 397 | * @drv: Log driver |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 398 | * Return: Device associated with that driver, or NULL if not found |
Simon Glass | 133350a | 2020-09-12 12:28:49 -0600 | [diff] [blame] | 399 | */ |
| 400 | static struct log_device *log_find_device_by_drv(struct log_driver *drv) |
| 401 | { |
| 402 | struct log_device *ldev; |
| 403 | |
| 404 | list_for_each_entry(ldev, &gd->log_head, sibling_node) { |
| 405 | if (ldev->drv == drv) |
| 406 | return ldev; |
| 407 | } |
| 408 | /* |
| 409 | * It is quite hard to pass an invalid driver since passing an unknown |
| 410 | * LOG_GET_DRIVER(xxx) would normally produce a compilation error. But |
| 411 | * it is possible to pass NULL, for example, so this |
| 412 | */ |
| 413 | |
| 414 | return NULL; |
| 415 | } |
| 416 | |
| 417 | int log_device_set_enable(struct log_driver *drv, bool enable) |
| 418 | { |
| 419 | struct log_device *ldev; |
| 420 | |
| 421 | ldev = log_find_device_by_drv(drv); |
| 422 | if (!ldev) |
| 423 | return -ENOENT; |
| 424 | if (enable) |
| 425 | ldev->flags |= LOGDF_ENABLE; |
| 426 | else |
| 427 | ldev->flags &= ~LOGDF_ENABLE; |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
Thomas Weißschuh | 1bcd488 | 2024-02-13 18:13:28 +0100 | [diff] [blame] | 432 | void log_fixup_for_gd_move(struct global_data *new_gd) |
| 433 | { |
| 434 | new_gd->log_head.prev->next = &new_gd->log_head; |
| 435 | } |
| 436 | |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 437 | int log_init(void) |
| 438 | { |
| 439 | struct log_driver *drv = ll_entry_start(struct log_driver, log_driver); |
| 440 | const int count = ll_entry_count(struct log_driver, log_driver); |
| 441 | struct log_driver *end = drv + count; |
| 442 | |
| 443 | /* |
| 444 | * We cannot add runtime data to the driver since it is likely stored |
| 445 | * in rodata. Instead, set up a 'device' corresponding to each driver. |
Simon Glass | 5cc6ae1 | 2023-07-15 21:38:49 -0600 | [diff] [blame] | 446 | * We only support having a single device for each driver. |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 447 | */ |
| 448 | INIT_LIST_HEAD((struct list_head *)&gd->log_head); |
| 449 | while (drv < end) { |
| 450 | struct log_device *ldev; |
| 451 | |
| 452 | ldev = calloc(1, sizeof(*ldev)); |
| 453 | if (!ldev) { |
| 454 | debug("%s: Cannot allocate memory\n", __func__); |
| 455 | return -ENOMEM; |
| 456 | } |
| 457 | INIT_LIST_HEAD(&ldev->filter_head); |
| 458 | ldev->drv = drv; |
Simon Glass | 885cf5f | 2020-09-12 12:28:47 -0600 | [diff] [blame] | 459 | ldev->flags = drv->flags; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 460 | list_add_tail(&ldev->sibling_node, |
| 461 | (struct list_head *)&gd->log_head); |
| 462 | drv++; |
| 463 | } |
Simon Glass | 55e32ba | 2017-12-04 13:48:28 -0700 | [diff] [blame] | 464 | gd->flags |= GD_FLG_LOG_READY; |
Simon Glass | 4e9a64d | 2018-10-01 11:55:11 -0600 | [diff] [blame] | 465 | if (!gd->default_log_level) |
Simon Glass | 38617e8 | 2019-02-16 20:24:35 -0700 | [diff] [blame] | 466 | gd->default_log_level = CONFIG_LOG_DEFAULT_LEVEL; |
Heinrich Schuchardt | 2d349c7 | 2020-06-17 21:52:44 +0200 | [diff] [blame] | 467 | gd->log_fmt = log_get_default_format(); |
Heinrich Schuchardt | 0fc9f4f | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 468 | gd->logc_prev = LOGC_NONE; |
| 469 | gd->logl_prev = LOGL_INFO; |
Simon Glass | d95645d | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 470 | |
| 471 | return 0; |
| 472 | } |