701c1acf92364dde7098f142edf61354b78d5148
[asterisk/asterisk.git] / include / asterisk / logger.h
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 /*!
20   \file logger.h
21   \brief Support for logging to various files, console and syslog
22         Configuration in file logger.conf
23 */
24
25 #ifndef _ASTERISK_LOGGER_H
26 #define _ASTERISK_LOGGER_H
27
28 #include "asterisk/compat.h"
29
30 #include <stdarg.h>
31
32 #if defined(__cplusplus) || defined(c_plusplus)
33 extern "C" {
34 #endif
35
36 #define EVENTLOG "event_log"
37 #define QUEUELOG        "queue_log"
38
39 #define DEBUG_M(a) { \
40         a; \
41 }
42
43 #define VERBOSE_PREFIX_1 " "
44 #define VERBOSE_PREFIX_2 "  == "
45 #define VERBOSE_PREFIX_3 "    -- "
46 #define VERBOSE_PREFIX_4 "       > "
47
48 /*! Used for sending a log message */
49 /*!
50         \brief This is the standard logger function.  Probably the only way you will invoke it would be something like this:
51         ast_log(LOG_WHATEVER, "Problem with the %s Captain.  We should get some more.  Will %d be enough?\n", "flux capacitor", 10);
52         where WHATEVER is one of ERROR, DEBUG, EVENT, NOTICE, or WARNING depending
53         on which log you wish to output to. These are implemented as macros, that
54         will provide the function with the needed arguments.
55
56         \param level    Type of log event
57         \param file     Will be provided by the LOG_* macro
58         \param line     Will be provided by the LOG_* macro
59         \param function Will be provided by the LOG_* macro
60         \param fmt      This is what is important.  The format is the same as your favorite breed of printf.  You know how that works, right? :-)
61  */
62
63 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
64         __attribute__ ((format (printf, 5, 6)));
65
66 void ast_backtrace(void);
67
68 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
69         __attribute__ ((format (printf, 5, 6)));
70
71 /*! Send a verbose message (based on verbose level)
72         \brief This works like ast_log, but prints verbose messages to the console depending on verbosity level set.
73         ast_verbose(VERBOSE_PREFIX_3 "Whatever %s is happening\n", "nothing");
74         This will print the message to the console if the verbose level is set to a level >= 3
75         Note the abscence of a comma after the VERBOSE_PREFIX_3.  This is important.
76         VERBOSE_PREFIX_1 through VERBOSE_PREFIX_3 are defined.
77  */
78 void ast_verbose(const char *fmt, ...)
79         __attribute__ ((format (printf, 1, 2)));
80
81 int ast_register_verbose(void (*verboser)(const char *string));
82 int ast_unregister_verbose(void (*verboser)(const char *string));
83
84 void ast_console_puts(const char *string);
85
86 void ast_console_puts_mutable(const char *string);
87 void ast_console_toggle_mute(int fd);
88
89 #define _A_ __FILE__, __LINE__, __PRETTY_FUNCTION__
90
91 #ifdef LOG_DEBUG
92 #undef LOG_DEBUG
93 #endif
94 #define __LOG_DEBUG    0
95 #define LOG_DEBUG      __LOG_DEBUG, _A_
96
97 #ifdef LOG_EVENT
98 #undef LOG_EVENT
99 #endif
100 #define __LOG_EVENT    1
101 #define LOG_EVENT      __LOG_EVENT, _A_
102
103 #ifdef LOG_NOTICE
104 #undef LOG_NOTICE
105 #endif
106 #define __LOG_NOTICE   2
107 #define LOG_NOTICE     __LOG_NOTICE, _A_
108
109 #ifdef LOG_WARNING
110 #undef LOG_WARNING
111 #endif
112 #define __LOG_WARNING  3
113 #define LOG_WARNING    __LOG_WARNING, _A_
114
115 #ifdef LOG_ERROR
116 #undef LOG_ERROR
117 #endif
118 #define __LOG_ERROR    4
119 #define LOG_ERROR      __LOG_ERROR, _A_
120
121 #ifdef LOG_VERBOSE
122 #undef LOG_VERBOSE
123 #endif
124 #define __LOG_VERBOSE  5
125 #define LOG_VERBOSE    __LOG_VERBOSE, _A_
126
127 #ifdef LOG_DTMF
128 #undef LOG_DTMF
129 #endif
130 #define __LOG_DTMF  6
131 #define LOG_DTMF    __LOG_DTMF, _A_
132
133 /*!
134  * \brief Get the debug level for a file
135  * \arg file the filename
136  * \return the debug level
137  */
138 unsigned int ast_debug_get_by_file(const char *file);
139
140 /*!
141  * \brief Get the debug level for a file
142  * \arg file the filename
143  * \return the debug level
144  */
145 unsigned int ast_verbose_get_by_file(const char *file);
146
147 /*!
148  * \brief Log a DEBUG message
149  * \param level The minimum value of option_debug for this message
150  *        to get logged
151  */
152 #define ast_debug(level, ...) do {       \
153         if (option_debug >= (level) || (ast_opt_dbg_file && ast_debug_get_by_file(__FILE__) >= (level)) ) \
154                 ast_log(LOG_DEBUG, __VA_ARGS__); \
155 } while (0)
156
157 #define ast_verb(level, ...) do { \
158         if (option_verbose >= (level) || (ast_opt_verb_file && ast_verbose_get_by_file(__FILE__) >= (level)) ) { \
159                 if (level >= 4) \
160                         ast_verbose(VERBOSE_PREFIX_4 __VA_ARGS__); \
161                 else if (level == 3) \
162                         ast_verbose(VERBOSE_PREFIX_3 __VA_ARGS__); \
163                 else if (level == 2) \
164                         ast_verbose(VERBOSE_PREFIX_2 __VA_ARGS__); \
165                 else if (level == 1) \
166                         ast_verbose(VERBOSE_PREFIX_1 __VA_ARGS__); \
167                 else \
168                         ast_verbose(__VA_ARGS__); \
169         } \
170 } while (0)
171
172 #if defined(__cplusplus) || defined(c_plusplus)
173 }
174 #endif
175
176 #endif /* _ASTERISK_LOGGER_H */