1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
10 #include <display_options.h>
13 #include <asm/global_data.h>
14 #include <dm/uclass.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static const char *const log_cat_name[] = {
35 _Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE,
38 static const char *const log_level_name[] = {
51 _Static_assert(ARRAY_SIZE(log_level_name) == LOGL_COUNT, "log_level_name size");
53 /* All error responses MUST begin with '<' */
54 const char *log_get_cat_name(enum log_category_t cat)
58 if (cat < 0 || cat >= LOGC_COUNT)
61 return log_cat_name[cat - LOGC_NONE];
63 #if CONFIG_IS_ENABLED(DM)
64 name = uclass_get_name((enum uclass_id)cat);
69 return name ? name : "<missing>";
72 enum log_category_t log_get_cat_by_name(const char *name)
77 for (i = LOGC_NONE; i < LOGC_COUNT; i++)
78 if (!strcmp(name, log_cat_name[i - LOGC_NONE]))
80 id = uclass_get_by_name(name);
81 if (id != UCLASS_INVALID)
82 return (enum log_category_t)id;
87 const char *log_get_level_name(enum log_level_t level)
89 if (level >= LOGL_COUNT)
91 return log_level_name[level];
94 enum log_level_t log_get_level_by_name(const char *name)
98 for (i = 0; i < LOGL_COUNT; i++) {
99 if (!strcasecmp(log_level_name[i], name))
106 struct log_device *log_device_find_by_name(const char *drv_name)
108 struct log_device *ldev;
110 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
111 if (!strcmp(drv_name, ldev->drv->name))
118 bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
122 for (i = 0; i < LOGF_MAX_CATEGORIES && cat_list[i] != LOGC_END; i++) {
123 if (cat_list[i] == cat)
130 bool log_has_file(const char *file_list, const char *file)
132 int file_len = strlen(file);
136 for (s = file_list; *s; s = p + (*p != '\0')) {
137 p = strchrnul(s, ',');
139 if (file_len >= substr_len &&
140 !strncmp(file + file_len - substr_len, s, substr_len))
148 * log_passes_filters() - check if a log record passes the filters for a device
150 * @ldev: Log device to check
151 * @rec: Log record to check
152 * Return: true if @rec is not blocked by the filters in @ldev, false if it is
154 static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec)
156 struct log_filter *filt;
158 if (rec->flags & LOGRECF_FORCE_DEBUG)
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)
168 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
169 if (filt->flags & LOGFF_LEVEL_MIN) {
170 if (rec->level < filt->level)
172 } else if (rec->level > filt->level) {
176 if ((filt->flags & LOGFF_HAS_CAT) &&
177 !log_has_cat(filt->cat_list, rec->cat))
180 if (filt->file_list &&
181 !log_has_file(filt->file_list, rec->file))
184 if (filt->flags & LOGFF_DENY)
194 * log_dispatch() - Send a log record to all log devices for processing
196 * The log record is sent to each log device in turn, skipping those which have
197 * filters which block the record.
199 * All log messages created while processing log record @rec are ignored.
201 * @rec: log record to dispatch
202 * Return: 0 msg sent, 1 msg not sent while already dispatching another msg
204 static int log_dispatch(struct log_rec *rec, const char *fmt, va_list args)
206 struct log_device *ldev;
207 char buf[CONFIG_SYS_CBSIZE];
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.
214 if (gd->processing_msg)
218 gd->processing_msg = true;
219 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
220 if ((ldev->flags & LOGDF_ENABLE) &&
221 log_passes_filters(ldev, rec)) {
225 len = vsnprintf(buf, sizeof(buf), fmt, args);
227 gd->log_cont = len && buf[len - 1] != '\n';
229 ldev->drv->emit(ldev, rec);
232 gd->processing_msg = false;
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, ...)
245 /* Check for message continuation */
246 if (cat == LOGC_CONT)
248 if (level == LOGL_CONT)
249 level = gd->logl_prev;
252 rec.level = level & LOGL_LEVEL_MASK;
254 if (level & LOGL_FORCE_DEBUG)
255 rec.flags |= LOGRECF_FORCE_DEBUG;
257 rec.flags |= LOGRECF_CONT;
263 if (!(gd->flags & GD_FLG_LOG_READY)) {
264 gd->log_drop_count++;
266 /* display dropped traces with console puts and DEBUG_UART */
267 if (rec.level <= CONFIG_LOG_DEFAULT_LEVEL ||
268 rec.flags & LOGRECF_FORCE_DEBUG) {
269 char buf[CONFIG_SYS_CBSIZE];
272 vsnprintf(buf, sizeof(buf), fmt, args);
280 if (!log_dispatch(&rec, fmt, args)) {
282 gd->logl_prev = level;
289 #define MAX_LINE_LENGTH_BYTES 64
290 #define DEFAULT_LINE_LENGTH_BYTES 16
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)
296 if (linelen * width > MAX_LINE_LENGTH_BYTES)
297 linelen = MAX_LINE_LENGTH_BYTES / width;
299 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
303 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)];
305 thislinelen = hexdump_line(addr, data, width, count, linelen,
307 assert(thislinelen >= 0);
308 _log(cat, level, file, line, func, "%s\n", buf);
310 /* update references */
311 data += thislinelen * width;
312 addr += thislinelen * width;
313 count -= thislinelen;
319 int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
320 enum log_level_t level, const char *file_list,
323 struct log_filter *filt;
324 struct log_device *ldev;
328 ldev = log_device_find_by_name(drv_name);
331 filt = calloc(1, sizeof(*filt));
337 filt->flags |= LOGFF_HAS_CAT;
339 if (i == ARRAY_SIZE(filt->cat_list)) {
343 filt->cat_list[i] = cat_list[i];
344 if (cat_list[i] == LOGC_END)
350 filt->file_list = strdup(file_list);
351 if (!filt->file_list) {
356 filt->filter_num = ldev->next_filter_num++;
357 /* Add deny filters to the beginning of the list */
358 if (flags & LOGFF_DENY)
359 list_add(&filt->sibling_node, &ldev->filter_head);
361 list_add_tail(&filt->sibling_node, &ldev->filter_head);
363 return filt->filter_num;
370 int log_remove_filter(const char *drv_name, int filter_num)
372 struct log_filter *filt;
373 struct log_device *ldev;
375 ldev = log_device_find_by_name(drv_name);
379 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
380 if (filt->filter_num == filter_num) {
381 list_del(&filt->sibling_node);
392 * log_find_device_by_drv() - Find a device by its driver
395 * Return: Device associated with that driver, or NULL if not found
397 static struct log_device *log_find_device_by_drv(struct log_driver *drv)
399 struct log_device *ldev;
401 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
402 if (ldev->drv == drv)
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
414 int log_device_set_enable(struct log_driver *drv, bool enable)
416 struct log_device *ldev;
418 ldev = log_find_device_by_drv(drv);
422 ldev->flags |= LOGDF_ENABLE;
424 ldev->flags &= ~LOGDF_ENABLE;
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;
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.
440 INIT_LIST_HEAD((struct list_head *)&gd->log_head);
442 struct log_device *ldev;
444 ldev = calloc(1, sizeof(*ldev));
446 debug("%s: Cannot allocate memory\n", __func__);
449 INIT_LIST_HEAD(&ldev->filter_head);
451 ldev->flags = drv->flags;
452 list_add_tail(&ldev->sibling_node,
453 (struct list_head *)&gd->log_head);
456 gd->flags |= GD_FLG_LOG_READY;
457 if (!gd->default_log_level)
458 gd->default_log_level = CONFIG_LOG_DEFAULT_LEVEL;
459 gd->log_fmt = log_get_default_format();
460 gd->logc_prev = LOGC_NONE;
461 gd->logl_prev = LOGL_INFO;