logger.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. \file logger.h
  20. \brief Support for logging to various files, console and syslog
  21. Configuration in file logger.conf
  22. */
  23. #ifndef _ASTERISK_LOGGER_H
  24. #define _ASTERISK_LOGGER_H
  25. #include "asterisk/options.h" /* need option_debug */
  26. #if defined(__cplusplus) || defined(c_plusplus)
  27. extern "C" {
  28. #endif
  29. #define EVENTLOG "event_log"
  30. #define QUEUELOG "queue_log"
  31. #define DEBUG_M(a) { \
  32. a; \
  33. }
  34. #define VERBOSE_PREFIX_1 " "
  35. #define VERBOSE_PREFIX_2 " == "
  36. #define VERBOSE_PREFIX_3 " -- "
  37. #define VERBOSE_PREFIX_4 " > "
  38. #define AST_CALLID_BUFFER_LENGTH 13
  39. enum ast_logger_results {
  40. AST_LOGGER_SUCCESS = 0, /*!< Log channel was created or deleted successfully*/
  41. AST_LOGGER_FAILURE = 1, /*!< Log channel already exists for create or doesn't exist for deletion of log channel */
  42. AST_LOGGER_DECLINE = -1, /*!< Log channel request was not accepted */
  43. AST_LOGGER_ALLOC_ERROR = -2 /*!< filename allocation error */
  44. };
  45. /*! \brief Used for sending a log message
  46. This is the standard logger function. Probably the only way you will invoke it would be something like this:
  47. ast_log(AST_LOG_WHATEVER, "Problem with the %s Captain. We should get some more. Will %d be enough?\n", "flux capacitor", 10);
  48. where WHATEVER is one of ERROR, DEBUG, EVENT, NOTICE, or WARNING depending
  49. on which log you wish to output to. These are implemented as macros, that
  50. will provide the function with the needed arguments.
  51. \param level Type of log event
  52. \param file Will be provided by the AST_LOG_* macro
  53. \param line Will be provided by the AST_LOG_* macro
  54. \param function Will be provided by the AST_LOG_* macro
  55. \param fmt This is what is important. The format is the same as your favorite breed of printf. You know how that works, right? :-)
  56. */
  57. void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
  58. __attribute__((format(printf, 5, 6)));
  59. /*!
  60. * \brief Used for sending a log message with protection against recursion.
  61. *
  62. * \note This function should be used by all error messages that might be directly
  63. * or indirectly caused by logging.
  64. *
  65. * \see ast_log for documentation on the parameters.
  66. */
  67. void ast_log_safe(int level, const char *file, int line, const char *function, const char *fmt, ...)
  68. __attribute__((format(printf, 5, 6)));
  69. /* XXX needs documentation */
  70. struct ast_callid;
  71. /*! \brief Used for sending a log message with a known call_id
  72. This is a modified logger function which is functionally identical to the above logger function,
  73. it just include a call_id argument as well. If NULL is specified here, no attempt will be made to
  74. join the log message with a call_id.
  75. \param level Type of log event
  76. \param file Will be provided by the AST_LOG_* macro
  77. \param line Will be provided by the AST_LOG_* macro
  78. \param function Will be provided by the AST_LOG_* macro
  79. \param callid This is the ast_callid that is associated with the log message. May be NULL.
  80. \param fmt This is what is important. The format is the same as your favorite breed of printf. You know how that works, right? :-)
  81. */
  82. void ast_log_callid(int level, const char *file, int line, const char *function, struct ast_callid *callid, const char *fmt, ...)
  83. __attribute__((format(printf, 6, 7)));
  84. /*!
  85. * \brief Retrieve the existing log channels
  86. * \param logentry A callback to an updater function
  87. * \param data Data passed into the callback for manipulation
  88. *
  89. * For each of the logging channels, logentry will be executed with the
  90. * channel file name, log type, status of the log, and configuration levels.
  91. *
  92. * \retval 0 on success
  93. * \retval 1 on failure
  94. * \retval -2 on allocation error
  95. */
  96. int ast_logger_get_channels(int (*logentry)(const char *channel, const char *type,
  97. const char *status, const char *configuration, void *data), void *data);
  98. /*!
  99. * \brief Create a log channel
  100. *
  101. * \param log_channel Log channel to create
  102. * \param components Logging config levels to add to the log channel
  103. */
  104. int ast_logger_create_channel(const char *log_channel, const char *components);
  105. /*!
  106. * \brief Delete the specified log channel
  107. *
  108. * \param log_channel The log channel to delete
  109. */
  110. int ast_logger_remove_channel(const char *log_channel);
  111. /*!
  112. * \brief Log a backtrace of the current thread's execution stack to the Asterisk log
  113. */
  114. void ast_log_backtrace(void);
  115. /*! \brief Reload logger without rotating log files */
  116. int logger_reload(void);
  117. /*! \brief Reload logger while rotating log files */
  118. int ast_logger_rotate(void);
  119. /*!
  120. * \brief Rotate the specified log channel.
  121. *
  122. * \param log_channel The log channel to rotate
  123. */
  124. int ast_logger_rotate_channel(const char *log_channel);
  125. void __attribute__((format(printf, 5, 6))) ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...);
  126. /*!
  127. * \brief Send a verbose message (based on verbose level)
  128. *
  129. * \details This works like ast_log, but prints verbose messages to the console depending on verbosity level set.
  130. *
  131. * ast_verbose(VERBOSE_PREFIX_3 "Whatever %s is happening\n", "nothing");
  132. *
  133. * This will print the message to the console if the verbose level is set to a level >= 3
  134. *
  135. * Note the absence of a comma after the VERBOSE_PREFIX_3. This is important.
  136. * VERBOSE_PREFIX_1 through VERBOSE_PREFIX_4 are defined.
  137. *
  138. * \version 11 added level parameter
  139. */
  140. void __attribute__((format(printf, 5, 6))) __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...);
  141. /*!
  142. * \brief Send a verbose message (based on verbose level) with deliberately specified callid
  143. *
  144. * \details just like __ast_verbose, only __ast_verbose_callid allows you to specify which callid is being used
  145. * for the log without needing to bind it to a thread. NULL is a valid argument for this function and will
  146. * allow you to specify that a log will never display a call id even when there is a call id bound to the
  147. * thread.
  148. */
  149. void __attribute__((format(printf, 6, 7))) __ast_verbose_callid(const char *file, int line, const char *func, int level, struct ast_callid *callid, const char *fmt, ...);
  150. #define ast_verbose(...) __ast_verbose(__FILE__, __LINE__, __PRETTY_FUNCTION__, -1, __VA_ARGS__)
  151. #define ast_verbose_callid(callid, ...) __ast_verbose_callid(__FILE__, __LINE__, __PRETTY_FUNCTION__, -1, callid, __VA_ARGS__)
  152. void __attribute__((format(printf, 6, 0))) __ast_verbose_ap(const char *file, int line, const char *func, int level, struct ast_callid *callid, const char *fmt, va_list ap);
  153. void __attribute__((format(printf, 2, 3))) ast_child_verbose(int level, const char *fmt, ...);
  154. int ast_register_verbose(void (*verboser)(const char *string)) attribute_warn_unused_result;
  155. int ast_unregister_verbose(void (*verboser)(const char *string)) attribute_warn_unused_result;
  156. /*
  157. * These gymnastics are due to platforms which designate char as unsigned by
  158. * default. Level is the negative character -- offset by 1, because \0 is
  159. * the string terminator.
  160. */
  161. #define VERBOSE_MAGIC2LEVEL(x) (((char) -*(signed char *) (x)) - 1)
  162. #define VERBOSE_HASMAGIC(x) (*(signed char *) (x) < 0)
  163. void ast_console_puts(const char *string);
  164. /*!
  165. * \brief log the string to the console, and all attached
  166. * console clients
  167. * \version 1.6.1 added level parameter
  168. */
  169. void ast_console_puts_mutable(const char *string, int level);
  170. void ast_console_toggle_mute(int fd, int silent);
  171. /*!
  172. * \brief enables or disables logging of a specified level to the console
  173. * fd specifies the index of the console receiving the level change
  174. * level specifies the index of the logging level being toggled
  175. * state indicates whether logging will be on or off (0 for off, 1 for on)
  176. */
  177. void ast_console_toggle_loglevel(int fd, int level, int state);
  178. /* Note: The AST_LOG_* macros below are the same as
  179. * the LOG_* macros and are intended to eventually replace
  180. * the LOG_* macros to avoid name collisions with the syslog(3)
  181. * log levels. However, please do NOT remove
  182. * the LOG_* macros from the source since these may be still
  183. * needed for third-party modules
  184. */
  185. #define _A_ __FILE__, __LINE__, __PRETTY_FUNCTION__
  186. #ifdef LOG_DEBUG
  187. #undef LOG_DEBUG
  188. #endif
  189. #define __LOG_DEBUG 0
  190. #define LOG_DEBUG __LOG_DEBUG, _A_
  191. #ifdef AST_LOG_DEBUG
  192. #undef AST_LOG_DEBUG
  193. #endif
  194. #define AST_LOG_DEBUG __LOG_DEBUG, _A_
  195. #ifdef LOG_NOTICE
  196. #undef LOG_NOTICE
  197. #endif
  198. #define __LOG_NOTICE 2
  199. #define LOG_NOTICE __LOG_NOTICE, _A_
  200. #ifdef AST_LOG_NOTICE
  201. #undef AST_LOG_NOTICE
  202. #endif
  203. #define AST_LOG_NOTICE __LOG_NOTICE, _A_
  204. #ifdef LOG_WARNING
  205. #undef LOG_WARNING
  206. #endif
  207. #define __LOG_WARNING 3
  208. #define LOG_WARNING __LOG_WARNING, _A_
  209. #ifdef AST_LOG_WARNING
  210. #undef AST_LOG_WARNING
  211. #endif
  212. #define AST_LOG_WARNING __LOG_WARNING, _A_
  213. #ifdef LOG_ERROR
  214. #undef LOG_ERROR
  215. #endif
  216. #define __LOG_ERROR 4
  217. #define LOG_ERROR __LOG_ERROR, _A_
  218. #ifdef AST_LOG_ERROR
  219. #undef AST_LOG_ERROR
  220. #endif
  221. #define AST_LOG_ERROR __LOG_ERROR, _A_
  222. #ifdef LOG_VERBOSE
  223. #undef LOG_VERBOSE
  224. #endif
  225. #define __LOG_VERBOSE 5
  226. #define LOG_VERBOSE __LOG_VERBOSE, _A_
  227. #ifdef AST_LOG_VERBOSE
  228. #undef AST_LOG_VERBOSE
  229. #endif
  230. #define AST_LOG_VERBOSE __LOG_VERBOSE, _A_
  231. #ifdef LOG_DTMF
  232. #undef LOG_DTMF
  233. #endif
  234. #define __LOG_DTMF 6
  235. #define LOG_DTMF __LOG_DTMF, _A_
  236. #ifdef AST_LOG_DTMF
  237. #undef AST_LOG_DTMF
  238. #endif
  239. #define AST_LOG_DTMF __LOG_DTMF, _A_
  240. #define NUMLOGLEVELS 32
  241. /*!
  242. * \brief Get the debug level for a module
  243. * \param module the name of module
  244. * \return the debug level
  245. */
  246. unsigned int ast_debug_get_by_module(const char *module);
  247. /*!
  248. * \brief Get the verbose level for a module
  249. * \param module the name of module
  250. * \return the verbose level
  251. * \version 11.0.0 deprecated
  252. */
  253. unsigned int ast_verbose_get_by_module(const char *module) __attribute__((deprecated));
  254. /*!
  255. * \brief Register a new logger level
  256. * \param name The name of the level to be registered
  257. * \retval -1 if an error occurs
  258. * \retval non-zero level to be used with ast_log for sending messages to this level
  259. * \since 1.8
  260. */
  261. int ast_logger_register_level(const char *name);
  262. /*!
  263. * \brief Unregister a previously registered logger level
  264. * \param name The name of the level to be unregistered
  265. * \return nothing
  266. * \since 1.8
  267. */
  268. void ast_logger_unregister_level(const char *name);
  269. /*!
  270. * \brief Get the logger configured date format
  271. *
  272. * \retval The date format string
  273. *
  274. * \since 13.0.0
  275. */
  276. const char *ast_logger_get_dateformat(void);
  277. /*!
  278. * \brief factory function to create a new uniquely identifying callid.
  279. *
  280. * \retval ast_callid struct pointer containing the call id
  281. *
  282. * \note The newly created callid will be referenced upon creation and this function should be
  283. * paired with a call to ast_callid_unref()
  284. */
  285. struct ast_callid *ast_create_callid(void);
  286. /*!
  287. * \brief extracts the callerid from the thread
  288. *
  289. * \retval ast_callid reference to call_id related to the thread
  290. * \retval NULL if no call_id is present in the thread
  291. *
  292. * This reference must be unreffed before it loses scope to prevent memory leaks.
  293. */
  294. struct ast_callid *ast_read_threadstorage_callid(void);
  295. /*!
  296. * \brief Increase callid reference count
  297. *
  298. * \param c the ast_callid
  299. *
  300. * \retval c always
  301. */
  302. #define ast_callid_ref(c) ({ ao2_ref(c, +1); (c); })
  303. /*!
  304. * \brief Decrease callid reference count
  305. *
  306. * \param c the ast_callid
  307. *
  308. * \retval NULL always
  309. */
  310. #define ast_callid_unref(c) ({ ao2_ref(c, -1); (struct ast_callid *) (NULL); })
  311. /*!
  312. * \brief Cleanup a callid reference (NULL safe ao2 unreference)
  313. *
  314. * \param c the ast_callid
  315. *
  316. * \retval NULL always
  317. */
  318. #define ast_callid_cleanup(c) ({ ao2_cleanup(c); (struct ast_callid *) (NULL); })
  319. /*!
  320. * \brief Sets what is stored in the thread storage to the given
  321. * callid if it does not match what is already there.
  322. *
  323. * \retval 0 - success
  324. * \retval non-zero - failure
  325. */
  326. int ast_callid_threadassoc_change(struct ast_callid *callid);
  327. /*!
  328. * \brief Adds a known callid to thread storage of the calling thread
  329. *
  330. * \retval 0 - success
  331. * \retval non-zero - failure
  332. */
  333. int ast_callid_threadassoc_add(struct ast_callid *callid);
  334. /*!
  335. * \brief Removes callid from thread storage of the calling thread
  336. *
  337. * \retval 0 - success
  338. * \retval non-zero - failure
  339. */
  340. int ast_callid_threadassoc_remove(void);
  341. /*!
  342. * \brief Checks thread storage for a callid and stores a reference if it exists.
  343. * If not, then a new one will be created, bound to the thread, and a reference
  344. * to it will be stored.
  345. *
  346. * \param callid pointer to struct pointer used to store the referenced callid
  347. * \retval 0 - callid was found
  348. * \retval 1 - callid was created
  349. * \retval -1 - the function failed somehow (presumably memory problems)
  350. */
  351. int ast_callid_threadstorage_auto(struct ast_callid **callid);
  352. /*!
  353. * \brief Use in conjunction with ast_callid_threadstorage_auto. Cleans up the
  354. * references and if the callid was created by threadstorage_auto, unbinds
  355. * the callid from the threadstorage
  356. * \param callid The callid set by ast_callid_threadstorage_auto
  357. * \param callid_created The integer returned through ast_callid_threadstorage_auto
  358. */
  359. void ast_callid_threadstorage_auto_clean(struct ast_callid *callid, int callid_created);
  360. /*!
  361. * \brief copy a string representation of the callid into a target string
  362. *
  363. * \param buffer destination of callid string (should be able to store 13 characters or more)
  364. * \param buffer_size maximum writable length of the string (Less than 13 will result in truncation)
  365. * \param callid Callid for which string is being requested
  366. */
  367. void ast_callid_strnprint(char *buffer, size_t buffer_size, struct ast_callid *callid);
  368. /*!
  369. * \brief Send a log message to a dynamically registered log level
  370. * \param level The log level to send the message to
  371. *
  372. * Like ast_log, the log message may include printf-style formats, and
  373. * the data for these must be provided as additional parameters after
  374. * the log message.
  375. *
  376. * \return nothing
  377. * \since 1.8
  378. */
  379. #define ast_log_dynamic_level(level, ...) ast_log(level, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__)
  380. #define DEBUG_ATLEAST(level) \
  381. (option_debug >= (level) \
  382. || (ast_opt_dbg_module \
  383. && ((int)ast_debug_get_by_module(AST_MODULE) >= (level) \
  384. || (int)ast_debug_get_by_module(__FILE__) >= (level))))
  385. /*!
  386. * \brief Log a DEBUG message
  387. * \param level The minimum value of option_debug for this message
  388. * to get logged
  389. */
  390. #define ast_debug(level, ...) \
  391. do { \
  392. if (DEBUG_ATLEAST(level)) { \
  393. ast_log(AST_LOG_DEBUG, __VA_ARGS__); \
  394. } \
  395. } while (0)
  396. extern int ast_verb_sys_level;
  397. #define VERBOSITY_ATLEAST(level) ((level) <= ast_verb_sys_level)
  398. #define ast_verb(level, ...) \
  399. do { \
  400. if (VERBOSITY_ATLEAST(level) ) { \
  401. __ast_verbose(__FILE__, __LINE__, __PRETTY_FUNCTION__, level, __VA_ARGS__); \
  402. } \
  403. } while (0)
  404. #define ast_verb_callid(level, callid, ...) \
  405. do { \
  406. if (VERBOSITY_ATLEAST(level) ) { \
  407. __ast_verbose_callid(__FILE__, __LINE__, __PRETTY_FUNCTION__, level, callid, __VA_ARGS__); \
  408. } \
  409. } while (0)
  410. /*!
  411. * \brief Re-evaluate the system max verbosity level (ast_verb_sys_level).
  412. *
  413. * \return Nothing
  414. */
  415. void ast_verb_update(void);
  416. /*!
  417. * \brief Register this thread's console verbosity level pointer.
  418. *
  419. * \param level Where the verbose level value is.
  420. *
  421. * \return Nothing
  422. */
  423. void ast_verb_console_register(int *level);
  424. /*!
  425. * \brief Unregister this thread's console verbosity level.
  426. *
  427. * \return Nothing
  428. */
  429. void ast_verb_console_unregister(void);
  430. /*!
  431. * \brief Get this thread's console verbosity level.
  432. *
  433. * \retval verbosity level of the console.
  434. */
  435. int ast_verb_console_get(void);
  436. /*!
  437. * \brief Set this thread's console verbosity level.
  438. *
  439. * \param verb_level New level to set.
  440. *
  441. * \return Nothing
  442. */
  443. void ast_verb_console_set(int verb_level);
  444. /*!
  445. * \brief Test if logger is initialized
  446. *
  447. * \retval true if the logger is initialized
  448. */
  449. int ast_is_logger_initialized(void);
  450. /*!
  451. * \brief Set the maximum number of messages allowed in the processing queue
  452. *
  453. * \param queue_limit
  454. *
  455. * \return Nothing
  456. */
  457. void ast_logger_set_queue_limit(int queue_limit);
  458. /*!
  459. * \brief Get the maximum number of messages allowed in the processing queue
  460. *
  461. * \return Queue limit
  462. */
  463. int ast_logger_get_queue_limit(void);
  464. #if defined(__cplusplus) || defined(c_plusplus)
  465. }
  466. #endif
  467. #endif /* _ASTERISK_LOGGER_H */