1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Logging support
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 2017 Google, Inc
5*4882a593Smuzhiyun * Written by Simon Glass <sjg@chromium.org>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #ifndef __LOG_H
11*4882a593Smuzhiyun #define __LOG_H
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <dm/uclass-id.h>
14*4882a593Smuzhiyun #include <linux/list.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /** Log levels supported, ranging from most to least important */
17*4882a593Smuzhiyun enum log_level_t {
18*4882a593Smuzhiyun LOGL_EMERG = 0, /*U-Boot is unstable */
19*4882a593Smuzhiyun LOGL_ALERT, /* Action must be taken immediately */
20*4882a593Smuzhiyun LOGL_CRIT, /* Critical conditions */
21*4882a593Smuzhiyun LOGL_ERR, /* Error that prevents something from working */
22*4882a593Smuzhiyun LOGL_WARNING, /* Warning may prevent optimial operation */
23*4882a593Smuzhiyun LOGL_NOTICE, /* Normal but significant condition, printf() */
24*4882a593Smuzhiyun LOGL_INFO, /* General information message */
25*4882a593Smuzhiyun LOGL_DEBUG, /* Basic debug-level message */
26*4882a593Smuzhiyun LOGL_DEBUG_CONTENT, /* Debug message showing full message content */
27*4882a593Smuzhiyun LOGL_DEBUG_IO, /* Debug message showing hardware I/O access */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun LOGL_COUNT,
30*4882a593Smuzhiyun LOGL_FIRST = LOGL_EMERG,
31*4882a593Smuzhiyun LOGL_MAX = LOGL_DEBUG,
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /**
35*4882a593Smuzhiyun * Log categories supported. Most of these correspond to uclasses (i.e.
36*4882a593Smuzhiyun * enum uclass_id) but there are also some more generic categories
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun enum log_category_t {
39*4882a593Smuzhiyun LOGC_FIRST = 0, /* First part mirrors UCLASS_... */
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun LOGC_NONE = UCLASS_COUNT,
42*4882a593Smuzhiyun LOGC_ARCH,
43*4882a593Smuzhiyun LOGC_BOARD,
44*4882a593Smuzhiyun LOGC_CORE,
45*4882a593Smuzhiyun LOGC_DM, /* Core driver-model */
46*4882a593Smuzhiyun LOGC_DT, /* Device-tree */
47*4882a593Smuzhiyun LOGC_EFI, /* EFI implementation */
48*4882a593Smuzhiyun LOGC_ALLOC, /* Memory allocation */
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun LOGC_COUNT,
51*4882a593Smuzhiyun LOGC_END,
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* Helper to cast a uclass ID to a log category */
log_uc_cat(enum uclass_id id)55*4882a593Smuzhiyun static inline int log_uc_cat(enum uclass_id id)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun return (enum log_category_t)id;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /**
61*4882a593Smuzhiyun * _log() - Internal function to emit a new log record
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * @cat: Category of log record (indicating which subsystem generated it)
64*4882a593Smuzhiyun * @level: Level of log record (indicating its severity)
65*4882a593Smuzhiyun * @file: File name of file where log record was generated
66*4882a593Smuzhiyun * @line: Line number in file where log record was generated
67*4882a593Smuzhiyun * @func: Function where log record was generated
68*4882a593Smuzhiyun * @fmt: printf() format string for log record
69*4882a593Smuzhiyun * @...: Optional parameters, according to the format string @fmt
70*4882a593Smuzhiyun * @return 0 if log record was emitted, -ve on error
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun int _log(enum log_category_t cat, enum log_level_t level, const char *file,
73*4882a593Smuzhiyun int line, const char *func, const char *fmt, ...);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* Define this at the top of a file to add a prefix to debug messages */
76*4882a593Smuzhiyun #ifndef pr_fmt
77*4882a593Smuzhiyun #define pr_fmt(fmt) fmt
78*4882a593Smuzhiyun #endif
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* Use a default category if this file does not supply one */
81*4882a593Smuzhiyun #ifndef LOG_CATEGORY
82*4882a593Smuzhiyun #define LOG_CATEGORY LOGC_NONE
83*4882a593Smuzhiyun #endif
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * This header may be including when CONFIG_LOG is disabled, in which case
87*4882a593Smuzhiyun * CONFIG_LOG_MAX_LEVEL is not defined. Add a check for this.
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(LOG)
90*4882a593Smuzhiyun #define _LOG_MAX_LEVEL CONFIG_VAL(LOG_MAX_LEVEL)
91*4882a593Smuzhiyun #else
92*4882a593Smuzhiyun #define _LOG_MAX_LEVEL LOGL_INFO
93*4882a593Smuzhiyun #endif
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /* Emit a log record if the level is less that the maximum */
96*4882a593Smuzhiyun #define log(_cat, _level, _fmt, _args...) ({ \
97*4882a593Smuzhiyun int _l = _level; \
98*4882a593Smuzhiyun if (_l <= _LOG_MAX_LEVEL) \
99*4882a593Smuzhiyun _log((enum log_category_t)(_cat), _l, __FILE__, __LINE__, \
100*4882a593Smuzhiyun __func__, \
101*4882a593Smuzhiyun pr_fmt(_fmt), ##_args); \
102*4882a593Smuzhiyun })
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun #ifdef DEBUG
105*4882a593Smuzhiyun #define _DEBUG 1
106*4882a593Smuzhiyun #else
107*4882a593Smuzhiyun #define _DEBUG 0
108*4882a593Smuzhiyun #endif
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun #ifdef CONFIG_SPL_BUILD
111*4882a593Smuzhiyun #define _SPL_BUILD 1
112*4882a593Smuzhiyun #else
113*4882a593Smuzhiyun #define _SPL_BUILD 0
114*4882a593Smuzhiyun #endif
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun #if !_DEBUG && CONFIG_IS_ENABLED(LOG)
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun #define debug_cond(cond, fmt, args...) \
119*4882a593Smuzhiyun do { \
120*4882a593Smuzhiyun if (1) \
121*4882a593Smuzhiyun log(LOG_CATEGORY, LOGL_DEBUG, fmt, ##args); \
122*4882a593Smuzhiyun } while (0)
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun #else /* _DEBUG */
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /*
127*4882a593Smuzhiyun * Output a debug text when condition "cond" is met. The "cond" should be
128*4882a593Smuzhiyun * computed by a preprocessor in the best case, allowing for the best
129*4882a593Smuzhiyun * optimization.
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun #define debug_cond(cond, fmt, args...) \
132*4882a593Smuzhiyun do { \
133*4882a593Smuzhiyun if (cond) \
134*4882a593Smuzhiyun printf(pr_fmt(fmt), ##args); \
135*4882a593Smuzhiyun } while (0)
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun #endif /* _DEBUG */
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /* Show a message if DEBUG is defined in a file */
140*4882a593Smuzhiyun #define debug(fmt, args...) \
141*4882a593Smuzhiyun debug_cond(_DEBUG, fmt, ##args)
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* Show a message if not in SPL */
144*4882a593Smuzhiyun #define warn_non_spl(fmt, args...) \
145*4882a593Smuzhiyun debug_cond(!_SPL_BUILD, fmt, ##args)
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /*
148*4882a593Smuzhiyun * An assertion is run-time check done in debug mode only. If DEBUG is not
149*4882a593Smuzhiyun * defined then it is skipped. If DEBUG is defined and the assertion fails,
150*4882a593Smuzhiyun * then it calls panic*( which may or may not reset/halt U-Boot (see
151*4882a593Smuzhiyun * CONFIG_PANIC_HANG), It is hoped that all failing assertions are found
152*4882a593Smuzhiyun * before release, and after release it is hoped that they don't matter. But
153*4882a593Smuzhiyun * in any case these failing assertions cannot be fixed with a reset (which
154*4882a593Smuzhiyun * may just do the same assertion again).
155*4882a593Smuzhiyun */
156*4882a593Smuzhiyun void __assert_fail(const char *assertion, const char *file, unsigned int line,
157*4882a593Smuzhiyun const char *function);
158*4882a593Smuzhiyun #define assert(x) \
159*4882a593Smuzhiyun ({ if (!(x) && _DEBUG) \
160*4882a593Smuzhiyun __assert_fail(#x, __FILE__, __LINE__, __func__); })
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun #ifdef CONFIG_LOG_ERROR_RETURN
163*4882a593Smuzhiyun #define log_ret(_ret) ({ \
164*4882a593Smuzhiyun int __ret = (_ret); \
165*4882a593Smuzhiyun if (__ret < 0) \
166*4882a593Smuzhiyun log(LOG_CATEGORY, LOGL_ERR, "returning err=%d\n", __ret); \
167*4882a593Smuzhiyun __ret; \
168*4882a593Smuzhiyun })
169*4882a593Smuzhiyun #define log_msg_ret(_msg, _ret) ({ \
170*4882a593Smuzhiyun int __ret = (_ret); \
171*4882a593Smuzhiyun if (__ret < 0) \
172*4882a593Smuzhiyun log(LOG_CATEGORY, LOGL_ERR, "%s: returning err=%d\n", _msg, \
173*4882a593Smuzhiyun __ret); \
174*4882a593Smuzhiyun __ret; \
175*4882a593Smuzhiyun })
176*4882a593Smuzhiyun #else
177*4882a593Smuzhiyun #define log_ret(_ret) (_ret)
178*4882a593Smuzhiyun #define log_msg_ret(_msg, _ret) (_ret)
179*4882a593Smuzhiyun #endif
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /**
182*4882a593Smuzhiyun * struct log_rec - a single log record
183*4882a593Smuzhiyun *
184*4882a593Smuzhiyun * Holds information about a single record in the log
185*4882a593Smuzhiyun *
186*4882a593Smuzhiyun * Members marked as 'not allocated' are stored as pointers and the caller is
187*4882a593Smuzhiyun * responsible for making sure that the data pointed to is not overwritten.
188*4882a593Smuzhiyun * Memebers marked as 'allocated' are allocated (e.g. via strdup()) by the log
189*4882a593Smuzhiyun * system.
190*4882a593Smuzhiyun *
191*4882a593Smuzhiyun * @cat: Category, representing a uclass or part of U-Boot
192*4882a593Smuzhiyun * @level: Severity level, less severe is higher
193*4882a593Smuzhiyun * @file: Name of file where the log record was generated (not allocated)
194*4882a593Smuzhiyun * @line: Line number where the log record was generated
195*4882a593Smuzhiyun * @func: Function where the log record was generated (not allocated)
196*4882a593Smuzhiyun * @msg: Log message (allocated)
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun struct log_rec {
199*4882a593Smuzhiyun enum log_category_t cat;
200*4882a593Smuzhiyun enum log_level_t level;
201*4882a593Smuzhiyun const char *file;
202*4882a593Smuzhiyun int line;
203*4882a593Smuzhiyun const char *func;
204*4882a593Smuzhiyun const char *msg;
205*4882a593Smuzhiyun };
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun struct log_device;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /**
210*4882a593Smuzhiyun * struct log_driver - a driver which accepts and processes log records
211*4882a593Smuzhiyun *
212*4882a593Smuzhiyun * @name: Name of driver
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun struct log_driver {
215*4882a593Smuzhiyun const char *name;
216*4882a593Smuzhiyun /**
217*4882a593Smuzhiyun * emit() - emit a log record
218*4882a593Smuzhiyun *
219*4882a593Smuzhiyun * Called by the log system to pass a log record to a particular driver
220*4882a593Smuzhiyun * for processing. The filter is checked before calling this function.
221*4882a593Smuzhiyun */
222*4882a593Smuzhiyun int (*emit)(struct log_device *ldev, struct log_rec *rec);
223*4882a593Smuzhiyun };
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /**
226*4882a593Smuzhiyun * struct log_device - an instance of a log driver
227*4882a593Smuzhiyun *
228*4882a593Smuzhiyun * Since drivers are set up at build-time we need to have a separate device for
229*4882a593Smuzhiyun * the run-time aspects of drivers (currently just a list of filters to apply
230*4882a593Smuzhiyun * to records send to this device).
231*4882a593Smuzhiyun *
232*4882a593Smuzhiyun * @next_filter_num: Seqence number of next filter filter added (0=no filters
233*4882a593Smuzhiyun * yet). This increments with each new filter on the device, but never
234*4882a593Smuzhiyun * decrements
235*4882a593Smuzhiyun * @drv: Pointer to driver for this device
236*4882a593Smuzhiyun * @filter_head: List of filters for this device
237*4882a593Smuzhiyun * @sibling_node: Next device in the list of all devices
238*4882a593Smuzhiyun */
239*4882a593Smuzhiyun struct log_device {
240*4882a593Smuzhiyun int next_filter_num;
241*4882a593Smuzhiyun struct log_driver *drv;
242*4882a593Smuzhiyun struct list_head filter_head;
243*4882a593Smuzhiyun struct list_head sibling_node;
244*4882a593Smuzhiyun };
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun enum {
247*4882a593Smuzhiyun LOGF_MAX_CATEGORIES = 5, /* maximum categories per filter */
248*4882a593Smuzhiyun };
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun enum log_filter_flags {
251*4882a593Smuzhiyun LOGFF_HAS_CAT = 1 << 0, /* Filter has a category list */
252*4882a593Smuzhiyun };
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /**
255*4882a593Smuzhiyun * struct log_filter - criterial to filter out log messages
256*4882a593Smuzhiyun *
257*4882a593Smuzhiyun * @filter_num: Sequence number of this filter. This is returned when adding a
258*4882a593Smuzhiyun * new filter, and must be provided when removing a previously added
259*4882a593Smuzhiyun * filter.
260*4882a593Smuzhiyun * @flags: Flags for this filter (LOGFF_...)
261*4882a593Smuzhiyun * @cat_list: List of categories to allow (terminated by LOGC_none). If empty
262*4882a593Smuzhiyun * then all categories are permitted. Up to LOGF_MAX_CATEGORIES entries
263*4882a593Smuzhiyun * can be provided
264*4882a593Smuzhiyun * @max_level: Maximum log level to allow
265*4882a593Smuzhiyun * @file_list: List of files to allow, separated by comma. If NULL then all
266*4882a593Smuzhiyun * files are permitted
267*4882a593Smuzhiyun * @sibling_node: Next filter in the list of filters for this log device
268*4882a593Smuzhiyun */
269*4882a593Smuzhiyun struct log_filter {
270*4882a593Smuzhiyun int filter_num;
271*4882a593Smuzhiyun int flags;
272*4882a593Smuzhiyun enum log_category_t cat_list[LOGF_MAX_CATEGORIES];
273*4882a593Smuzhiyun enum log_level_t max_level;
274*4882a593Smuzhiyun const char *file_list;
275*4882a593Smuzhiyun struct list_head sibling_node;
276*4882a593Smuzhiyun };
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun #define LOG_DRIVER(_name) \
279*4882a593Smuzhiyun ll_entry_declare(struct log_driver, _name, log_driver)
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /* Handle the 'log test' command */
282*4882a593Smuzhiyun int do_log_test(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /**
285*4882a593Smuzhiyun * log_add_filter() - Add a new filter to a log device
286*4882a593Smuzhiyun *
287*4882a593Smuzhiyun * @drv_name: Driver name to add the filter to (since each driver only has a
288*4882a593Smuzhiyun * single device)
289*4882a593Smuzhiyun * @cat_list: List of categories to allow (terminated by LOGC_none). If empty
290*4882a593Smuzhiyun * then all categories are permitted. Up to LOGF_MAX_CATEGORIES entries
291*4882a593Smuzhiyun * can be provided
292*4882a593Smuzhiyun * @max_level: Maximum log level to allow
293*4882a593Smuzhiyun * @file_list: List of files to allow, separated by comma. If NULL then all
294*4882a593Smuzhiyun * files are permitted
295*4882a593Smuzhiyun * @return the sequence number of the new filter (>=0) if the filter was added,
296*4882a593Smuzhiyun * or a -ve value on error
297*4882a593Smuzhiyun */
298*4882a593Smuzhiyun int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
299*4882a593Smuzhiyun enum log_level_t max_level, const char *file_list);
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun /**
302*4882a593Smuzhiyun * log_remove_filter() - Remove a filter from a log device
303*4882a593Smuzhiyun *
304*4882a593Smuzhiyun * @drv_name: Driver name to remove the filter from (since each driver only has
305*4882a593Smuzhiyun * a single device)
306*4882a593Smuzhiyun * @filter_num: Filter number to remove (as returned by log_add_filter())
307*4882a593Smuzhiyun * @return 0 if the filter was removed, -ENOENT if either the driver or the
308*4882a593Smuzhiyun * filter number was not found
309*4882a593Smuzhiyun */
310*4882a593Smuzhiyun int log_remove_filter(const char *drv_name, int filter_num);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(LOG)
313*4882a593Smuzhiyun /**
314*4882a593Smuzhiyun * log_init() - Set up the log system ready for use
315*4882a593Smuzhiyun *
316*4882a593Smuzhiyun * @return 0 if OK, -ENOMEM if out of memory
317*4882a593Smuzhiyun */
318*4882a593Smuzhiyun int log_init(void);
319*4882a593Smuzhiyun #else
log_init(void)320*4882a593Smuzhiyun static inline int log_init(void)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun return 0;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun #endif
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun #endif
327