log.h 20 KB

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