log.h 14 KB

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