log.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. /**
  17. * enum log_level_t - Log levels supported, ranging from most to least important
  18. */
  19. enum log_level_t {
  20. /** @LOGL_EMERG: U-Boot is unstable */
  21. LOGL_EMERG = 0,
  22. /** @LOGL_ALERT: Action must be taken immediately */
  23. LOGL_ALERT,
  24. /** @LOGL_CRIT: Critical conditions */
  25. LOGL_CRIT,
  26. /** @LOGL_ERR: Error that prevents something from working */
  27. LOGL_ERR,
  28. /** @LOGL_WARNING: Warning may prevent optimal operation */
  29. LOGL_WARNING,
  30. /** @LOGL_NOTICE: Normal but significant condition, printf() */
  31. LOGL_NOTICE,
  32. /** @LOGL_INFO: General information message */
  33. LOGL_INFO,
  34. /** @LOGL_DEBUG: Basic debug-level message */
  35. LOGL_DEBUG,
  36. /** @LOGL_DEBUG_CONTENT: Debug message showing full message content */
  37. LOGL_DEBUG_CONTENT,
  38. /** @LOGL_DEBUG_IO: Debug message showing hardware I/O access */
  39. LOGL_DEBUG_IO,
  40. /** @LOGL_COUNT: Total number of valid log levels */
  41. LOGL_COUNT,
  42. /** @LOGL_NONE: Used to indicate that there is no valid log level */
  43. LOGL_NONE,
  44. /** @LOGL_LEVEL_MASK: Mask for valid log levels */
  45. LOGL_LEVEL_MASK = 0xf,
  46. /** @LOGL_FORCE_DEBUG: Mask to force output due to LOG_DEBUG */
  47. LOGL_FORCE_DEBUG = 0x10,
  48. /** @LOGL_FIRST: The first, most-important log level */
  49. LOGL_FIRST = LOGL_EMERG,
  50. /** @LOGL_MAX: The last, least-important log level */
  51. LOGL_MAX = LOGL_DEBUG_IO,
  52. /** @LOGL_CONT: Use same log level as in previous call */
  53. LOGL_CONT = -1,
  54. };
  55. /**
  56. * enum log_category_t - Log categories supported.
  57. *
  58. * Log categories between %LOGC_FIRST and %LOGC_NONE correspond to uclasses
  59. * (i.e. &enum uclass_id), but there are also some more generic categories.
  60. *
  61. * Remember to update log_cat_name[] after adding a new category.
  62. */
  63. enum log_category_t {
  64. /** @LOGC_FIRST: First log category */
  65. LOGC_FIRST = 0, /* First part mirrors UCLASS_... */
  66. /** @LOGC_NONE: Default log category */
  67. LOGC_NONE = UCLASS_COUNT, /* First number is after all uclasses */
  68. /** @LOGC_ARCH: Related to arch-specific code */
  69. LOGC_ARCH,
  70. /** @LOGC_BOARD: Related to board-specific code */
  71. LOGC_BOARD,
  72. /** @LOGC_CORE: Related to core features (non-driver-model) */
  73. LOGC_CORE,
  74. /** @LOGC_DM: Core driver-model */
  75. LOGC_DM,
  76. /** @LOGC_DT: Device-tree */
  77. LOGC_DT,
  78. /** @LOGC_EFI: EFI implementation */
  79. LOGC_EFI,
  80. /** @LOGC_ALLOC: Memory allocation */
  81. LOGC_ALLOC,
  82. /** @LOGC_SANDBOX: Related to the sandbox board */
  83. LOGC_SANDBOX,
  84. /** @LOGC_BLOBLIST: Bloblist */
  85. LOGC_BLOBLIST,
  86. /** @LOGC_DEVRES: Device resources (``devres_...`` functions) */
  87. LOGC_DEVRES,
  88. /** @LOGC_ACPI: Advanced Configuration and Power Interface (ACPI) */
  89. LOGC_ACPI,
  90. /** @LOGC_BOOT: Related to boot process / boot image processing */
  91. LOGC_BOOT,
  92. /** @LOGC_COUNT: Number of log categories */
  93. LOGC_COUNT,
  94. /** @LOGC_END: Sentinel value for lists of log categories */
  95. LOGC_END,
  96. /** @LOGC_CONT: Use same category as in previous call */
  97. LOGC_CONT = -1,
  98. };
  99. /* Helper to cast a uclass ID to a log category */
  100. static inline int log_uc_cat(enum uclass_id id)
  101. {
  102. return (enum log_category_t)id;
  103. }
  104. /**
  105. * _log() - Internal function to emit a new log record
  106. *
  107. * @cat: Category of log record (indicating which subsystem generated it)
  108. * @level: Level of log record (indicating its severity)
  109. * @file: File name of file where log record was generated
  110. * @line: Line number in file where log record was generated
  111. * @func: Function where log record was generated
  112. * @fmt: printf() format string for log record
  113. * @...: Optional parameters, according to the format string @fmt
  114. * Return: 0 if log record was emitted, -ve on error
  115. */
  116. int _log(enum log_category_t cat, enum log_level_t level, const char *file,
  117. int line, const char *func, const char *fmt, ...)
  118. __attribute__ ((format (__printf__, 6, 7)));
  119. static inline int _log_nop(enum log_category_t cat, enum log_level_t level,
  120. const char *file, int line, const char *func,
  121. const char *fmt, ...)
  122. __attribute__ ((format (__printf__, 6, 7)));
  123. static inline int _log_nop(enum log_category_t cat, enum log_level_t level,
  124. const char *file, int line, const char *func,
  125. const char *fmt, ...)
  126. {
  127. return 0;
  128. }
  129. /**
  130. * _log_buffer - Internal function to print data buffer in hex and ascii form
  131. *
  132. * @cat: Category of log record (indicating which subsystem generated it)
  133. * @level: Level of log record (indicating its severity)
  134. * @file: File name of file where log record was generated
  135. * @line: Line number in file where log record was generated
  136. * @func: Function where log record was generated
  137. * @addr: Starting address to display at start of line
  138. * @data: pointer to data buffer
  139. * @width: data value width. May be 1, 2, or 4.
  140. * @count: number of values to display
  141. * @linelen: Number of values to print per line; specify 0 for default length
  142. */
  143. int _log_buffer(enum log_category_t cat, enum log_level_t level,
  144. const char *file, int line, const char *func, ulong addr,
  145. const void *data, uint width, uint count, uint linelen);
  146. /* Define this at the top of a file to add a prefix to debug messages */
  147. #ifndef pr_fmt
  148. #define pr_fmt(fmt) fmt
  149. #endif
  150. /* Use a default category if this file does not supply one */
  151. #ifndef LOG_CATEGORY
  152. #define LOG_CATEGORY LOGC_NONE
  153. #endif
  154. /*
  155. * This header may be including when CONFIG_LOG is disabled, in which case
  156. * CONFIG_LOG_MAX_LEVEL is not defined. Add a check for this.
  157. */
  158. #if CONFIG_IS_ENABLED(LOG)
  159. #define _LOG_MAX_LEVEL CONFIG_VAL(LOG_MAX_LEVEL)
  160. #else
  161. #define _LOG_MAX_LEVEL LOGL_INFO
  162. #endif
  163. #define log_emer(_fmt...) log(LOG_CATEGORY, LOGL_EMERG, ##_fmt)
  164. #define log_alert(_fmt...) log(LOG_CATEGORY, LOGL_ALERT, ##_fmt)
  165. #define log_crit(_fmt...) log(LOG_CATEGORY, LOGL_CRIT, ##_fmt)
  166. #define log_err(_fmt...) log(LOG_CATEGORY, LOGL_ERR, ##_fmt)
  167. #define log_warning(_fmt...) log(LOG_CATEGORY, LOGL_WARNING, ##_fmt)
  168. #define log_notice(_fmt...) log(LOG_CATEGORY, LOGL_NOTICE, ##_fmt)
  169. #define log_info(_fmt...) log(LOG_CATEGORY, LOGL_INFO, ##_fmt)
  170. #define log_debug(_fmt...) log(LOG_CATEGORY, LOGL_DEBUG, ##_fmt)
  171. #define log_content(_fmt...) log(LOG_CATEGORY, LOGL_DEBUG_CONTENT, ##_fmt)
  172. #define log_io(_fmt...) log(LOG_CATEGORY, LOGL_DEBUG_IO, ##_fmt)
  173. #define log_cont(_fmt...) log(LOGC_CONT, LOGL_CONT, ##_fmt)
  174. #ifdef LOG_DEBUG
  175. #define _LOG_DEBUG LOGL_FORCE_DEBUG
  176. #else
  177. #define _LOG_DEBUG 0
  178. #endif
  179. #if CONFIG_IS_ENABLED(LOG)
  180. /* Emit a log record if the level is less that the maximum */
  181. #define log(_cat, _level, _fmt, _args...) ({ \
  182. int _l = _level; \
  183. if (_LOG_DEBUG != 0 || _l <= _LOG_MAX_LEVEL) \
  184. _log((enum log_category_t)(_cat), \
  185. (enum log_level_t)(_l | _LOG_DEBUG), __FILE__, \
  186. __LINE__, __func__, \
  187. pr_fmt(_fmt), ##_args); \
  188. })
  189. /* Emit a dump if the level is less that the maximum */
  190. #define log_buffer(_cat, _level, _addr, _data, _width, _count, _linelen) ({ \
  191. int _l = _level; \
  192. if (_LOG_DEBUG != 0 || _l <= _LOG_MAX_LEVEL) \
  193. _log_buffer((enum log_category_t)(_cat), \
  194. (enum log_level_t)(_l | _LOG_DEBUG), __FILE__, \
  195. __LINE__, __func__, _addr, _data, \
  196. _width, _count, _linelen); \
  197. })
  198. #else
  199. /* Note: _LOG_DEBUG != 0 avoids a warning with clang */
  200. #define log(_cat, _level, _fmt, _args...) ({ \
  201. int _l = _level; \
  202. if (_LOG_DEBUG != 0 || _l <= LOGL_INFO || \
  203. (_DEBUG && _l == LOGL_DEBUG)) \
  204. printf(_fmt, ##_args); \
  205. })
  206. #define log_buffer(_cat, _level, _addr, _data, _width, _count, _linelen) ({ \
  207. int _l = _level; \
  208. if (_LOG_DEBUG != 0 || _l <= LOGL_INFO || \
  209. (_DEBUG && _l == LOGL_DEBUG)) \
  210. print_buffer(_addr, _data, _width, _count, _linelen); \
  211. })
  212. #endif
  213. #define log_nop(_cat, _level, _fmt, _args...) ({ \
  214. int _l = _level; \
  215. _log_nop((enum log_category_t)(_cat), _l, __FILE__, __LINE__, \
  216. __func__, pr_fmt(_fmt), ##_args); \
  217. })
  218. #ifdef DEBUG
  219. #define _DEBUG 1
  220. #else
  221. #define _DEBUG 0
  222. #endif
  223. #ifdef CONFIG_SPL_BUILD
  224. #define _SPL_BUILD 1
  225. #else
  226. #define _SPL_BUILD 0
  227. #endif
  228. #if CONFIG_IS_ENABLED(LOG)
  229. #define debug_cond(cond, fmt, args...) \
  230. ({ \
  231. if (cond) \
  232. log(LOG_CATEGORY, \
  233. (enum log_level_t)(LOGL_FORCE_DEBUG | _LOG_DEBUG), \
  234. fmt, ##args); \
  235. })
  236. #else /* _DEBUG */
  237. /*
  238. * Output a debug text when condition "cond" is met. The "cond" should be
  239. * computed by a preprocessor in the best case, allowing for the best
  240. * optimization.
  241. */
  242. #define debug_cond(cond, fmt, args...) \
  243. ({ \
  244. if (cond) \
  245. printf(pr_fmt(fmt), ##args); \
  246. })
  247. #endif /* _DEBUG */
  248. /* Show a message if DEBUG is defined in a file */
  249. #define debug(fmt, args...) \
  250. debug_cond(_DEBUG, fmt, ##args)
  251. /* Show a message if not in SPL */
  252. #define warn_non_spl(fmt, args...) \
  253. debug_cond(!_SPL_BUILD, fmt, ##args)
  254. /*
  255. * An assertion is run-time check done in debug mode only. If DEBUG is not
  256. * defined then it is skipped. If DEBUG is defined and the assertion fails,
  257. * then it calls panic*( which may or may not reset/halt U-Boot (see
  258. * CONFIG_PANIC_HANG), It is hoped that all failing assertions are found
  259. * before release, and after release it is hoped that they don't matter. But
  260. * in any case these failing assertions cannot be fixed with a reset (which
  261. * may just do the same assertion again).
  262. */
  263. void __assert_fail(const char *assertion, const char *file, unsigned int line,
  264. const char *function);
  265. /**
  266. * assert() - assert expression is true
  267. *
  268. * If the expression x evaluates to false and _DEBUG evaluates to true, a panic
  269. * message is written and the system stalls. The value of _DEBUG is set to true
  270. * if DEBUG is defined before including common.h.
  271. *
  272. * The expression x is always executed irrespective of the value of _DEBUG.
  273. *
  274. * @x: expression to test
  275. */
  276. #define assert(x) \
  277. ({ if (!(x) && _DEBUG) \
  278. __assert_fail(#x, __FILE__, __LINE__, __func__); })
  279. /*
  280. * This one actually gets compiled in even without DEBUG. It doesn't include the
  281. * full pathname as it may be huge. Only use this when the user should be
  282. * warning, similar to BUG_ON() in linux.
  283. *
  284. * Return: true if assertion succeeded (condition is true), else false
  285. */
  286. #define assert_noisy(x) \
  287. ({ bool _val = (x); \
  288. if (!_val) \
  289. __assert_fail(#x, "?", __LINE__, __func__); \
  290. _val; \
  291. })
  292. #if CONFIG_IS_ENABLED(LOG) && defined(CONFIG_LOG_ERROR_RETURN)
  293. /*
  294. * Log an error return value, possibly with a message. Usage:
  295. *
  296. * return log_ret(fred_call());
  297. *
  298. * or:
  299. *
  300. * return log_msg_ret("fred failed", fred_call());
  301. */
  302. #define log_ret(_ret) ({ \
  303. int __ret = (_ret); \
  304. if (__ret < 0) \
  305. log(LOG_CATEGORY, LOGL_ERR, "returning err=%d\n", __ret); \
  306. __ret; \
  307. })
  308. #define log_msg_ret(_msg, _ret) ({ \
  309. int __ret = (_ret); \
  310. if (__ret < 0) \
  311. log(LOG_CATEGORY, LOGL_ERR, "%s: returning err=%d\n", _msg, \
  312. __ret); \
  313. __ret; \
  314. })
  315. /*
  316. * Similar to the above, but any non-zero value is consider an error, not just
  317. * values less than 0.
  318. */
  319. #define log_retz(_ret) ({ \
  320. int __ret = (_ret); \
  321. if (__ret) \
  322. log(LOG_CATEGORY, LOGL_ERR, "returning err=%d\n", __ret); \
  323. __ret; \
  324. })
  325. #define log_msg_retz(_msg, _ret) ({ \
  326. int __ret = (_ret); \
  327. if (__ret) \
  328. log(LOG_CATEGORY, LOGL_ERR, "%s: returning err=%d\n", _msg, \
  329. __ret); \
  330. __ret; \
  331. })
  332. #else
  333. /* Non-logging versions of the above which just return the error code */
  334. #define log_ret(_ret) (_ret)
  335. #define log_msg_ret(_msg, _ret) ((void)(_msg), _ret)
  336. #define log_retz(_ret) (_ret)
  337. #define log_msg_retz(_msg, _ret) ((void)(_msg), _ret)
  338. #endif
  339. /** * enum log_rec_flags - Flags for a log record */
  340. enum log_rec_flags {
  341. /** @LOGRECF_FORCE_DEBUG: Force output of debug record */
  342. LOGRECF_FORCE_DEBUG = BIT(0),
  343. /** @LOGRECF_CONT: Continuation of previous log record */
  344. LOGRECF_CONT = BIT(1),
  345. };
  346. /**
  347. * struct log_rec - a single log record
  348. *
  349. * Holds information about a single record in the log
  350. *
  351. * Members marked as 'not allocated' are stored as pointers and the caller is
  352. * responsible for making sure that the data pointed to is not overwritten.
  353. * Members marked as 'allocated' are allocated (e.g. via strdup()) by the log
  354. * system.
  355. *
  356. * TODO(sjg@chromium.org): Compress this struct down a bit to reduce space, e.g.
  357. * a single u32 for cat, level, line and force_debug
  358. *
  359. * @cat: Category, representing a uclass or part of U-Boot
  360. * @level: Severity level, less severe is higher
  361. * @line: Line number where the log record was generated
  362. * @flags: Flags for log record (enum log_rec_flags)
  363. * @file: Name of file where the log record was generated (not allocated)
  364. * @func: Function where the log record was generated (not allocated)
  365. * @msg: Log message (allocated)
  366. */
  367. struct log_rec {
  368. enum log_category_t cat;
  369. enum log_level_t level;
  370. u16 line;
  371. u8 flags;
  372. const char *file;
  373. const char *func;
  374. const char *msg;
  375. };
  376. struct log_device;
  377. enum log_device_flags {
  378. LOGDF_ENABLE = BIT(0), /* Device is enabled */
  379. };
  380. /**
  381. * struct log_driver - a driver which accepts and processes log records
  382. *
  383. * @name: Name of driver
  384. * @emit: Method to call to emit a log record via this device
  385. * @flags: Initial value for flags (use LOGDF_ENABLE to enable on start-up)
  386. */
  387. struct log_driver {
  388. const char *name;
  389. /**
  390. * @emit: emit a log record
  391. *
  392. * Called by the log system to pass a log record to a particular driver
  393. * for processing. The filter is checked before calling this function.
  394. */
  395. int (*emit)(struct log_device *ldev, struct log_rec *rec);
  396. unsigned short flags;
  397. };
  398. /**
  399. * struct log_device - an instance of a log driver
  400. *
  401. * Since drivers are set up at build-time we need to have a separate device for
  402. * the run-time aspects of drivers (currently just a list of filters to apply
  403. * to records send to this device).
  404. *
  405. * @next_filter_num: Sequence number of next filter filter added (0=no filters
  406. * yet). This increments with each new filter on the device, but never
  407. * decrements
  408. * @flags: Flags for this filter (enum log_device_flags)
  409. * @drv: Pointer to driver for this device
  410. * @filter_head: List of filters for this device
  411. * @sibling_node: Next device in the list of all devices
  412. */
  413. struct log_device {
  414. unsigned short next_filter_num;
  415. unsigned short flags;
  416. struct log_driver *drv;
  417. struct list_head filter_head;
  418. struct list_head sibling_node;
  419. };
  420. enum {
  421. LOGF_MAX_CATEGORIES = 5, /* maximum categories per filter */
  422. };
  423. /**
  424. * enum log_filter_flags - Flags which modify a filter
  425. */
  426. enum log_filter_flags {
  427. /** @LOGFF_HAS_CAT: Filter has a category list */
  428. LOGFF_HAS_CAT = 1 << 0,
  429. /** @LOGFF_DENY: Filter denies matching messages */
  430. LOGFF_DENY = 1 << 1,
  431. /** @LOGFF_LEVEL_MIN: Filter's level is a minimum, not a maximum */
  432. LOGFF_LEVEL_MIN = 1 << 2,
  433. };
  434. /**
  435. * struct log_filter - criteria to filter out log messages
  436. *
  437. * If a message matches all criteria, then it is allowed. If LOGFF_DENY is set,
  438. * then it is denied instead.
  439. *
  440. * @filter_num: Sequence number of this filter. This is returned when adding a
  441. * new filter, and must be provided when removing a previously added
  442. * filter.
  443. * @flags: Flags for this filter (``LOGFF_...``)
  444. * @cat_list: List of categories to allow (terminated by %LOGC_END). If empty
  445. * then all categories are permitted. Up to %LOGF_MAX_CATEGORIES entries
  446. * can be provided
  447. * @level: Maximum (or minimum, if %LOGFF_MIN_LEVEL) log level to allow
  448. * @file_list: List of files to allow, separated by comma. If NULL then all
  449. * files are permitted
  450. * @sibling_node: Next filter in the list of filters for this log device
  451. */
  452. struct log_filter {
  453. int filter_num;
  454. int flags;
  455. enum log_category_t cat_list[LOGF_MAX_CATEGORIES];
  456. enum log_level_t level;
  457. const char *file_list;
  458. struct list_head sibling_node;
  459. };
  460. #define LOG_DRIVER(_name) \
  461. ll_entry_declare(struct log_driver, _name, log_driver)
  462. /* Get a pointer to a given driver */
  463. #define LOG_GET_DRIVER(__name) \
  464. ll_entry_get(struct log_driver, __name, log_driver)
  465. /**
  466. * log_get_cat_name() - Get the name of a category
  467. *
  468. * @cat: Category to look up
  469. * Return: category name (which may be a uclass driver name) if found, or
  470. * "<invalid>" if invalid, or "<missing>" if not found. All error
  471. * responses begin with '<'.
  472. */
  473. const char *log_get_cat_name(enum log_category_t cat);
  474. /**
  475. * log_get_cat_by_name() - Look up a category by name
  476. *
  477. * @name: Name to look up
  478. * Return: Category, or %LOGC_NONE if not found
  479. */
  480. enum log_category_t log_get_cat_by_name(const char *name);
  481. /**
  482. * log_get_level_name() - Get the name of a log level
  483. *
  484. * @level: Log level to look up
  485. * Return: Log level name (in ALL CAPS)
  486. */
  487. const char *log_get_level_name(enum log_level_t level);
  488. /**
  489. * log_get_level_by_name() - Look up a log level by name
  490. *
  491. * @name: Name to look up
  492. * Return: Log level, or %LOGL_NONE if not found
  493. */
  494. enum log_level_t log_get_level_by_name(const char *name);
  495. /**
  496. * log_device_find_by_name() - Look up a log device by its driver's name
  497. *
  498. * @drv_name: Name of the driver
  499. * Return: the log device, or %NULL if not found
  500. */
  501. struct log_device *log_device_find_by_name(const char *drv_name);
  502. /**
  503. * log_has_cat() - check if a log category exists within a list
  504. *
  505. * @cat_list: List of categories to check, at most %LOGF_MAX_CATEGORIES entries
  506. * long, terminated by %LC_END if fewer
  507. * @cat: Category to search for
  508. *
  509. * Return: ``true`` if @cat is in @cat_list, else ``false``
  510. */
  511. bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat);
  512. /**
  513. * log_has_file() - check if a file is with a list
  514. *
  515. * @file_list: List of files to check, separated by comma
  516. * @file: File to check for. This string is matched against the end of each
  517. * file in the list, i.e. ignoring any preceding path. The list is
  518. * intended to consist of relative pathnames, e.g. common/main.c,cmd/log.c
  519. *
  520. * Return: ``true`` if @file is in @file_list, else ``false``
  521. */
  522. bool log_has_file(const char *file_list, const char *file);
  523. /* Log format flags (bit numbers) for gd->log_fmt. See log_fmt_chars */
  524. enum log_fmt {
  525. LOGF_CAT = 0,
  526. LOGF_LEVEL,
  527. LOGF_FILE,
  528. LOGF_LINE,
  529. LOGF_FUNC,
  530. LOGF_MSG,
  531. LOGF_COUNT,
  532. LOGF_ALL = 0x3f,
  533. };
  534. /* Handle the 'log test' command */
  535. int do_log_test(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
  536. /**
  537. * log_add_filter_flags() - Add a new filter to a log device, specifying flags
  538. *
  539. * @drv_name: Driver name to add the filter to (since each driver only has a
  540. * single device)
  541. * @flags: Flags for this filter (``LOGFF_...``)
  542. * @cat_list: List of categories to allow (terminated by %LOGC_END). If empty
  543. * then all categories are permitted. Up to %LOGF_MAX_CATEGORIES entries
  544. * can be provided
  545. * @level: Maximum (or minimum, if %LOGFF_LEVEL_MIN) log level to allow
  546. * @file_list: List of files to allow, separated by comma. If NULL then all
  547. * files are permitted
  548. * Return:
  549. * the sequence number of the new filter (>=0) if the filter was added, or a
  550. * -ve value on error
  551. */
  552. int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
  553. enum log_level_t level, const char *file_list,
  554. int flags);
  555. /**
  556. * log_add_filter() - Add a new filter to a log device
  557. *
  558. * @drv_name: Driver name to add the filter to (since each driver only has a
  559. * single device)
  560. * @cat_list: List of categories to allow (terminated by %LOGC_END). If empty
  561. * then all categories are permitted. Up to %LOGF_MAX_CATEGORIES entries
  562. * can be provided
  563. * @max_level: Maximum log level to allow
  564. * @file_list: List of files to allow, separated by comma. If %NULL then all
  565. * files are permitted
  566. * Return:
  567. * the sequence number of the new filter (>=0) if the filter was added, or a
  568. * -ve value on error
  569. */
  570. static inline int log_add_filter(const char *drv_name,
  571. enum log_category_t cat_list[],
  572. enum log_level_t max_level,
  573. const char *file_list)
  574. {
  575. return log_add_filter_flags(drv_name, cat_list, max_level, file_list,
  576. 0);
  577. }
  578. /**
  579. * log_remove_filter() - Remove a filter from a log device
  580. *
  581. * @drv_name: Driver name to remove the filter from (since each driver only has
  582. * a single device)
  583. * @filter_num: Filter number to remove (as returned by log_add_filter())
  584. * Return:
  585. * 0 if the filter was removed, -%ENOENT if either the driver or the filter
  586. * number was not found
  587. */
  588. int log_remove_filter(const char *drv_name, int filter_num);
  589. /**
  590. * log_device_set_enable() - Enable or disable a log device
  591. *
  592. * Devices are referenced by their driver, so use LOG_GET_DRIVER(name) to pass
  593. * the driver to this function. For example if the driver is declared with
  594. * LOG_DRIVER(wibble) then pass LOG_GET_DRIVER(wibble) here.
  595. *
  596. * @drv: Driver of device to enable
  597. * @enable: true to enable, false to disable
  598. * @return 0 if OK, -ENOENT if the driver was not found
  599. */
  600. int log_device_set_enable(struct log_driver *drv, bool enable);
  601. #if CONFIG_IS_ENABLED(LOG)
  602. /**
  603. * log_init() - Set up the log system ready for use
  604. *
  605. * Return: 0 if OK, -%ENOMEM if out of memory
  606. */
  607. int log_init(void);
  608. #else
  609. static inline int log_init(void)
  610. {
  611. return 0;
  612. }
  613. #endif
  614. /**
  615. * log_get_default_format() - get default log format
  616. *
  617. * The default log format is configurable via
  618. * %CONFIG_LOGF_FILE, %CONFIG_LOGF_LINE, and %CONFIG_LOGF_FUNC.
  619. *
  620. * Return: default log format
  621. */
  622. static inline int log_get_default_format(void)
  623. {
  624. return BIT(LOGF_MSG) |
  625. (IS_ENABLED(CONFIG_LOGF_FILE) ? BIT(LOGF_FILE) : 0) |
  626. (IS_ENABLED(CONFIG_LOGF_LINE) ? BIT(LOGF_LINE) : 0) |
  627. (IS_ENABLED(CONFIG_LOGF_FUNC) ? BIT(LOGF_FUNC) : 0);
  628. }
  629. #endif