log.h 10 KB

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