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