log.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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_COUNT, /* Number of log categories */
  44. LOGC_END, /* Sentinel value for a list of log categories */
  45. };
  46. /* Helper to cast a uclass ID to a log category */
  47. static inline int log_uc_cat(enum uclass_id id)
  48. {
  49. return (enum log_category_t)id;
  50. }
  51. /**
  52. * _log() - Internal function to emit a new log record
  53. *
  54. * @cat: Category of log record (indicating which subsystem generated it)
  55. * @level: Level of log record (indicating its severity)
  56. * @file: File name of file where log record was generated
  57. * @line: Line number in file where log record was generated
  58. * @func: Function where log record was generated
  59. * @fmt: printf() format string for log record
  60. * @...: Optional parameters, according to the format string @fmt
  61. * @return 0 if log record was emitted, -ve on error
  62. */
  63. int _log(enum log_category_t cat, enum log_level_t level, const char *file,
  64. int line, const char *func, const char *fmt, ...);
  65. /* Define this at the top of a file to add a prefix to debug messages */
  66. #ifndef pr_fmt
  67. #define pr_fmt(fmt) fmt
  68. #endif
  69. /* Use a default category if this file does not supply one */
  70. #ifndef LOG_CATEGORY
  71. #define LOG_CATEGORY LOGC_NONE
  72. #endif
  73. /*
  74. * This header may be including when CONFIG_LOG is disabled, in which case
  75. * CONFIG_LOG_MAX_LEVEL is not defined. Add a check for this.
  76. */
  77. #if CONFIG_IS_ENABLED(LOG)
  78. #define _LOG_MAX_LEVEL CONFIG_VAL(LOG_MAX_LEVEL)
  79. #define log_err(_fmt...) log(LOG_CATEGORY, LOGL_ERR, ##_fmt)
  80. #define log_warning(_fmt...) log(LOG_CATEGORY, LOGL_WARNING, ##_fmt)
  81. #define log_notice(_fmt...) log(LOG_CATEGORY, LOGL_NOTICE, ##_fmt)
  82. #define log_info(_fmt...) log(LOG_CATEGORY, LOGL_INFO, ##_fmt)
  83. #define log_debug(_fmt...) log(LOG_CATEGORY, LOGL_DEBUG, ##_fmt)
  84. #define log_content(_fmt...) log(LOG_CATEGORY, LOGL_DEBUG_CONTENT, ##_fmt)
  85. #define log_io(_fmt...) log(LOG_CATEGORY, LOGL_DEBUG_IO, ##_fmt)
  86. #else
  87. #define _LOG_MAX_LEVEL LOGL_INFO
  88. #define log_err(_fmt...)
  89. #define log_warning(_fmt...)
  90. #define log_notice(_fmt...)
  91. #define log_info(_fmt...)
  92. #define log_debug(_fmt...)
  93. #define log_content(_fmt...)
  94. #define log_io(_fmt...)
  95. #endif
  96. /* Emit a log record if the level is less that the maximum */
  97. #define log(_cat, _level, _fmt, _args...) ({ \
  98. int _l = _level; \
  99. if (_l <= _LOG_MAX_LEVEL) \
  100. _log((enum log_category_t)(_cat), _l, __FILE__, __LINE__, \
  101. __func__, \
  102. pr_fmt(_fmt), ##_args); \
  103. })
  104. #ifdef DEBUG
  105. #define _DEBUG 1
  106. #else
  107. #define _DEBUG 0
  108. #endif
  109. #ifdef CONFIG_SPL_BUILD
  110. #define _SPL_BUILD 1
  111. #else
  112. #define _SPL_BUILD 0
  113. #endif
  114. #if !_DEBUG && CONFIG_IS_ENABLED(LOG)
  115. #define debug_cond(cond, fmt, args...) \
  116. do { \
  117. if (1) \
  118. log(LOG_CATEGORY, LOGL_DEBUG, fmt, ##args); \
  119. } while (0)
  120. #else /* _DEBUG */
  121. /*
  122. * Output a debug text when condition "cond" is met. The "cond" should be
  123. * computed by a preprocessor in the best case, allowing for the best
  124. * optimization.
  125. */
  126. #define debug_cond(cond, fmt, args...) \
  127. do { \
  128. if (cond) \
  129. printf(pr_fmt(fmt), ##args); \
  130. } while (0)
  131. #endif /* _DEBUG */
  132. /* Show a message if DEBUG is defined in a file */
  133. #define debug(fmt, args...) \
  134. debug_cond(_DEBUG, fmt, ##args)
  135. /* Show a message if not in SPL */
  136. #define warn_non_spl(fmt, args...) \
  137. debug_cond(!_SPL_BUILD, fmt, ##args)
  138. /*
  139. * An assertion is run-time check done in debug mode only. If DEBUG is not
  140. * defined then it is skipped. If DEBUG is defined and the assertion fails,
  141. * then it calls panic*( which may or may not reset/halt U-Boot (see
  142. * CONFIG_PANIC_HANG), It is hoped that all failing assertions are found
  143. * before release, and after release it is hoped that they don't matter. But
  144. * in any case these failing assertions cannot be fixed with a reset (which
  145. * may just do the same assertion again).
  146. */
  147. void __assert_fail(const char *assertion, const char *file, unsigned int line,
  148. const char *function);
  149. #define assert(x) \
  150. ({ if (!(x) && _DEBUG) \
  151. __assert_fail(#x, __FILE__, __LINE__, __func__); })
  152. #ifdef CONFIG_LOG_ERROR_RETURN
  153. #define log_ret(_ret) ({ \
  154. int __ret = (_ret); \
  155. if (__ret < 0) \
  156. log(LOG_CATEGORY, LOGL_ERR, "returning err=%d\n", __ret); \
  157. __ret; \
  158. })
  159. #define log_msg_ret(_msg, _ret) ({ \
  160. int __ret = (_ret); \
  161. if (__ret < 0) \
  162. log(LOG_CATEGORY, LOGL_ERR, "%s: returning err=%d\n", _msg, \
  163. __ret); \
  164. __ret; \
  165. })
  166. #else
  167. #define log_ret(_ret) (_ret)
  168. #define log_msg_ret(_msg, _ret) (_ret)
  169. #endif
  170. /**
  171. * struct log_rec - a single log record
  172. *
  173. * Holds information about a single record in the log
  174. *
  175. * Members marked as 'not allocated' are stored as pointers and the caller is
  176. * responsible for making sure that the data pointed to is not overwritten.
  177. * Memebers marked as 'allocated' are allocated (e.g. via strdup()) by the log
  178. * system.
  179. *
  180. * @cat: Category, representing a uclass or part of U-Boot
  181. * @level: Severity level, less severe is higher
  182. * @file: Name of file where the log record was generated (not allocated)
  183. * @line: Line number where the log record was generated
  184. * @func: Function where the log record was generated (not allocated)
  185. * @msg: Log message (allocated)
  186. */
  187. struct log_rec {
  188. enum log_category_t cat;
  189. enum log_level_t level;
  190. const char *file;
  191. int line;
  192. const char *func;
  193. const char *msg;
  194. };
  195. struct log_device;
  196. /**
  197. * struct log_driver - a driver which accepts and processes log records
  198. *
  199. * @name: Name of driver
  200. */
  201. struct log_driver {
  202. const char *name;
  203. /**
  204. * emit() - emit a log record
  205. *
  206. * Called by the log system to pass a log record to a particular driver
  207. * for processing. The filter is checked before calling this function.
  208. */
  209. int (*emit)(struct log_device *ldev, struct log_rec *rec);
  210. };
  211. /**
  212. * struct log_device - an instance of a log driver
  213. *
  214. * Since drivers are set up at build-time we need to have a separate device for
  215. * the run-time aspects of drivers (currently just a list of filters to apply
  216. * to records send to this device).
  217. *
  218. * @next_filter_num: Seqence number of next filter filter added (0=no filters
  219. * yet). This increments with each new filter on the device, but never
  220. * decrements
  221. * @drv: Pointer to driver for this device
  222. * @filter_head: List of filters for this device
  223. * @sibling_node: Next device in the list of all devices
  224. */
  225. struct log_device {
  226. int next_filter_num;
  227. struct log_driver *drv;
  228. struct list_head filter_head;
  229. struct list_head sibling_node;
  230. };
  231. enum {
  232. LOGF_MAX_CATEGORIES = 5, /* maximum categories per filter */
  233. };
  234. enum log_filter_flags {
  235. LOGFF_HAS_CAT = 1 << 0, /* Filter has a category list */
  236. };
  237. /**
  238. * struct log_filter - criterial to filter out log messages
  239. *
  240. * @filter_num: Sequence number of this filter. This is returned when adding a
  241. * new filter, and must be provided when removing a previously added
  242. * filter.
  243. * @flags: Flags for this filter (LOGFF_...)
  244. * @cat_list: List of categories to allow (terminated by LOGC_none). If empty
  245. * then all categories are permitted. Up to LOGF_MAX_CATEGORIES entries
  246. * can be provided
  247. * @max_level: Maximum log level to allow
  248. * @file_list: List of files to allow, separated by comma. If NULL then all
  249. * files are permitted
  250. * @sibling_node: Next filter in the list of filters for this log device
  251. */
  252. struct log_filter {
  253. int filter_num;
  254. int flags;
  255. enum log_category_t cat_list[LOGF_MAX_CATEGORIES];
  256. enum log_level_t max_level;
  257. const char *file_list;
  258. struct list_head sibling_node;
  259. };
  260. #define LOG_DRIVER(_name) \
  261. ll_entry_declare(struct log_driver, _name, log_driver)
  262. /**
  263. * log_get_cat_name() - Get the name of a category
  264. *
  265. * @cat: Category to look up
  266. * @return category name (which may be a uclass driver name) if found, or
  267. * "<invalid>" if invalid, or "<missing>" if not found
  268. */
  269. const char *log_get_cat_name(enum log_category_t cat);
  270. /**
  271. * log_get_cat_by_name() - Look up a category by name
  272. *
  273. * @name: Name to look up
  274. * @return category ID, or LOGC_NONE if not found
  275. */
  276. enum log_category_t log_get_cat_by_name(const char *name);
  277. /**
  278. * log_get_level_name() - Get the name of a log level
  279. *
  280. * @level: Log level to look up
  281. * @return log level name (in ALL CAPS)
  282. */
  283. const char *log_get_level_name(enum log_level_t level);
  284. /**
  285. * log_get_level_by_name() - Look up a log level by name
  286. *
  287. * @name: Name to look up
  288. * @return log level ID, or LOGL_NONE if not found
  289. */
  290. enum log_level_t log_get_level_by_name(const char *name);
  291. /* Log format flags (bit numbers) for gd->log_fmt. See log_fmt_chars */
  292. enum log_fmt {
  293. LOGF_CAT = 0,
  294. LOGF_LEVEL,
  295. LOGF_FILE,
  296. LOGF_LINE,
  297. LOGF_FUNC,
  298. LOGF_MSG,
  299. LOGF_COUNT,
  300. LOGF_DEFAULT = (1 << LOGF_FUNC) | (1 << LOGF_MSG),
  301. LOGF_ALL = 0x3f,
  302. };
  303. /* Handle the 'log test' command */
  304. int do_log_test(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);
  305. /**
  306. * log_add_filter() - Add a new filter to a log device
  307. *
  308. * @drv_name: Driver name to add the filter to (since each driver only has a
  309. * single device)
  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. * @return the sequence number of the new filter (>=0) if the filter was added,
  317. * or a -ve value on error
  318. */
  319. int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
  320. enum log_level_t max_level, const char *file_list);
  321. /**
  322. * log_remove_filter() - Remove a filter from a log device
  323. *
  324. * @drv_name: Driver name to remove the filter from (since each driver only has
  325. * a single device)
  326. * @filter_num: Filter number to remove (as returned by log_add_filter())
  327. * @return 0 if the filter was removed, -ENOENT if either the driver or the
  328. * filter number was not found
  329. */
  330. int log_remove_filter(const char *drv_name, int filter_num);
  331. #if CONFIG_IS_ENABLED(LOG)
  332. /**
  333. * log_init() - Set up the log system ready for use
  334. *
  335. * @return 0 if OK, -ENOMEM if out of memory
  336. */
  337. int log_init(void);
  338. #else
  339. static inline int log_init(void)
  340. {
  341. return 0;
  342. }
  343. #endif
  344. #endif