log.h 20 KB

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