log.h 8.7 KB

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