log.h 13 KB

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