blob: b7b144fa5d2485d3edadee93f60e3b7afef7970e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd95645d2017-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 Glassd95645d2017-12-04 13:48:24 -07007 */
8
9#include <common.h>
10#include <log.h>
11#include <malloc.h>
Simon Glass4f0fdb72017-12-28 13:14:16 -070012#include <dm/uclass.h>
Simon Glassd95645d2017-12-04 13:48:24 -070013
14DECLARE_GLOBAL_DATA_PTR;
15
Sean Anderson3215fbb2020-10-27 19:55:22 -040016static const char *const log_cat_name[] = {
Simon Glass4f0fdb72017-12-28 13:14:16 -070017 "none",
18 "arch",
19 "board",
20 "core",
21 "driver-model",
22 "device-tree",
Heinrich Schuchardt90979f92018-01-22 20:10:45 +010023 "efi",
Simon Glass31120e12020-09-27 18:46:13 -060024 "alloc",
25 "sandbox",
26 "bloblist",
27 "devres",
28 "acpi",
Simon Glass4f0fdb72017-12-28 13:14:16 -070029};
30
Heinrich Schuchardt7e7041d2020-10-23 13:00:01 +020031_Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE,
32 "log_cat_name size");
33
Sean Anderson3215fbb2020-10-27 19:55:22 -040034static const char *const log_level_name[] = {
Simon Glass4f0fdb72017-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 Schuchardt7e7041d2020-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 Glass4f0fdb72017-12-28 13:14:16 -070050const char *log_get_cat_name(enum log_category_t cat)
51{
Simon Glass89096562018-06-12 00:04:55 -060052 const char *name;
53
54 if (cat < 0 || cat >= LOGC_COUNT)
55 return "<invalid>";
Simon Glass4f0fdb72017-12-28 13:14:16 -070056 if (cat >= LOGC_NONE)
57 return log_cat_name[cat - LOGC_NONE];
58
Heinrich Schuchardt60075ea2020-06-08 18:04:22 +020059#if CONFIG_IS_ENABLED(DM)
Simon Glass89096562018-06-12 00:04:55 -060060 name = uclass_get_name((enum uclass_id)cat);
Heinrich Schuchardt60075ea2020-06-08 18:04:22 +020061#else
62 name = NULL;
63#endif
Simon Glass89096562018-06-12 00:04:55 -060064
65 return name ? name : "<missing>";
Simon Glass4f0fdb72017-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 Andersoncc4960d2020-10-27 19:55:24 -0400102struct log_device *log_device_find_by_name(const char *drv_name)
Simon Glassd95645d2017-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 Andersoncc4960d2020-10-27 19:55:24 -0400114bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
Simon Glassd95645d2017-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 Andersoncc4960d2020-10-27 19:55:24 -0400126bool log_has_file(const char *file_list, const char *file)
Simon Glassd95645d2017-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 Glass9c83cac2020-09-12 11:13:34 -0600154 if (rec->force_debug)
155 return true;
156
Simon Glassd95645d2017-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) {
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 Schuchardt0fc9f4f2020-10-17 14:31:58 +0200183 * filters which block the record.
Simon Glassd95645d2017-12-04 13:48:24 -0700184 *
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200185 * 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 Glassd95645d2017-12-04 13:48:24 -0700189 */
190static int log_dispatch(struct log_rec *rec)
191{
192 struct log_device *ldev;
193
Heinrich Schuchardtb5ecfca2020-09-14 10:01:00 +0200194 /*
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 Schuchardtfdf55992020-10-17 14:31:57 +0200199 if (gd->processing_msg)
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200200 return 1;
Heinrich Schuchardtb5ecfca2020-09-14 10:01:00 +0200201
202 /* Emit message */
Heinrich Schuchardtfdf55992020-10-17 14:31:57 +0200203 gd->processing_msg = true;
Simon Glassd95645d2017-12-04 13:48:24 -0700204 list_for_each_entry(ldev, &gd->log_head, sibling_node) {
Simon Glass885cf5f2020-09-12 12:28:47 -0600205 if ((ldev->flags & LOGDF_ENABLE) &&
206 log_passes_filters(ldev, rec))
Simon Glassd95645d2017-12-04 13:48:24 -0700207 ldev->drv->emit(ldev, rec);
208 }
Heinrich Schuchardtfdf55992020-10-17 14:31:57 +0200209 gd->processing_msg = false;
Simon Glassd95645d2017-12-04 13:48:24 -0700210 return 0;
211}
212
213int _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 Schuchardt0fc9f4f2020-10-17 14:31:58 +0200220 /* 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 Glassd95645d2017-12-04 13:48:24 -0700226 rec.cat = cat;
Simon Glass9c83cac2020-09-12 11:13:34 -0600227 rec.level = level & LOGL_LEVEL_MASK;
228 rec.force_debug = level & LOGL_FORCE_DEBUG;
Simon Glassd95645d2017-12-04 13:48:24 -0700229 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 Schuchardt0fc9f4f2020-10-17 14:31:58 +0200241 if (!log_dispatch(&rec)) {
242 gd->logc_prev = cat;
243 gd->logl_prev = level;
244 }
Simon Glassd95645d2017-12-04 13:48:24 -0700245
246 return 0;
247}
248
Sean Anderson8c410182020-10-27 19:55:25 -0400249int 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 Glassd95645d2017-12-04 13:48:24 -0700252{
253 struct log_filter *filt;
254 struct log_device *ldev;
Simon Glassf14f3932018-04-02 02:42:39 -0600255 int ret;
Simon Glassd95645d2017-12-04 13:48:24 -0700256 int i;
257
258 ldev = log_device_find_by_name(drv_name);
259 if (!ldev)
260 return -ENOENT;
Heinrich Schuchardtbe9ab322020-04-19 10:48:36 +0200261 filt = calloc(1, sizeof(*filt));
Simon Glassd95645d2017-12-04 13:48:24 -0700262 if (!filt)
263 return -ENOMEM;
264
Sean Anderson8c410182020-10-27 19:55:25 -0400265 filt->flags = flags;
Simon Glassd95645d2017-12-04 13:48:24 -0700266 if (cat_list) {
267 filt->flags |= LOGFF_HAS_CAT;
268 for (i = 0; ; i++) {
Simon Glassf14f3932018-04-02 02:42:39 -0600269 if (i == ARRAY_SIZE(filt->cat_list)) {
270 ret = -ENOSPC;
271 goto err;
272 }
Simon Glassd95645d2017-12-04 13:48:24 -0700273 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 Glassf14f3932018-04-02 02:42:39 -0600281 if (!filt->file_list) {
Sean Anderson638b65a2020-10-27 19:55:20 -0400282 ret = -ENOMEM;
Simon Glassf14f3932018-04-02 02:42:39 -0600283 goto err;
284 }
Simon Glassd95645d2017-12-04 13:48:24 -0700285 }
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 Glassf14f3932018-04-02 02:42:39 -0600291err:
Simon Glassd95645d2017-12-04 13:48:24 -0700292 free(filt);
Simon Glassf14f3932018-04-02 02:42:39 -0600293 return ret;
Simon Glassd95645d2017-12-04 13:48:24 -0700294}
295
296int 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 Glass133350a2020-09-12 12:28:49 -0600317/**
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 */
323static 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
340int 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 Glassd95645d2017-12-04 13:48:24 -0700355int 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 Glass885cf5f2020-09-12 12:28:47 -0600377 ldev->flags = drv->flags;
Simon Glassd95645d2017-12-04 13:48:24 -0700378 list_add_tail(&ldev->sibling_node,
379 (struct list_head *)&gd->log_head);
380 drv++;
381 }
Simon Glass55e32ba2017-12-04 13:48:28 -0700382 gd->flags |= GD_FLG_LOG_READY;
Simon Glass4e9a64d2018-10-01 11:55:11 -0600383 if (!gd->default_log_level)
Simon Glass38617e82019-02-16 20:24:35 -0700384 gd->default_log_level = CONFIG_LOG_DEFAULT_LEVEL;
Heinrich Schuchardt2d349c72020-06-17 21:52:44 +0200385 gd->log_fmt = log_get_default_format();
Heinrich Schuchardt0fc9f4f2020-10-17 14:31:58 +0200386 gd->logc_prev = LOGC_NONE;
387 gd->logl_prev = LOGL_INFO;
Simon Glassd95645d2017-12-04 13:48:24 -0700388
389 return 0;
390}