2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 \brief Support for logging to various files, console and syslog
22 Configuration in file logger.conf
25 #ifndef _ASTERISK_LOGGER_H
26 #define _ASTERISK_LOGGER_H
28 #include "asterisk/options.h" /* need option_debug */
30 #if defined(__cplusplus) || defined(c_plusplus)
34 #define EVENTLOG "event_log"
35 #define QUEUELOG "queue_log"
37 #define DEBUG_M(a) { \
41 #define VERBOSE_PREFIX_1 " "
42 #define VERBOSE_PREFIX_2 " == "
43 #define VERBOSE_PREFIX_3 " -- "
44 #define VERBOSE_PREFIX_4 " > "
46 /*! \brief Used for sending a log message
47 This is the standard logger function. Probably the only way you will invoke it would be something like this:
48 ast_log(AST_LOG_WHATEVER, "Problem with the %s Captain. We should get some more. Will %d be enough?\n", "flux capacitor", 10);
49 where WHATEVER is one of ERROR, DEBUG, EVENT, NOTICE, or WARNING depending
50 on which log you wish to output to. These are implemented as macros, that
51 will provide the function with the needed arguments.
53 \param level Type of log event
54 \param file Will be provided by the AST_LOG_* macro
55 \param line Will be provided by the AST_LOG_* macro
56 \param function Will be provided by the AST_LOG_* macro
57 \param fmt This is what is important. The format is the same as your favorite breed of printf. You know how that works, right? :-)
60 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
61 __attribute__((format(printf, 5, 6)));
63 void ast_backtrace(void);
65 /*! \brief Reload logger without rotating log files */
66 int logger_reload(void);
68 void __attribute__((format(printf, 5, 6))) ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...);
70 /*! Send a verbose message (based on verbose level)
71 * \brief This works like ast_log, but prints verbose messages to the console depending on verbosity level set.
72 * ast_verbose(VERBOSE_PREFIX_3 "Whatever %s is happening\n", "nothing");
73 * This will print the message to the console if the verbose level is set to a level >= 3
74 * Note the absence of a comma after the VERBOSE_PREFIX_3. This is important.
75 * VERBOSE_PREFIX_1 through VERBOSE_PREFIX_4 are defined.
76 * \version 11 added level parameter
78 void __attribute__((format(printf, 5, 6))) __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...);
80 #define ast_verbose(...) __ast_verbose(__FILE__, __LINE__, __PRETTY_FUNCTION__, -1, __VA_ARGS__)
82 void __attribute__((format(printf, 5, 0))) __ast_verbose_ap(const char *file, int line, const char *func, int level, const char *fmt, va_list ap);
84 #define ast_verbose_ap(fmt, ap) __ast_verbose_ap(__FILE__, __LINE__, __PRETTY_FUNCTION__, -1, fmt, ap)
86 void __attribute__((format(printf, 2, 3))) ast_child_verbose(int level, const char *fmt, ...);
88 int ast_register_verbose(void (*verboser)(const char *string)) attribute_warn_unused_result;
89 int ast_unregister_verbose(void (*verboser)(const char *string)) attribute_warn_unused_result;
91 void ast_console_puts(const char *string);
94 * \brief log the string to the console, and all attached
96 * \version 1.6.1 added level parameter
98 void ast_console_puts_mutable(const char *string, int level);
99 void ast_console_toggle_mute(int fd, int silent);
102 * \brief enables or disables logging of a specified level to the console
103 * fd specifies the index of the console receiving the level change
104 * level specifies the index of the logging level being toggled
105 * state indicates whether logging will be on or off (0 for off, 1 for on)
107 void ast_console_toggle_loglevel(int fd, int level, int state);
109 /* Note: The AST_LOG_* macros below are the same as
110 * the LOG_* macros and are intended to eventually replace
111 * the LOG_* macros to avoid name collisions with the syslog(3)
112 * log levels. However, please do NOT remove
113 * the LOG_* macros from the source since these may be still
114 * needed for third-party modules
117 #define _A_ __FILE__, __LINE__, __PRETTY_FUNCTION__
122 #define __LOG_DEBUG 0
123 #define LOG_DEBUG __LOG_DEBUG, _A_
128 #define AST_LOG_DEBUG __LOG_DEBUG, _A_
133 #define __LOG_NOTICE 2
134 #define LOG_NOTICE __LOG_NOTICE, _A_
136 #ifdef AST_LOG_NOTICE
137 #undef AST_LOG_NOTICE
139 #define AST_LOG_NOTICE __LOG_NOTICE, _A_
144 #define __LOG_WARNING 3
145 #define LOG_WARNING __LOG_WARNING, _A_
147 #ifdef AST_LOG_WARNING
148 #undef AST_LOG_WARNING
150 #define AST_LOG_WARNING __LOG_WARNING, _A_
155 #define __LOG_ERROR 4
156 #define LOG_ERROR __LOG_ERROR, _A_
161 #define AST_LOG_ERROR __LOG_ERROR, _A_
166 #define __LOG_VERBOSE 5
167 #define LOG_VERBOSE __LOG_VERBOSE, _A_
169 #ifdef AST_LOG_VERBOSE
170 #undef AST_LOG_VERBOSE
172 #define AST_LOG_VERBOSE __LOG_VERBOSE, _A_
178 #define LOG_DTMF __LOG_DTMF, _A_
183 #define AST_LOG_DTMF __LOG_DTMF, _A_
185 #define NUMLOGLEVELS 32
188 * \brief Get the debug level for a module
189 * \param module the name of module
190 * \return the debug level
192 unsigned int ast_debug_get_by_module(const char *module);
195 * \brief Get the verbose level for a module
196 * \param module the name of module
197 * \return the verbose level
199 unsigned int ast_verbose_get_by_module(const char *module);
202 * \brief Register a new logger level
203 * \param name The name of the level to be registered
204 * \retval -1 if an error occurs
205 * \retval non-zero level to be used with ast_log for sending messages to this level
208 int ast_logger_register_level(const char *name);
211 * \brief Unregister a previously registered logger level
212 * \param name The name of the level to be unregistered
216 void ast_logger_unregister_level(const char *name);
219 * \brief Send a log message to a dynamically registered log level
220 * \param level The log level to send the message to
222 * Like ast_log, the log message may include printf-style formats, and
223 * the data for these must be provided as additional parameters after
230 #define ast_log_dynamic_level(level, ...) ast_log(level, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__)
233 * \brief Log a DEBUG message
234 * \param level The minimum value of option_debug for this message
237 #define ast_debug(level, ...) do { \
238 if (option_debug >= (level) || (ast_opt_dbg_module && ast_debug_get_by_module(AST_MODULE) >= (level)) ) \
239 ast_log(AST_LOG_DEBUG, __VA_ARGS__); \
242 #define ast_verb(level, ...) __ast_verbose(__FILE__, __LINE__, __PRETTY_FUNCTION__, level, __VA_ARGS__)
244 #ifndef _LOGGER_BACKTRACE_H
245 #define _LOGGER_BACKTRACE_H
247 #define AST_MAX_BT_FRAMES 32
250 * A structure to hold backtrace information. This structure provides an easy means to
251 * store backtrace information or pass backtraces to other functions.
254 /*! The addresses of the stack frames. This is filled in by calling the glibc backtrace() function */
255 void *addresses[AST_MAX_BT_FRAMES];
256 /*! The number of stack frames in the backtrace */
258 /*! Tells if the ast_bt structure was dynamically allocated */
259 unsigned int alloced:1;
263 * Allocates memory for an ast_bt and stores addresses and symbols.
265 * \return Returns NULL on failure, or the allocated ast_bt on success
268 struct ast_bt *ast_bt_create(void);
271 * Fill an allocated ast_bt with addresses
277 int ast_bt_get_addresses(struct ast_bt *bt);
281 * Free dynamically allocated portions of an ast_bt
286 void *ast_bt_destroy(struct ast_bt *bt);
288 /* \brief Retrieve symbols for a set of backtrace addresses
290 * \param addresses A list of addresses, such as the ->addresses structure element of struct ast_bt.
291 * \param num_frames Number of addresses in the addresses list
292 * \retval NULL Unable to allocate memory
293 * \return List of strings
296 char **ast_bt_get_symbols(void **addresses, size_t num_frames);
298 #endif /* HAVE_BKTR */
299 #endif /* _LOGGER_BACKTRACE_H */
301 #if defined(__cplusplus) || defined(c_plusplus)
305 #endif /* _ASTERISK_LOGGER_H */