Blame view

include/log.h 13.5 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  /* SPDX-License-Identifier: GPL-2.0+ */
0e98b0a65   Simon Glass   Move debug and lo...
2
3
4
5
6
  /*
   * Logging support
   *
   * Copyright (c) 2017 Google, Inc
   * Written by Simon Glass <sjg@chromium.org>
0e98b0a65   Simon Glass   Move debug and lo...
7
8
9
10
   */
  
  #ifndef __LOG_H
  #define __LOG_H
aaa05deb1   Sean Anderson   log: Include miss...
11
  #include <command.h>
e9c8d49d5   Simon Glass   log: Add an imple...
12
13
14
15
16
  #include <dm/uclass-id.h>
  #include <linux/list.h>
  
  /** Log levels supported, ranging from most to least important */
  enum log_level_t {
6fc7e9389   Simon Glass   log: Fix up Kconf...
17
  	LOGL_EMERG = 0,		/* U-Boot is unstable */
e9c8d49d5   Simon Glass   log: Add an imple...
18
19
20
21
22
23
24
25
26
27
28
  	LOGL_ALERT,		/* Action must be taken immediately */
  	LOGL_CRIT,		/* Critical conditions */
  	LOGL_ERR,		/* Error that prevents something from working */
  	LOGL_WARNING,		/* Warning may prevent optimial operation */
  	LOGL_NOTICE,		/* Normal but significant condition, printf() */
  	LOGL_INFO,		/* General information message */
  	LOGL_DEBUG,		/* Basic debug-level message */
  	LOGL_DEBUG_CONTENT,	/* Debug message showing full message content */
  	LOGL_DEBUG_IO,		/* Debug message showing hardware I/O access */
  
  	LOGL_COUNT,
f941c8d76   Simon Glass   log: Add function...
29
  	LOGL_NONE,
e9c8d49d5   Simon Glass   log: Add an imple...
30
  	LOGL_FIRST = LOGL_EMERG,
f941c8d76   Simon Glass   log: Add function...
31
  	LOGL_MAX = LOGL_DEBUG_IO,
e9c8d49d5   Simon Glass   log: Add an imple...
32
33
34
35
36
37
38
39
  };
  
  /**
   * Log categories supported. Most of these correspond to uclasses (i.e.
   * enum uclass_id) but there are also some more generic categories
   */
  enum log_category_t {
  	LOGC_FIRST = 0,	/* First part mirrors UCLASS_... */
0bf964592   Simon Glass   log: Add comments...
40
41
42
43
  	LOGC_NONE = UCLASS_COUNT,	/* First number is after all uclasses */
  	LOGC_ARCH,	/* Related to arch-specific code */
  	LOGC_BOARD,	/* Related to board-specific code */
  	LOGC_CORE,	/* Related to core features (non-driver-model) */
f941c8d76   Simon Glass   log: Add function...
44
45
  	LOGC_DM,	/* Core driver-model */
  	LOGC_DT,	/* Device-tree */
d8d8997bb   Heinrich Schuchardt   log: fix typo LOG...
46
  	LOGC_EFI,	/* EFI implementation */
c3aed5db5   Simon Glass   sandbox: spi: Add...
47
  	LOGC_ALLOC,	/* Memory allocation */
a5c13b68e   Simon Glass   sandbox: log: Add...
48
  	LOGC_SANDBOX,	/* Related to the sandbox board */
9f407d4ef   Simon Glass   Add core support ...
49
  	LOGC_BLOBLIST,	/* Bloblist */
cce61fc42   Simon Glass   dm: devres: Conve...
50
  	LOGC_DEVRES,	/* Device resources (devres_... functions) */
e9c8d49d5   Simon Glass   log: Add an imple...
51

0bf964592   Simon Glass   log: Add comments...
52
53
  	LOGC_COUNT,	/* Number of log categories */
  	LOGC_END,	/* Sentinel value for a list of log categories */
e9c8d49d5   Simon Glass   log: Add an imple...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  };
  
  /* Helper to cast a uclass ID to a log category */
  static inline int log_uc_cat(enum uclass_id id)
  {
  	return (enum log_category_t)id;
  }
  
  /**
   * _log() - Internal function to emit a new log record
   *
   * @cat: Category of log record (indicating which subsystem generated it)
   * @level: Level of log record (indicating its severity)
   * @file: File name of file where log record was generated
   * @line: Line number in file where log record was generated
   * @func: Function where log record was generated
   * @fmt: printf() format string for log record
   * @...: Optional parameters, according to the format string @fmt
   * @return 0 if log record was emitted, -ve on error
   */
  int _log(enum log_category_t cat, enum log_level_t level, const char *file,
ed4e933d1   Simon Glass   log: Check printf...
75
76
  	 int line, const char *func, const char *fmt, ...)
  		__attribute__ ((format (__printf__, 6, 7)));
