1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2017 Google, Inc
9 #include <display_options.h>
12 #include <asm/global_data.h>
13 #include <dm/uclass.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 static const char *const log_cat_name[] = {
38 _Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE,
41 static const char *const log_level_name[] = {
54 _Static_assert(ARRAY_SIZE(log_level_name) == LOGL_COUNT, "log_level_name size");
56 /* All error responses MUST begin with '<' */
57 const char *log_get_cat_name(enum log_category_t cat)
61 if (cat < 0 || cat >= LOGC_COUNT)
64 return log_cat_name[cat - LOGC_NONE];
66 #if CONFIG_IS_ENABLED(DM)
67 name = uclass_get_name((enum uclass_id)cat);
72 return name ? name : "<missing>";
75 enum log_category_t log_get_cat_by_name(const char *name)
80 for (i = LOGC_NONE; i < LOGC_COUNT; i++)
81 if (!strcmp(name, log_cat_name[i - LOGC_NONE]))
83 id = uclass_get_by_name(name);
84 if (id != UCLASS_INVALID)
85 return (enum log_category_t)id;
90 const char *log_get_level_name(enum log_level_t level)
92 if (level >= LOGL_COUNT)
94 return log_level_name[level];
97 enum log_level_t log_get_level_by_name(const char *name)
101 for (i = 0; i < LOGL_COUNT; i++) {
102 if (!strcasecmp(log_level_name[i], name))
109 struct log_device *log_device_find_by_name(const char *drv_name)
111 struct log_device *ldev;
113 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
114 if (!strcmp(drv_name, ldev->drv->name))
121 bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
125 for (i = 0; i < LOGF_MAX_CATEGORIES && cat_list[i] != LOGC_END; i++) {
126 if (cat_list[i] == cat)
133 bool log_has_file(const char *file_list, const char *file)
135 int file_len = strlen(file);
139 for (s = file_list; *s; s = p + (*p != '\0')) {
140 p = strchrnul(s, ',');
142 if (file_len >= substr_len &&
143 !strncmp(file + file_len - substr_len, s, substr_len))
151 * log_passes_filters() - check if a log record passes the filters for a device
153 * @ldev: Log device to check
154 * @rec: Log record to check
155 * Return: true if @rec is not blocked by the filters in @ldev, false if it is
157 static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec)
159 struct log_filter *filt;
161 if (rec->flags & LOGRECF_FORCE_DEBUG)
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)
171 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
172 if (filt->flags & LOGFF_LEVEL_MIN) {
173 if (rec->level < filt->level)
175 } else if (rec->level > filt->level) {
179 if ((filt->flags & LOGFF_HAS_CAT) &&
180 !log_has_cat(filt->cat_list, rec->cat))
183 if (filt->file_list &&
184 !log_has_file(filt->file_list, rec->file))
187 if (filt->flags & LOGFF_DENY)
197 * log_dispatch() - Send a log record to all log devices for processing
199 * The log record is sent to each log device in turn, skipping those which have
200 * filters which block the record.
202 * All log messages created while processing log record @rec are ignored.
204 * @rec: log record to dispatch
205 * Return: 0 msg sent, 1 msg not sent while already dispatching another msg
207 static int log_dispatch(struct log_rec *rec, const char *fmt, va_list args)
209 struct log_device *ldev;
210 char buf[CONFIG_SYS_CBSIZE];
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.
217 if (gd->processing_msg)
221 gd->processing_msg = true;
222 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
223 if ((ldev->flags & LOGDF_ENABLE) &&
224 log_passes_filters(ldev, rec)) {
228 len = vsnprintf(buf, sizeof(buf), fmt, args);
230 gd->log_cont = len && buf[len - 1] != '\n';
232 ldev->drv->emit(ldev, rec);
235 gd->processing_msg = false;
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, ...)
248 /* Check for message continuation */
249 if (cat == LOGC_CONT)
251 if (level == LOGL_CONT)
252 level = gd->logl_prev;
255 rec.level = level & LOGL_LEVEL_MASK;
257 if (level & LOGL_FORCE_DEBUG)
258 rec.flags |= LOGRECF_FORCE_DEBUG;
260 rec.flags |= LOGRECF_CONT;
266 if (!(gd->flags & GD_FLG_LOG_READY)) {
267 gd->log_drop_count++;
269 /* display dropped traces with console puts and DEBUG_UART */
270 if (rec.level <= CONFIG_LOG_DEFAULT_LEVEL ||
271 rec.flags & LOGRECF_FORCE_DEBUG) {
272 char buf[CONFIG_SYS_CBSIZE];
275 vsnprintf(buf, sizeof(buf), fmt, args);
283 if (!log_dispatch(&rec, fmt, args)) {
285 gd->logl_prev = level;
292 #define MAX_LINE_LENGTH_BYTES 64
293 #define DEFAULT_LINE_LENGTH_BYTES 16
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)
299 if (linelen * width > MAX_LINE_LENGTH_BYTES)
300 linelen = MAX_LINE_LENGTH_BYTES / width;
302 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
306 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)];
308 thislinelen = hexdump_line(addr, data, width, count, linelen,
310 assert(thislinelen >= 0);
311 _log(cat, level, file, line, func, "%s\n", buf);
313 /* update references */
314 data += thislinelen * width;
315 addr += thislinelen * width;
316 count -= thislinelen;
322 int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
323 enum log_level_t level, const char *file_list,
326 struct log_filter *filt;
327 struct log_device *ldev;
331 ldev = log_device_find_by_name(drv_name);
334 filt = calloc(1, sizeof(*filt));
340 filt->flags |= LOGFF_HAS_CAT;
342 if (i == ARRAY_SIZE(filt->cat_list)) {
346 filt->cat_list[i] = cat_list[i];
347 if (cat_list[i] == LOGC_END)
353 filt->file_list = strdup(file_list);
354 if (!filt->file_list) {
359 filt->filter_num = ldev->next_filter_num++;
360 /* Add deny filters to the beginning of the list */
361 if (flags & LOGFF_DENY)
362 list_add(&filt->sibling_node, &ldev->filter_head);
364 list_add_tail(&filt->sibling_node, &ldev->filter_head);
366 return filt->filter_num;
373 int log_remove_filter(const char *drv_name, int filter_num)
375 struct log_filter *filt;
376 struct log_device *ldev;
378 ldev = log_device_find_by_name(drv_name);
382 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
383 if (filt->filter_num == filter_num) {
384 list_del(&filt->sibling_node);
395 * log_find_device_by_drv() - Find a device by its driver
398 * Return: Device associated with that driver, or NULL if not found
400 static struct log_device *log_find_device_by_drv(struct log_driver *drv)
402 struct log_device *ldev;
404 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
405 if (ldev->drv == drv)
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
417 int log_device_set_enable(struct log_driver *drv, bool enable)
419 struct log_device *ldev;
421 ldev = log_find_device_by_drv(drv);
425 ldev->flags |= LOGDF_ENABLE;
427 ldev->flags &= ~LOGDF_ENABLE;
432 void log_fixup_for_gd_move(struct global_data *new_gd)
434 new_gd->log_head.prev->next = &new_gd->log_head;
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;
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.
446 * We only support having a single device for each driver.
448 INIT_LIST_HEAD((struct list_head *)&gd->log_head);
450 struct log_device *ldev;
452 ldev = calloc(1, sizeof(*ldev));
454 debug("%s: Cannot allocate memory\n", __func__);
457 INIT_LIST_HEAD(&ldev->filter_head);
459 ldev->flags = drv->flags;
460 list_add_tail(&ldev->sibling_node,
461 (struct list_head *)&gd->log_head);
464 gd->flags |= GD_FLG_LOG_READY;
465 if (!gd->default_log_level)
466 gd->default_log_level = CONFIG_LOG_DEFAULT_LEVEL;
467 gd->log_fmt = log_get_default_format();
468 gd->logc_prev = LOGC_NONE;
469 gd->logl_prev = LOGL_INFO;