xref: /openbmc/u-boot/common/log.c (revision 95963679)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Logging support
4  *
5  * Copyright (c) 2017 Google, Inc
6  * Written by Simon Glass <sjg@chromium.org>
7  */
8 
9 #include <common.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <dm/uclass.h>
13 
14 DECLARE_GLOBAL_DATA_PTR;
15 
16 static const char *log_cat_name[LOGC_COUNT - LOGC_NONE] = {
17 	"none",
18 	"arch",
19 	"board",
20 	"core",
21 	"driver-model",
22 	"device-tree",
23 	"efi",
24 };
25 
26 static const char *log_level_name[LOGL_COUNT] = {
27 	"EMERG",
28 	"ALERT",
29 	"CRIT",
30 	"ERR",
31 	"WARNING",
32 	"NOTICE",
33 	"INFO",
34 	"DEBUG",
35 	"CONTENT",
36 	"IO",
37 };
38 
39 const char *log_get_cat_name(enum log_category_t cat)
40 {
41 	const char *name;
42 
43 	if (cat < 0 || cat >= LOGC_COUNT)
44 		return "<invalid>";
45 	if (cat >= LOGC_NONE)
46 		return log_cat_name[cat - LOGC_NONE];
47 
48 	name = uclass_get_name((enum uclass_id)cat);
49 
50 	return name ? name : "<missing>";
51 }
52 
53 enum log_category_t log_get_cat_by_name(const char *name)
54 {
55 	enum uclass_id id;
56 	int i;
57 
58 	for (i = LOGC_NONE; i < LOGC_COUNT; i++)
59 		if (!strcmp(name, log_cat_name[i - LOGC_NONE]))
60 			return i;
61 	id = uclass_get_by_name(name);
62 	if (id != UCLASS_INVALID)
63 		return (enum log_category_t)id;
64 
65 	return LOGC_NONE;
66 }
67 
68 const char *log_get_level_name(enum log_level_t level)
69 {
70 	if (level >= LOGL_COUNT)
71 		return "INVALID";
72 	return log_level_name[level];
73 }
74 
75 enum log_level_t log_get_level_by_name(const char *name)
76 {
77 	int i;
78 
79 	for (i = 0; i < LOGL_COUNT; i++) {
80 		if (!strcasecmp(log_level_name[i], name))
81 			return i;
82 	}
83 
84 	return LOGL_NONE;
85 }
86 
87 static struct log_device *log_device_find_by_name(const char *drv_name)
88 {
89 	struct log_device *ldev;
90 
91 	list_for_each_entry(ldev, &gd->log_head, sibling_node) {
92 		if (!strcmp(drv_name, ldev->drv->name))
93 			return ldev;
94 	}
95 
96 	return NULL;
97 }
98 
99 /**
100  * log_has_cat() - check if a log category exists within a list
101  *
102  * @cat_list: List of categories to check, at most LOGF_MAX_CATEGORIES entries
103  *	long, terminated by LC_END if fewer
104  * @cat: Category to search for
105  * @return true if @cat is in @cat_list, else false
106  */
107 static bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
108 {
109 	int i;
110 
111 	for (i = 0; i < LOGF_MAX_CATEGORIES && cat_list[i] != LOGC_END; i++) {
112 		if (cat_list[i] == cat)
113 			return true;
114 	}
115 
116 	return false;
117 }
118 
119 /**
120  * log_has_file() - check if a file is with a list
121  *
122  * @file_list: List of files to check, separated by comma
123  * @file: File to check for. This string is matched against the end of each
124  *	file in the list, i.e. ignoring any preceding path. The list is
125  *	intended to consist of relative pathnames, e.g. common/main.c,cmd/log.c
126  * @return true if @file is in @file_list, else false
127  */
128 static bool log_has_file(const char *file_list, const char *file)
129 {
130 	int file_len = strlen(file);
131 	const char *s, *p;
132 	int substr_len;
133 
134 	for (s = file_list; *s; s = p + (*p != '\0')) {
135 		p = strchrnul(s, ',');
136 		substr_len = p - s;
137 		if (file_len >= substr_len &&
138 		    !strncmp(file + file_len - substr_len, s, substr_len))
139 			return true;
140 	}
141 
142 	return false;
143 }
144 
145 /**
146  * log_passes_filters() - check if a log record passes the filters for a device
147  *
148  * @ldev: Log device to check
149  * @rec: Log record to check
150  * @return true if @rec is not blocked by the filters in @ldev, false if it is
151  */
152 static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec)
153 {
154 	struct log_filter *filt;
155 
156 	/* If there are no filters, filter on the default log level */
157 	if (list_empty(&ldev->filter_head)) {
158 		if (rec->level > gd->default_log_level)
159 			return false;
160 		return true;
161 	}
162 
163 	list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
164 		if (rec->level > filt->max_level)
165 			continue;
166 		if ((filt->flags & LOGFF_HAS_CAT) &&
167 		    !log_has_cat(filt->cat_list, rec->cat))
168 			continue;
169 		if (filt->file_list &&
170 		    !log_has_file(filt->file_list, rec->file))
171 			continue;
172 		return true;
173 	}
174 
175 	return false;
176 }
177 
178 /**
179  * log_dispatch() - Send a log record to all log devices for processing
180  *
181  * The log record is sent to each log device in turn, skipping those which have
182  * filters which block the record
183  *
184  * @rec: Log record to dispatch
185  * @return 0 (meaning success)
186  */
187 static int log_dispatch(struct log_rec *rec)
188 {
189 	struct log_device *ldev;
190 
191 	list_for_each_entry(ldev, &gd->log_head, sibling_node) {
192 		if (log_passes_filters(ldev, rec))
193 			ldev->drv->emit(ldev, rec);
194 	}
195 
196 	return 0;
197 }
198 
199 int _log(enum log_category_t cat, enum log_level_t level, const char *file,
200 	 int line, const char *func, const char *fmt, ...)
201 {
202 	char buf[CONFIG_SYS_CBSIZE];
203 	struct log_rec rec;
204 	va_list args;
205 
206 	rec.cat = cat;
207 	rec.level = level;
208 	rec.file = file;
209 	rec.line = line;
210 	rec.func = func;
211 	va_start(args, fmt);
212 	vsnprintf(buf, sizeof(buf), fmt, args);
213 	va_end(args);
214 	rec.msg = buf;
215 	if (!gd || !(gd->flags & GD_FLG_LOG_READY)) {
216 		if (gd)
217 			gd->log_drop_count++;
218 		return -ENOSYS;
219 	}
220 	log_dispatch(&rec);
221 
222 	return 0;
223 }
224 
225 int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
226 		   enum log_level_t max_level, const char *file_list)
227 {
228 	struct log_filter *filt;
229 	struct log_device *ldev;
230 	int ret;
231 	int i;
232 
233 	ldev = log_device_find_by_name(drv_name);
234 	if (!ldev)
235 		return -ENOENT;
236 	filt = (struct log_filter *)calloc(1, sizeof(*filt));
237 	if (!filt)
238 		return -ENOMEM;
239 
240 	if (cat_list) {
241 		filt->flags |= LOGFF_HAS_CAT;
242 		for (i = 0; ; i++) {
243 			if (i == ARRAY_SIZE(filt->cat_list)) {
244 				ret = -ENOSPC;
245 				goto err;
246 			}
247 			filt->cat_list[i] = cat_list[i];
248 			if (cat_list[i] == LOGC_END)
249 				break;
250 		}
251 	}
252 	filt->max_level = max_level;
253 	if (file_list) {
254 		filt->file_list = strdup(file_list);
255 		if (!filt->file_list) {
256 			ret = ENOMEM;
257 			goto err;
258 		}
259 	}
260 	filt->filter_num = ldev->next_filter_num++;
261 	list_add_tail(&filt->sibling_node, &ldev->filter_head);
262 
263 	return filt->filter_num;
264 
265 err:
266 	free(filt);
267 	return ret;
268 }
269 
270 int log_remove_filter(const char *drv_name, int filter_num)
271 {
272 	struct log_filter *filt;
273 	struct log_device *ldev;
274 
275 	ldev = log_device_find_by_name(drv_name);
276 	if (!ldev)
277 		return -ENOENT;
278 
279 	list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
280 		if (filt->filter_num == filter_num) {
281 			list_del(&filt->sibling_node);
282 			free(filt);
283 
284 			return 0;
285 		}
286 	}
287 
288 	return -ENOENT;
289 }
290 
291 int log_init(void)
292 {
293 	struct log_driver *drv = ll_entry_start(struct log_driver, log_driver);
294 	const int count = ll_entry_count(struct log_driver, log_driver);
295 	struct log_driver *end = drv + count;
296 
297 	/*
298 	 * We cannot add runtime data to the driver since it is likely stored
299 	 * in rodata. Instead, set up a 'device' corresponding to each driver.
300 	 * We only support having a single device.
301 	 */
302 	INIT_LIST_HEAD((struct list_head *)&gd->log_head);
303 	while (drv < end) {
304 		struct log_device *ldev;
305 
306 		ldev = calloc(1, sizeof(*ldev));
307 		if (!ldev) {
308 			debug("%s: Cannot allocate memory\n", __func__);
309 			return -ENOMEM;
310 		}
311 		INIT_LIST_HEAD(&ldev->filter_head);
312 		ldev->drv = drv;
313 		list_add_tail(&ldev->sibling_node,
314 			      (struct list_head *)&gd->log_head);
315 		drv++;
316 	}
317 	gd->flags |= GD_FLG_LOG_READY;
318 	gd->default_log_level = LOGL_INFO;
319 	gd->log_fmt = LOGF_DEFAULT;
320 
321 	return 0;
322 }
323