log.h 14 KB

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