log.h 21 KB

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