blob: 4b6f3fcd04a57efde2f7ae643c95c4d6a7c89924 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse9c8d492017-12-04 13:48:24 -07002/*
3 * Logging support
4 *
5 * Copyright (c) 2017 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glasse9c8d492017-12-04 13:48:24 -07007 */
8
9#include <common.h>
10#include <log.h>
11#include <malloc.h>
Simon Glassf941c8d2017-12-28 13:14:16 -070012#include <dm/uclass.h>
Simon Glasse9c8d492017-12-04 13:48:24 -070013
14DECLARE_GLOBAL_DATA_PTR;
15
Sean Anderson43381402020-10-27 19:55:22 -040016static const char *const log_cat_name[] = {
Simon Glassf941c8d2017-12-28 13:14:16 -070017 "none",
18 "arch",
19 "board",
20 "core",
21 "driver-model",
22 "device-tree",
Heinrich Schuchardt1973b382018-01-22 20:10:45 +010023 "efi",
Simon Glass80212962020-09-27 18:46:13 -060024 "alloc",
25 "sandbox",
26 "bloblist",
27 "devres",
28 "acpi",
Simon Glassf941c8d2017-12-28 13:14:16 -070029};
30
Heinrich Schuchardt2a0cbf32020-10-23 13:00:01 +020031_Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE,
32 "log_cat_name size");
33
Sean Anderson43381402020-10-27 19:55:22 -040034static const char *const log_level_name[] = {
Simon Glassf941c8d2017-12-28 13:14:16 -070035 "EMERG",
36 "ALERT",
37 "CRIT",
38 "ERR",
39 "WARNING",
40 "NOTICE",
41 "INFO",
42 "DEBUG",
43 "CONTENT",
44 "IO",
45};
46
Heinrich Schuchardt2a0cbf32020-10-23 13:00:01 +020047_Static_assert(ARRAY_SIZE(log_level_name) == LOGL_COUNT, "log_level_name size");
48
49/* All error responses MUST begin with '<' */
Simon Glassf941c8d2017-12-28 13:14:16 -070050const char *log_get_cat_name(enum log_category_t cat)
51{
Simon Glassc2e4e7e2018-06-12 00:04:55 -060052 const char *name;
53
54 if (cat < 0 || cat >= LOGC_COUNT)
55 return "<invalid>";
Simon Glassf941c8d2017-12-28 13:14:16 -070056 if (cat >= LOGC_NONE)
57 return log_cat_name[cat - LOGC_NONE];
58
Heinrich Schuchardt6c9e4172020-06-08 18:04:22 +020059#if CONFIG_IS_ENABLED(DM)
Simon Glassc2e4e7e2018-06-12 00:04:55 -060060 name = uclass_get_name((enum uclass_id)cat);
Heinrich Schuchardt6c9e4172020-06-08 18:04:22 +020061#else
62 name = NULL;
63#endif
Simon Glassc2e4e7e2018-06-12 00:04:55 -060064
65 return name ? name : "<missing>";
Simon Glassf941c8d2017-12-28 13:14:16 -070066}
67
68enum 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
83const 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
90enum 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 Anderson3102c1d2020-10-27 19:55:24 -0400102struct log_device *log_device_find_by_name(const char *drv_name)
Simon Glasse9c8d492017-12-04 13:48:24 -0700103{
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 Anderson3102c1d2020-10-27 19:55:24 -0400114bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
Simon Glasse9c8d492017-12-04 13:48:24 -0700115{
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 Anderson3102c1d2020-10-27 19:55:24 -0400126bool log_has_file(const char *file_list, const char *file)
Simon Glasse9c8d492017-12-04 13:48:24 -0700127{
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 */
150static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec)
151{
152 struct log_filter *filt;
153
Simon Glass52d3df72020-09-12 11:13:34 -0600154 if (rec->force_debug)
155 return true;
156
Simon Glasse9c8d492017-12-04 13:48:24 -0700157 /* 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) {
Sean Anderson40455a62020-10-27 19:55:30 -0400165 if (filt->flags & LOGFF_LEVEL_MIN) {
166 if (rec->level < filt->level)
167 continue;
168 } else if (rec->level > filt->level) {
Simon Glasse9c8d492017-12-04 13:48:24 -0700169 continue;
Sean Anderson40455a62020-10-27 19:55:30 -0400170 }
171
Simon Glasse9c8d492017-12-04 13:48:24 -0700172 if ((filt->flags & LOGFF_HAS_CAT) &&
173 !log_has_cat(filt->cat_list, rec->cat))
174 continue;
Sean Anderson40455a62020-10-27 19:55:30 -0400175
Simon Glasse9c8d492017-12-04 13:48:24 -0700176 if (filt->file_list &&
177 !log_has_file(filt->file_list, rec->file))
178 continue;
Sean Andersonfe3b1a22020-10-27 19:55:26 -0400179
180 if (filt->flags & LOGFF_DENY)
181 return false;
182 else
183 return true;
Simon Glasse9c8d492017-12-04 13:48:24 -0700184 }
185
186 return false;
187}
188
189/**
190 * log_dispatch() - Send a log record to all log devices for processing
191 *
192 * The log record is sent to each log device in turn, skipping those which have
Heinrich Schuchardtd094a072020-10-17 14:31:58 +0200193 * filters which block the record.
Simon Glasse9c8d492017-12-04 13:48:24 -0700194 *
Heinrich Schuchardtd094a072020-10-17 14:31:58 +0200195 * All log messages created while processing log record @rec are ignored.
196 *
197 * @rec: log record to dispatch
198 * Return: 0 msg sent, 1 msg not sent while already dispatching another msg
Simon Glasse9c8d492017-12-04 13:48:24 -0700199 */
200static int log_dispatch(struct log_rec *rec)
201{
202 struct log_device *ldev;
203
Heinrich Schuchardte5b35f72020-09-14 10:01:00 +0200204 /*
205 * When a log driver writes messages (e.g. via the network stack) this
206 * may result in further generated messages. We cannot process them here
207 * as this might result in infinite recursion.
208 */
Heinrich Schuchardt993a06b2020-10-17 14:31:57 +0200209 if (gd->processing_msg)
Heinrich Schuchardtd094a072020-10-17 14:31:58 +0200210 return 1;
Heinrich Schuchardte5b35f72020-09-14 10:01:00 +0200211
212 /* Emit message */
Heinrich Schuchardt993a06b2020-10-17 14:31:57 +0200213 gd->processing_msg = true;
Simon Glasse9c8d492017-12-04 13:48:24 -0700214 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
Simon Glassb4520302020-09-12 12:28:47 -0600215 if ((ldev->flags & LOGDF_ENABLE) &&
216 log_passes_filters(ldev, rec))
Simon Glasse9c8d492017-12-04 13:48:24 -0700217 ldev->drv->emit(ldev, rec);
218 }
Heinrich Schuchardt993a06b2020-10-17 14:31:57 +0200219 gd->processing_msg = false;
Simon Glasse9c8d492017-12-04 13:48:24 -0700220 return 0;
221}
222
223int _log(enum log_category_t cat, enum log_level_t level, const char *file,
224 int line, const char *func, const char *fmt, ...)
225{
226 char buf[CONFIG_SYS_CBSIZE];
227 struct log_rec rec;
228 va_list args;
229
Heinrich Schuchardtd094a072020-10-17 14:31:58 +0200230 /* Check for message continuation */
231 if (cat == LOGC_CONT)
232 cat = gd->logc_prev;
233 if (level == LOGL_CONT)
234 level = gd->logl_prev;
235
Simon Glasse9c8d492017-12-04 13:48:24 -0700236 rec.cat = cat;
Simon Glass52d3df72020-09-12 11:13:34 -0600237 rec.level = level & LOGL_LEVEL_MASK;
238 rec.force_debug = level & LOGL_FORCE_DEBUG;
Simon Glasse9c8d492017-12-04 13:48:24 -0700239 rec.file = file;
240 rec.line = line;
241 rec.func = func;
242 va_start(args, fmt);
243 vsnprintf(buf, sizeof(buf), fmt, args);
244 va_end(args);
245 rec.msg = buf;
246 if (!gd || !(gd->flags & GD_FLG_LOG_READY)) {
247 if (gd)
248 gd->log_drop_count++;
249 return -ENOSYS;
250 }
Heinrich Schuchardtd094a072020-10-17 14:31:58 +0200251 if (!log_dispatch(&rec)) {
252 gd->logc_prev = cat;
253 gd->logl_prev = level;
254 }
Simon Glasse9c8d492017-12-04 13:48:24 -0700255
256 return 0;
257}
258
Sean Andersona02f84e2020-10-27 19:55:25 -0400259int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
Sean Anderson40455a62020-10-27 19:55:30 -0400260 enum log_level_t level, const char *file_list,
Sean Andersona02f84e2020-10-27 19:55:25 -0400261 int flags)
Simon Glasse9c8d492017-12-04 13:48:24 -0700262{
263 struct log_filter *filt;
264 struct log_device *ldev;
Simon Glass45fac9f2018-04-02 02:42:39 -0600265 int ret;
Simon Glasse9c8d492017-12-04 13:48:24 -0700266 int i;
267
268 ldev = log_device_find_by_name(drv_name);
269 if (!ldev)
270 return -ENOENT;
Heinrich Schuchardtc1f39ed2020-04-19 10:48:36 +0200271 filt = calloc(1, sizeof(*filt));
Simon Glasse9c8d492017-12-04 13:48:24 -0700272 if (!filt)
273 return -ENOMEM;
274
Sean Andersona02f84e2020-10-27 19:55:25 -0400275 filt->flags = flags;
Simon Glasse9c8d492017-12-04 13:48:24 -0700276 if (cat_list) {
277 filt->flags |= LOGFF_HAS_CAT;
278 for (i = 0; ; i++) {
Simon Glass45fac9f2018-04-02 02:42:39 -0600279 if (i == ARRAY_SIZE(filt->cat_list)) {
280 ret = -ENOSPC;
281 goto err;
282 }
Simon Glasse9c8d492017-12-04 13:48:24 -0700283 filt->cat_list[i] = cat_list[i];
284 if (cat_list[i] == LOGC_END)
285 break;
286 }
287 }
Sean Anderson40455a62020-10-27 19:55:30 -0400288 filt->level = level;
Simon Glasse9c8d492017-12-04 13:48:24 -0700289 if (file_list) {
290 filt->file_list = strdup(file_list);
Simon Glass45fac9f2018-04-02 02:42:39 -0600291 if (!filt->file_list) {
Sean Anderson69529f92020-10-27 19:55:20 -0400292 ret = -ENOMEM;
Simon Glass45fac9f2018-04-02 02:42:39 -0600293 goto err;
294 }
Simon Glasse9c8d492017-12-04 13:48:24 -0700295 }
296 filt->filter_num = ldev->next_filter_num++;
Sean Andersonfe3b1a22020-10-27 19:55:26 -0400297 /* Add deny filters to the beginning of the list */
298 if (flags & LOGFF_DENY)
299 list_add(&filt->sibling_node, &ldev->filter_head);
300 else
301 list_add_tail(&filt->sibling_node, &ldev->filter_head);
Simon Glasse9c8d492017-12-04 13:48:24 -0700302
303 return filt->filter_num;
304
Simon Glass45fac9f2018-04-02 02:42:39 -0600305err:
Simon Glasse9c8d492017-12-04 13:48:24 -0700306 free(filt);
Simon Glass45fac9f2018-04-02 02:42:39 -0600307 return ret;
Simon Glasse9c8d492017-12-04 13:48:24 -0700308}
309
310int log_remove_filter(const char *drv_name, int filter_num)
311{
312 struct log_filter *filt;
313 struct log_device *ldev;
314
315 ldev = log_device_find_by_name(drv_name);
316 if (!ldev)
317 return -ENOENT;
318
319 list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
320 if (filt->filter_num == filter_num) {
321 list_del(&filt->sibling_node);
322 free(filt);
323
324 return 0;
325 }
326 }
327
328 return -ENOENT;
329}
330
Simon Glass3d03ab62020-09-12 12:28:49 -0600331/**
332 * log_find_device_by_drv() - Find a device by its driver
333 *
334 * @drv: Log driver
335 * @return Device associated with that driver, or NULL if not found
336 */
337static struct log_device *log_find_device_by_drv(struct log_driver *drv)
338{
339 struct log_device *ldev;
340
341 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
342 if (ldev->drv == drv)
343 return ldev;
344 }
345 /*
346 * It is quite hard to pass an invalid driver since passing an unknown
347 * LOG_GET_DRIVER(xxx) would normally produce a compilation error. But
348 * it is possible to pass NULL, for example, so this
349 */
350
351 return NULL;
352}
353
354int log_device_set_enable(struct log_driver *drv, bool enable)
355{
356 struct log_device *ldev;
357
358 ldev = log_find_device_by_drv(drv);
359 if (!ldev)
360 return -ENOENT;
361 if (enable)
362 ldev->flags |= LOGDF_ENABLE;
363 else
364 ldev->flags &= ~LOGDF_ENABLE;
365
366 return 0;
367}
368
Simon Glasse9c8d492017-12-04 13:48:24 -0700369int log_init(void)
370{
371 struct log_driver *drv = ll_entry_start(struct log_driver, log_driver);
372 const int count = ll_entry_count(struct log_driver, log_driver);
373 struct log_driver *end = drv + count;
374
375 /*
376 * We cannot add runtime data to the driver since it is likely stored
377 * in rodata. Instead, set up a 'device' corresponding to each driver.
378 * We only support having a single device.
379 */
380 INIT_LIST_HEAD((struct list_head *)&gd->log_head);
381 while (drv < end) {
382 struct log_device *ldev;
383
384 ldev = calloc(1, sizeof(*ldev));
385 if (!ldev) {
386 debug("%s: Cannot allocate memory\n", __func__);
387 return -ENOMEM;
388 }
389 INIT_LIST_HEAD(&ldev->filter_head);
390 ldev->drv = drv;
Simon Glassb4520302020-09-12 12:28:47 -0600391 ldev->flags = drv->flags;
Simon Glasse9c8d492017-12-04 13:48:24 -0700392 list_add_tail(&ldev->sibling_node,
393 (struct list_head *)&gd->log_head);
394 drv++;
395 }
Simon Glassaf1bc0c2017-12-04 13:48:28 -0700396 gd->flags |= GD_FLG_LOG_READY;
Simon Glass2b1dc292018-10-01 11:55:11 -0600397 if (!gd->default_log_level)
Simon Glassf0b05c92019-02-16 20:24:35 -0700398 gd->default_log_level = CONFIG_LOG_DEFAULT_LEVEL;
Heinrich Schuchardt3c21d772020-06-17 21:52:44 +0200399 gd->log_fmt = log_get_default_format();
Heinrich Schuchardtd094a072020-10-17 14:31:58 +0200400 gd->logc_prev = LOGC_NONE;
401 gd->logl_prev = LOGL_INFO;
Simon Glasse9c8d492017-12-04 13:48:24 -0700402
403 return 0;
404}