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 | |
| 9 | #include <common.h> |
| 10 | #include <log.h> |
| 11 | #include <malloc.h> |
Simon Glass | f941c8d | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 12 | #include <dm/uclass.h> |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Sean Anderson | 4338140 | 2020-10-27 19:55:22 -0400 | [diff] [blame] | 16 | static const char *const log_cat_name[] = { |
Simon Glass | f941c8d | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 17 | "none", |
| 18 | "arch", |
| 19 | "board", |
| 20 | "core", |
| 21 | "driver-model", |
| 22 | "device-tree", |
Heinrich Schuchardt | 1973b38 | 2018-01-22 20:10:45 +0100 | [diff] [blame] | 23 | "efi", |
Simon Glass | 8021296 | 2020-09-27 18:46:13 -0600 | [diff] [blame] | 24 | "alloc", |
| 25 | "sandbox", |
| 26 | "bloblist", |
| 27 | "devres", |
| 28 | "acpi", |
Simon Glass | f941c8d | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
Heinrich Schuchardt | 2a0cbf3 | 2020-10-23 13:00:01 +0200 | [diff] [blame] | 31 | _Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE, |
| 32 | "log_cat_name size"); |
| 33 | |
Sean Anderson | 4338140 | 2020-10-27 19:55:22 -0400 | [diff] [blame] | 34 | static const char *const log_level_name[] = { |
Simon Glass | f941c8d | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 35 | "EMERG", |
| 36 | "ALERT", |
| 37 | "CRIT", |
| 38 | "ERR", |
| 39 | "WARNING", |
| 40 | "NOTICE", |
| 41 | "INFO", |
| 42 | "DEBUG", |
| 43 | "CONTENT", |
| 44 | "IO", |
| 45 | }; |
| 46 | |
Heinrich Schuchardt | 2a0cbf3 | 2020-10-23 13:00:01 +0200 | [diff] [blame] | 47 | _Static_assert(ARRAY_SIZE(log_level_name) == LOGL_COUNT, "log_level_name size"); |
| 48 | |
| 49 | /* All error responses MUST begin with '<' */ |
Simon Glass | f941c8d | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 50 | const char *log_get_cat_name(enum log_category_t cat) |
| 51 | { |
Simon Glass | c2e4e7e | 2018-06-12 00:04:55 -0600 | [diff] [blame] | 52 | const char *name; |
| 53 | |
| 54 | if (cat < 0 || cat >= LOGC_COUNT) |
| 55 | return "<invalid>"; |
Simon Glass | f941c8d | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 56 | if (cat >= LOGC_NONE) |
| 57 | return log_cat_name[cat - LOGC_NONE]; |
| 58 | |
Heinrich Schuchardt | 6c9e417 | 2020-06-08 18:04:22 +0200 | [diff] [blame] | 59 | #if CONFIG_IS_ENABLED(DM) |
Simon Glass | c2e4e7e | 2018-06-12 00:04:55 -0600 | [diff] [blame] | 60 | name = uclass_get_name((enum uclass_id)cat); |
Heinrich Schuchardt | 6c9e417 | 2020-06-08 18:04:22 +0200 | [diff] [blame] | 61 | #else |
| 62 | name = NULL; |
| 63 | #endif |
Simon Glass | c2e4e7e | 2018-06-12 00:04:55 -0600 | [diff] [blame] | 64 | |
| 65 | return name ? name : "<missing>"; |
Simon Glass | f941c8d | 2017-12-28 13:14:16 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | enum log_category_t log_get_cat_by_name(const char *name) |
| 69 | { |
| 70 | enum uclass_id id; |
| 71 | int i; |
| 72 | |
| 73 | for (i = LOGC_NONE; i < LOGC_COUNT; i++) |
| 74 | if (!strcmp(name, log_cat_name[i - LOGC_NONE])) |
| 75 | return i; |
| 76 | id = uclass_get_by_name(name); |
| 77 | if (id != UCLASS_INVALID) |
| 78 | return (enum log_category_t)id; |
| 79 | |
| 80 | return LOGC_NONE; |
| 81 | } |
| 82 | |
| 83 | const char *log_get_level_name(enum log_level_t level) |
| 84 | { |
| 85 | if (level >= LOGL_COUNT) |
| 86 | return "INVALID"; |
| 87 | return log_level_name[level]; |
| 88 | } |
| 89 | |
| 90 | enum log_level_t log_get_level_by_name(const char *name) |
| 91 | { |
| 92 | int i; |
| 93 | |
| 94 | for (i = 0; i < LOGL_COUNT; i++) { |
| 95 | if (!strcasecmp(log_level_name[i], name)) |
| 96 | return i; |
| 97 | } |
| 98 | |
| 99 | return LOGL_NONE; |
| 100 | } |
| 101 | |
Sean Anderson | 3102c1d | 2020-10-27 19:55:24 -0400 | [diff] [blame] | 102 | struct log_device *log_device_find_by_name(const char *drv_name) |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 103 | { |
| 104 | struct log_device *ldev; |
| 105 | |
| 106 | list_for_each_entry(ldev, &gd->log_head, sibling_node) { |
| 107 | if (!strcmp(drv_name, ldev->drv->name)) |
| 108 | return ldev; |
| 109 | } |
| 110 | |
| 111 | return NULL; |
| 112 | } |
| 113 | |
Sean Anderson | 3102c1d | 2020-10-27 19:55:24 -0400 | [diff] [blame] | 114 | 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] | 115 | { |
| 116 | int i; |
| 117 | |
| 118 | for (i = 0; i < LOGF_MAX_CATEGORIES && cat_list[i] != LOGC_END; i++) { |
| 119 | if (cat_list[i] == cat) |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | return false; |
| 124 | } |
| 125 | |
Sean Anderson | 3102c1d | 2020-10-27 19:55:24 -0400 | [diff] [blame] | 126 | bool log_has_file(const char *file_list, const char *file) |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 127 | { |
| 128 | int file_len = strlen(file); |
| 129 | const char *s, *p; |
| 130 | int substr_len; |
| 131 | |
| 132 | for (s = file_list; *s; s = p + (*p != '\0')) { |
| 133 | p = strchrnul(s, ','); |
| 134 | substr_len = p - s; |
| 135 | if (file_len >= substr_len && |
| 136 | !strncmp(file + file_len - substr_len, s, substr_len)) |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * log_passes_filters() - check if a log record passes the filters for a device |
| 145 | * |
| 146 | * @ldev: Log device to check |
| 147 | * @rec: Log record to check |
| 148 | * @return true if @rec is not blocked by the filters in @ldev, false if it is |
| 149 | */ |
| 150 | static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec) |
| 151 | { |
| 152 | struct log_filter *filt; |
| 153 | |
Simon Glass | 52d3df7 | 2020-09-12 11:13:34 -0600 | [diff] [blame] | 154 | if (rec->force_debug) |
| 155 | return true; |
| 156 | |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 157 | /* If there are no filters, filter on the default log level */ |
| 158 | if (list_empty(&ldev->filter_head)) { |
| 159 | if (rec->level > gd->default_log_level) |
| 160 | return false; |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | list_for_each_entry(filt, &ldev->filter_head, sibling_node) { |
| 165 | if (rec->level > filt->max_level) |
| 166 | continue; |
| 167 | if ((filt->flags & LOGFF_HAS_CAT) && |
| 168 | !log_has_cat(filt->cat_list, rec->cat)) |
| 169 | continue; |
| 170 | if (filt->file_list && |
| 171 | !log_has_file(filt->file_list, rec->file)) |
| 172 | continue; |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * log_dispatch() - Send a log record to all log devices for processing |
| 181 | * |
| 182 | * 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] | 183 | * filters which block the record. |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 184 | * |
Heinrich Schuchardt | d094a07 | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 185 | * All log messages created while processing log record @rec are ignored. |
| 186 | * |
| 187 | * @rec: log record to dispatch |
| 188 | * 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] | 189 | */ |
| 190 | static int log_dispatch(struct log_rec *rec) |
| 191 | { |
| 192 | struct log_device *ldev; |
| 193 | |
Heinrich Schuchardt | e5b35f7 | 2020-09-14 10:01:00 +0200 | [diff] [blame] | 194 | /* |
| 195 | * When a log driver writes messages (e.g. via the network stack) this |
| 196 | * may result in further generated messages. We cannot process them here |
| 197 | * as this might result in infinite recursion. |
| 198 | */ |
Heinrich Schuchardt | 993a06b | 2020-10-17 14:31:57 +0200 | [diff] [blame] | 199 | if (gd->processing_msg) |
Heinrich Schuchardt | d094a07 | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 200 | return 1; |
Heinrich Schuchardt | e5b35f7 | 2020-09-14 10:01:00 +0200 | [diff] [blame] | 201 | |
| 202 | /* Emit message */ |
Heinrich Schuchardt | 993a06b | 2020-10-17 14:31:57 +0200 | [diff] [blame] | 203 | gd->processing_msg = true; |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 204 | list_for_each_entry(ldev, &gd->log_head, sibling_node) { |
Simon Glass | b452030 | 2020-09-12 12:28:47 -0600 | [diff] [blame] | 205 | if ((ldev->flags & LOGDF_ENABLE) && |
| 206 | log_passes_filters(ldev, rec)) |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 207 | ldev->drv->emit(ldev, rec); |
| 208 | } |
Heinrich Schuchardt | 993a06b | 2020-10-17 14:31:57 +0200 | [diff] [blame] | 209 | gd->processing_msg = false; |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | int _log(enum log_category_t cat, enum log_level_t level, const char *file, |
| 214 | int line, const char *func, const char *fmt, ...) |
| 215 | { |
| 216 | char buf[CONFIG_SYS_CBSIZE]; |
| 217 | struct log_rec rec; |
| 218 | va_list args; |
| 219 | |
Heinrich Schuchardt | d094a07 | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 220 | /* Check for message continuation */ |
| 221 | if (cat == LOGC_CONT) |
| 222 | cat = gd->logc_prev; |
| 223 | if (level == LOGL_CONT) |
| 224 | level = gd->logl_prev; |
| 225 | |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 226 | rec.cat = cat; |
Simon Glass | 52d3df7 | 2020-09-12 11:13:34 -0600 | [diff] [blame] | 227 | rec.level = level & LOGL_LEVEL_MASK; |
| 228 | rec.force_debug = level & LOGL_FORCE_DEBUG; |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 229 | rec.file = file; |
| 230 | rec.line = line; |
| 231 | rec.func = func; |
| 232 | va_start(args, fmt); |
| 233 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 234 | va_end(args); |
| 235 | rec.msg = buf; |
| 236 | if (!gd || !(gd->flags & GD_FLG_LOG_READY)) { |
| 237 | if (gd) |
| 238 | gd->log_drop_count++; |
| 239 | return -ENOSYS; |
| 240 | } |
Heinrich Schuchardt | d094a07 | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 241 | if (!log_dispatch(&rec)) { |
| 242 | gd->logc_prev = cat; |
| 243 | gd->logl_prev = level; |
| 244 | } |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Sean Anderson | a02f84e | 2020-10-27 19:55:25 -0400 | [diff] [blame^] | 249 | int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[], |
| 250 | enum log_level_t max_level, const char *file_list, |
| 251 | int flags) |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 252 | { |
| 253 | struct log_filter *filt; |
| 254 | struct log_device *ldev; |
Simon Glass | 45fac9f | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 255 | int ret; |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 256 | int i; |
| 257 | |
| 258 | ldev = log_device_find_by_name(drv_name); |
| 259 | if (!ldev) |
| 260 | return -ENOENT; |
Heinrich Schuchardt | c1f39ed | 2020-04-19 10:48:36 +0200 | [diff] [blame] | 261 | filt = calloc(1, sizeof(*filt)); |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 262 | if (!filt) |
| 263 | return -ENOMEM; |
| 264 | |
Sean Anderson | a02f84e | 2020-10-27 19:55:25 -0400 | [diff] [blame^] | 265 | filt->flags = flags; |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 266 | if (cat_list) { |
| 267 | filt->flags |= LOGFF_HAS_CAT; |
| 268 | for (i = 0; ; i++) { |
Simon Glass | 45fac9f | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 269 | if (i == ARRAY_SIZE(filt->cat_list)) { |
| 270 | ret = -ENOSPC; |
| 271 | goto err; |
| 272 | } |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 273 | filt->cat_list[i] = cat_list[i]; |
| 274 | if (cat_list[i] == LOGC_END) |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | filt->max_level = max_level; |
| 279 | if (file_list) { |
| 280 | filt->file_list = strdup(file_list); |
Simon Glass | 45fac9f | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 281 | if (!filt->file_list) { |
Sean Anderson | 69529f9 | 2020-10-27 19:55:20 -0400 | [diff] [blame] | 282 | ret = -ENOMEM; |
Simon Glass | 45fac9f | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 283 | goto err; |
| 284 | } |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 285 | } |
| 286 | filt->filter_num = ldev->next_filter_num++; |
| 287 | list_add_tail(&filt->sibling_node, &ldev->filter_head); |
| 288 | |
| 289 | return filt->filter_num; |
| 290 | |
Simon Glass | 45fac9f | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 291 | err: |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 292 | free(filt); |
Simon Glass | 45fac9f | 2018-04-02 02:42:39 -0600 | [diff] [blame] | 293 | return ret; |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | int log_remove_filter(const char *drv_name, int filter_num) |
| 297 | { |
| 298 | struct log_filter *filt; |
| 299 | struct log_device *ldev; |
| 300 | |
| 301 | ldev = log_device_find_by_name(drv_name); |
| 302 | if (!ldev) |
| 303 | return -ENOENT; |
| 304 | |
| 305 | list_for_each_entry(filt, &ldev->filter_head, sibling_node) { |
| 306 | if (filt->filter_num == filter_num) { |
| 307 | list_del(&filt->sibling_node); |
| 308 | free(filt); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return -ENOENT; |
| 315 | } |
| 316 | |
Simon Glass | 3d03ab6 | 2020-09-12 12:28:49 -0600 | [diff] [blame] | 317 | /** |
| 318 | * log_find_device_by_drv() - Find a device by its driver |
| 319 | * |
| 320 | * @drv: Log driver |
| 321 | * @return Device associated with that driver, or NULL if not found |
| 322 | */ |
| 323 | static struct log_device *log_find_device_by_drv(struct log_driver *drv) |
| 324 | { |
| 325 | struct log_device *ldev; |
| 326 | |
| 327 | list_for_each_entry(ldev, &gd->log_head, sibling_node) { |
| 328 | if (ldev->drv == drv) |
| 329 | return ldev; |
| 330 | } |
| 331 | /* |
| 332 | * It is quite hard to pass an invalid driver since passing an unknown |
| 333 | * LOG_GET_DRIVER(xxx) would normally produce a compilation error. But |
| 334 | * it is possible to pass NULL, for example, so this |
| 335 | */ |
| 336 | |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | int log_device_set_enable(struct log_driver *drv, bool enable) |
| 341 | { |
| 342 | struct log_device *ldev; |
| 343 | |
| 344 | ldev = log_find_device_by_drv(drv); |
| 345 | if (!ldev) |
| 346 | return -ENOENT; |
| 347 | if (enable) |
| 348 | ldev->flags |= LOGDF_ENABLE; |
| 349 | else |
| 350 | ldev->flags &= ~LOGDF_ENABLE; |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 355 | int log_init(void) |
| 356 | { |
| 357 | struct log_driver *drv = ll_entry_start(struct log_driver, log_driver); |
| 358 | const int count = ll_entry_count(struct log_driver, log_driver); |
| 359 | struct log_driver *end = drv + count; |
| 360 | |
| 361 | /* |
| 362 | * We cannot add runtime data to the driver since it is likely stored |
| 363 | * in rodata. Instead, set up a 'device' corresponding to each driver. |
| 364 | * We only support having a single device. |
| 365 | */ |
| 366 | INIT_LIST_HEAD((struct list_head *)&gd->log_head); |
| 367 | while (drv < end) { |
| 368 | struct log_device *ldev; |
| 369 | |
| 370 | ldev = calloc(1, sizeof(*ldev)); |
| 371 | if (!ldev) { |
| 372 | debug("%s: Cannot allocate memory\n", __func__); |
| 373 | return -ENOMEM; |
| 374 | } |
| 375 | INIT_LIST_HEAD(&ldev->filter_head); |
| 376 | ldev->drv = drv; |
Simon Glass | b452030 | 2020-09-12 12:28:47 -0600 | [diff] [blame] | 377 | ldev->flags = drv->flags; |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 378 | list_add_tail(&ldev->sibling_node, |
| 379 | (struct list_head *)&gd->log_head); |
| 380 | drv++; |
| 381 | } |
Simon Glass | af1bc0c | 2017-12-04 13:48:28 -0700 | [diff] [blame] | 382 | gd->flags |= GD_FLG_LOG_READY; |
Simon Glass | 2b1dc29 | 2018-10-01 11:55:11 -0600 | [diff] [blame] | 383 | if (!gd->default_log_level) |
Simon Glass | f0b05c9 | 2019-02-16 20:24:35 -0700 | [diff] [blame] | 384 | gd->default_log_level = CONFIG_LOG_DEFAULT_LEVEL; |
Heinrich Schuchardt | 3c21d77 | 2020-06-17 21:52:44 +0200 | [diff] [blame] | 385 | gd->log_fmt = log_get_default_format(); |
Heinrich Schuchardt | d094a07 | 2020-10-17 14:31:58 +0200 | [diff] [blame] | 386 | gd->logc_prev = LOGC_NONE; |
| 387 | gd->logl_prev = LOGL_INFO; |
Simon Glass | e9c8d49 | 2017-12-04 13:48:24 -0700 | [diff] [blame] | 388 | |
| 389 | return 0; |
| 390 | } |