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