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