e9c8d49d5   Simon Glass   log: Add an imple...
77

fd42948fc   Simon Glass   log: Add log_nop(...
78
79
80
81
82
83
84
85
86
87
88
  static inline int _log_nop(enum log_category_t cat, enum log_level_t level,
  			   const char *file, int line, const char *func,
  			   const char *fmt, ...)
  		__attribute__ ((format (__printf__, 6, 7)));
  
  static inline int _log_nop(enum log_category_t cat, enum log_level_t level,
  			   const char *file, int line, const char *func,
  			   const char *fmt, ...)
  {
  	return 0;
  }
e9c8d49d5   Simon Glass   log: Add an imple...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  /* Define this at the top of a file to add a prefix to debug messages */
  #ifndef pr_fmt
  #define pr_fmt(fmt) fmt
  #endif
  
  /* Use a default category if this file does not supply one */
  #ifndef LOG_CATEGORY
  #define LOG_CATEGORY LOGC_NONE
  #endif
  
  /*
   * This header may be including when CONFIG_LOG is disabled, in which case
   * CONFIG_LOG_MAX_LEVEL is not defined. Add a check for this.
   */
  #if CONFIG_IS_ENABLED(LOG)
  #define _LOG_MAX_LEVEL CONFIG_VAL(LOG_MAX_LEVEL)
cdd140af5   Simon Glass   log: Add helpers ...
105
106
107
108
109
110
111
  #define log_err(_fmt...)	log(LOG_CATEGORY, LOGL_ERR, ##_fmt)
  #define log_warning(_fmt...)	log(LOG_CATEGORY, LOGL_WARNING, ##_fmt)
  #define log_notice(_fmt...)	log(LOG_CATEGORY, LOGL_NOTICE, ##_fmt)
  #define log_info(_fmt...)	log(LOG_CATEGORY, LOGL_INFO, ##_fmt)
  #define log_debug(_fmt...)	log(LOG_CATEGORY, LOGL_DEBUG, ##_fmt)
  #define log_content(_fmt...)	log(LOG_CATEGORY, LOGL_DEBUG_CONTENT, ##_fmt)
  #define log_io(_fmt...)		log(LOG_CATEGORY, LOGL_DEBUG_IO, ##_fmt)
e9c8d49d5   Simon Glass   log: Add an imple...
112
113
  #else
  #define _LOG_MAX_LEVEL LOGL_INFO
fd42948fc   Simon Glass   log: Add log_nop(...
114
115
116
117
118
119
120
121
  #define log_err(_fmt...)	log_nop(LOG_CATEGORY, LOGL_ERR, ##_fmt)
  #define log_warning(_fmt...)	log_nop(LOG_CATEGORY, LOGL_WARNING, ##_fmt)
  #define log_notice(_fmt...)	log_nop(LOG_CATEGORY, LOGL_NOTICE, ##_fmt)
  #define log_info(_fmt...)	log_nop(LOG_CATEGORY, LOGL_INFO, ##_fmt)
  #define log_debug(_fmt...)	log_nop(LOG_CATEGORY, LOGL_DEBUG, ##_fmt)
  #define log_content(_fmt...)	log_nop(LOG_CATEGORY, \
  					LOGL_DEBUG_CONTENT, ##_fmt)
  #define log_io(_fmt...)		log_nop(LOG_CATEGORY, LOGL_DEBUG_IO, ##_fmt)
e9c8d49d5   Simon Glass   log: Add an imple...
122
  #endif
4d8d3056f   Simon Glass   spl: Add support ...
123
  #if CONFIG_IS_ENABLED(LOG)
f9811e857   Simon Glass   log: Allow #defin...
124
125
126
127
128
  #ifdef LOG_DEBUG
  #define _LOG_DEBUG	1
  #else
  #define _LOG_DEBUG	0
  #endif
4d8d3056f   Simon Glass   spl: Add support ...
129

e9c8d49d5   Simon Glass   log: Add an imple...
130
131
132
  /* Emit a log record if the level is less that the maximum */
  #define log(_cat, _level, _fmt, _args...) ({ \
  	int _l = _level; \
f9811e857   Simon Glass   log: Allow #defin...
133
  	if (CONFIG_IS_ENABLED(LOG) && (_l <= _LOG_MAX_LEVEL || _LOG_DEBUG)) \
e9c8d49d5   Simon Glass   log: Add an imple...
134
135
136
137
  		_log((enum log_category_t)(_cat), _l, __FILE__, __LINE__, \
  		      __func__, \
  		      pr_fmt(_fmt), ##_args); \
  	})
4d8d3056f   Simon Glass   spl: Add support ...
138
139
140
  #else
  #define log(_cat, _level, _fmt, _args...)
  #endif
e9c8d49d5   Simon Glass   log: Add an imple...
141

fd42948fc   Simon Glass   log: Add log_nop(...
142
143
144
145
146
  #define log_nop(_cat, _level, _fmt, _args...) ({ \
  	int _l = _level; \
  	_log_nop((enum log_category_t)(_cat), _l, __FILE__, __LINE__, \
  		      __func__, pr_fmt(_fmt), ##_args); \
  })
0e98b0a65   Simon Glass   Move debug and lo...
147
148
149
150
151
152
153
154
155
156
157
  #ifdef DEBUG
  #define _DEBUG	1
  #else
  #define _DEBUG	0
  #endif
  
  #ifdef CONFIG_SPL_BUILD
  #define _SPL_BUILD	1
  #else
  #define _SPL_BUILD	0
  #endif
e9c8d49d5   Simon Glass   log: Add an imple...
158
159
160
161
162
163
164
165
166
  #if !_DEBUG && CONFIG_IS_ENABLED(LOG)
  
  #define debug_cond(cond, fmt, args...)			\
  	do {						\
  		if (1)					\
  			log(LOG_CATEGORY, LOGL_DEBUG, fmt, ##args); \
  	} while (0)
  
  #else /* _DEBUG */
0e98b0a65   Simon Glass   Move debug and lo...
167
168
169
170
171
172
173
174
175
176
  /*
   * Output a debug text when condition "cond" is met. The "cond" should be
   * computed by a preprocessor in the best case, allowing for the best
   * optimization.
   */
  #define debug_cond(cond, fmt, args...)			\
  	do {						\
  		if (cond)				\
  			printf(pr_fmt(fmt), ##args);	\
  	} while (0)
e9c8d49d5   Simon Glass   log: Add an imple...
177
  #endif /* _DEBUG */
0e98b0a65   Simon Glass   Move debug and lo...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  /* Show a message if DEBUG is defined in a file */
  #define debug(fmt, args...)			\
  	debug_cond(_DEBUG, fmt, ##args)
  
  /* Show a message if not in SPL */
  #define warn_non_spl(fmt, args...)			\
  	debug_cond(!_SPL_BUILD, fmt, ##args)
  
  /*
   * An assertion is run-time check done in debug mode only. If DEBUG is not
   * defined then it is skipped. If DEBUG is defined and the assertion fails,
   * then it calls panic*( which may or may not reset/halt U-Boot (see
   * CONFIG_PANIC_HANG), It is hoped that all failing assertions are found
   * before release, and after release it is hoped that they don't matter. But
   * in any case these failing assertions cannot be fixed with a reset (which
   * may just do the same assertion again).
   */
  void __assert_fail(const char *assertion, const char *file, unsigned int line,
  		   const char *function);
b236f8c08   Heinrich Schuchardt   log: document the...
197
198
199
200
201
202
203
204
205
206
207
208
  
  /**
   * assert() - assert expression is true
   *
   * If the expression x evaluates to false and _DEBUG evaluates to true, a panic
   * message is written and the system stalls. The value of _DEBUG is set to true
   * if DEBUG is defined before including common.h.
   *
   * The expression x is always executed irrespective of the value of _DEBUG.
   *
   * @x:		expression to test
   */
0e98b0a65   Simon Glass   Move debug and lo...
209
210
211
  #define assert(x) \
  	({ if (!(x) && _DEBUG) \
  		__assert_fail(#x, __FILE__, __LINE__, __func__); })
cd01d2d59   Simon Glass   common: Add a noi...
212
213
214
215
216
217
218
219
220
221
222
223
224
  /*
   * This one actually gets compiled in even without DEBUG. It doesn't include the
   * full pathname as it may be huge. Only use this when the user should be
   * warning, similar to BUG_ON() in linux.
   *
   * @return true if assertion succeeded (condition is true), else false
   */
  #define assert_noisy(x) \
  	({ bool _val = (x); \
  	if (!_val) \
  		__assert_fail(#x, "?", __LINE__, __func__); \
  	_val; \
  	})
4d8d3056f   Simon Glass   spl: Add support ...
225
226
227
228
229
230
231
232
233
234
  #if CONFIG_IS_ENABLED(LOG) && defined(CONFIG_LOG_ERROR_RETURN)
  /*
   * Log an error return value, possibly with a message. Usage:
   *
   *	return log_ret(fred_call());
   *
   * or:
   *
   *	return log_msg_ret("fred failed", fred_call());
   */
3707c6ee0   Simon Glass   log: Add a way to...
235
236
237
238
239
240
241
  #define log_ret(_ret) ({ \
  	int __ret = (_ret); \
  	if (__ret < 0) \
  		log(LOG_CATEGORY, LOGL_ERR, "returning err=%d
  ", __ret); \
  	__ret; \
  	})
b616cef97   Simon Glass   log: Add a way to...
242
243
244
245
246
247
248
249
  #define log_msg_ret(_msg, _ret) ({ \
  	int __ret = (_ret); \
  	if (__ret < 0) \
  		log(LOG_CATEGORY, LOGL_ERR, "%s: returning err=%d
  ", _msg, \
  		    __ret); \
  	__ret; \
  	})
3707c6ee0   Simon Glass   log: Add a way to...
250
  #else
4d8d3056f   Simon Glass   spl: Add support ...
251
  /* Non-logging versions of the above which just return the error code */
3707c6ee0   Simon Glass   log: Add a way to...
252
  #define log_ret(_ret) (_ret)
4d8d3056f   Simon Glass   spl: Add support ...
253
  #define log_msg_ret(_msg, _ret) ((void)(_msg), _ret)
3707c6ee0   Simon Glass   log: Add a way to...
254
  #endif
e9c8d49d5   Simon Glass   log: Add an imple...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  /**
   * struct log_rec - a single log record
   *
   * Holds information about a single record in the log
   *
   * Members marked as 'not allocated' are stored as pointers and the caller is
   * responsible for making sure that the data pointed to is not overwritten.
   * Memebers marked as 'allocated' are allocated (e.g. via strdup()) by the log
   * system.
   *
   * @cat: Category, representing a uclass or part of U-Boot
   * @level: Severity level, less severe is higher
   * @file: Name of file where the log record was generated (not allocated)
   * @line: Line number where the log record was generated
   * @func: Function where the log record was generated (not allocated)
   * @msg: Log message (allocated)
   */
  struct log_rec {
  	enum log_category_t cat;
  	enum log_level_t level;
  	const char *file;
  	int line;
  	const char *func;
  	const char *msg;
  };
  
  struct log_device;
  
  /**
   * struct log_driver - a driver which accepts and processes log records
   *
   * @name: Name of driver
   */
  struct log_driver {
  	const char *name;
  	/**
  	 * emit() - emit a log record
  	 *
  	 * Called by the log system to pass a log record to a particular driver
  	 * for processing. The filter is checked before calling this function.
  	 */
  	int (*emit)(struct log_device *ldev, struct log_rec *rec);
  };
  
  /**
   * struct log_device - an instance of a log driver
   *
   * Since drivers are set up at build-time we need to have a separate device for
   * the run-time aspects of drivers (currently just a list of filters to apply
   * to records send to this device).
   *
   * @next_filter_num: Seqence number of next filter filter added (0=no filters
   *	yet). This increments with each new filter on the device, but never
   *	decrements
   * @drv: Pointer to driver for this device
   * @filter_head: List of filters for this device
   * @sibling_node: Next device in the list of all devices
   */
  struct log_device {
  	int next_filter_num;
  	struct log_driver *drv;
  	struct list_head filter_head;
  	struct list_head sibling_node;
  };
  
  enum {
  	LOGF_MAX_CATEGORIES = 5,	/* maximum categories per filter */
  };
  
  enum log_filter_flags {
  	LOGFF_HAS_CAT		= 1 << 0,	/* Filter has a category list */
  };
  
  /**
   * struct log_filter - criterial to filter out log messages
   *
   * @filter_num: Sequence number of this filter.  This is returned when adding a
   *	new filter, and must be provided when removing a previously added
   *	filter.
   * @flags: Flags for this filter (LOGFF_...)
   * @cat_list: List of categories to allow (terminated by LOGC_none). If empty
   *	then all categories are permitted. Up to LOGF_MAX_CATEGORIES entries
   *	can be provided
   * @max_level: Maximum log level to allow
   * @file_list: List of files to allow, separated by comma. If NULL then all
   *	files are permitted
   * @sibling_node: Next filter in the list of filters for this log device
   */
  struct log_filter {
  	int filter_num;
  	int flags;
  	enum log_category_t cat_list[LOGF_MAX_CATEGORIES];
  	enum log_level_t max_level;
  	const char *file_list;
  	struct list_head sibling_node;
  };
  
  #define LOG_DRIVER(_name) \
  	ll_entry_declare(struct log_driver, _name, log_driver)
f941c8d76   Simon Glass   log: Add function...
354
355
356
357
  /**
   * log_get_cat_name() - Get the name of a category
   *
   * @cat: Category to look up
c2e4e7e63   Simon Glass   log: Fix incorect...
358
359
   * @return category name (which may be a uclass driver name) if found, or
   *	 "<invalid>" if invalid, or "<missing>" if not found
f941c8d76   Simon Glass   log: Add function...
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
   */
  const char *log_get_cat_name(enum log_category_t cat);
  
  /**
   * log_get_cat_by_name() - Look up a category by name
   *
   * @name: Name to look up
   * @return category ID, or LOGC_NONE if not found
   */
  enum log_category_t log_get_cat_by_name(const char *name);
  
  /**
   * log_get_level_name() - Get the name of a log level
   *
   * @level: Log level to look up
   * @return log level name (in ALL CAPS)
   */
  const char *log_get_level_name(enum log_level_t level);
  
  /**
   * log_get_level_by_name() - Look up a log level by name
   *
   * @name: Name to look up
   * @return log level ID, or LOGL_NONE if not found
   */
  enum log_level_t log_get_level_by_name(const char *name);
3b73e8d06   Simon Glass   log: Add control ...
386
387
388
389
390
391
392
393
394
395
396
397
398
  /* Log format flags (bit numbers) for gd->log_fmt. See log_fmt_chars */
  enum log_fmt {
  	LOGF_CAT	= 0,
  	LOGF_LEVEL,
  	LOGF_FILE,
  	LOGF_LINE,
  	LOGF_FUNC,
  	LOGF_MSG,
  
  	LOGF_COUNT,
  	LOGF_DEFAULT = (1 << LOGF_FUNC) | (1 << LOGF_MSG),
  	LOGF_ALL = 0x3f,
  };
ef11ed823   Simon Glass   log: Add a test c...
399
400
  /* Handle the 'log test' command */
  int do_log_test(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);
e9c8d49d5   Simon Glass   log: Add an imple...
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
  /**
   * log_add_filter() - Add a new filter to a log device
   *
   * @drv_name: Driver name to add the filter to (since each driver only has a
   *	single device)
   * @cat_list: List of categories to allow (terminated by LOGC_none). If empty
   *	then all categories are permitted. Up to LOGF_MAX_CATEGORIES entries
   *	can be provided
   * @max_level: Maximum log level to allow
   * @file_list: List of files to allow, separated by comma. If NULL then all
   *	files are permitted
   * @return the sequence number of the new filter (>=0) if the filter was added,
   *	or a -ve value on error
   */
  int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
  		   enum log_level_t max_level, const char *file_list);
  
  /**
   * log_remove_filter() - Remove a filter from a log device
   *
   * @drv_name: Driver name to remove the filter from (since each driver only has
   *	a single device)
   * @filter_num: Filter number to remove (as returned by log_add_filter())
   * @return 0 if the filter was removed, -ENOENT if either the driver or the
   *	filter number was not found
   */
  int log_remove_filter(const char *drv_name, int filter_num);
  
  #if CONFIG_IS_ENABLED(LOG)
  /**
   * log_init() - Set up the log system ready for use
   *
   * @return 0 if OK, -ENOMEM if out of memory
   */
  int log_init(void);
  #else
  static inline int log_init(void)
  {
  	return 0;
  }
  #endif
0e98b0a65   Simon Glass   Move debug and lo...
442
  #endif