blob: 5dfe17c118d16363af339452d9ee6d52df0004a7 [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
2 *
3 */
Benny Prijonodd859a62005-11-01 16:42:51 +00004
5#ifndef __PJ_LOG_H__
6#define __PJ_LOG_H__
7
8/**
9 * @file log.h
10 * @brief Logging Utility.
11 */
12
13#include <pj/types.h>
14
15
16PJ_BEGIN_DECL
17
18/**
19 * @defgroup PJ_MISC Miscelaneous
20 * @ingroup PJ
21 */
22
23/**
24 * @defgroup PJ_LOG Logging Facility
25 * @ingroup PJ_MISC
26 * @{
27 *
28 * The PJLIB logging facility is a configurable, flexible, and convenient
29 * way to write logging or trace information.
30 *
31 * To write to the log, one uses construct like below:
32 *
33 * <pre>
34 * ...
35 * PJ_LOG(3, ("main.c", "Starting hello..."));
36 * ...
37 * PJ_LOG(3, ("main.c", "Hello world from process %d", pj_getpid()));
38 * ...
39 * </pre>
40 *
41 * In the above example, the number @b 3 controls the verbosity level of
42 * the information (which means "information", by convention). The string
43 * "main.c" specifies the source or sender of the message.
44 *
45 *
46 * \section pj_log_quick_sample_sec Examples
47 *
48 * For examples, see:
49 * - @ref page_pjlib_samples_log_c.
50 *
51 */
52
53/**
54 * Log decoration flag, to be specified with #pj_log_set_decor().
55 */
56enum pj_log_decoration
57{
58 PJ_LOG_HAS_DAY_NAME = 1, /**< Include day name [default: no]. */
59 PJ_LOG_HAS_YEAR = 2, /**< Include year digit [default: no] */
60 PJ_LOG_HAS_MONTH = 4, /**< Include month [default: no] */
61 PJ_LOG_HAS_DAY_OF_MON = 8, /**< Include day of month [default: no] */
62 PJ_LOG_HAS_TIME = 16, /**< Include time [default: yes]. */
63 PJ_LOG_HAS_MICRO_SEC = 32, /**< Include microseconds [yes] */
64 PJ_LOG_HAS_SENDER = 64, /**< Include sender in the log [yes]. */
65 PJ_LOG_HAS_NEWLINE = 128, /**< Terminate each call with newline [yes].*/
66};
67
68/**
69 * Write log message.
70 * This is the main macro used to write text to the logging backend.
71 *
72 * @param level The logging verbosity level. Lower number indicates higher
73 * importance, with level zero indicates fatal error. Only
74 * numeral argument is permitted (e.g. not variable).
75 * @param arg Enclosed 'printf' like arguments, with the first
76 * argument is the sender, the second argument is format
77 * string and the following arguments are variable number of
78 * arguments suitable for the format string.
79 *
80 * Sample:
81 * \verbatim
82 PJ_LOG(2, (__FILE__, "current value is %d", value));
83 \endverbatim
84 * @hideinitializer
85 */
86#define PJ_LOG(level,arg) pj_log_wrapper_##level(arg)
87
88/**
89 * Signature for function to be registered to the logging subsystem to
90 * write the actual log message to some output device.
91 *
92 * @param level Log level.
93 * @param data Log message.
94 * @param len Message length.
95 */
96typedef void pj_log_func(int level, const char *data, int len);
97
98/**
99 * Default logging writer function used by front end logger function.
100 * Application normally should NOT need to call this function, but
101 * rather use the PJ_LOG macro.
102 *
103 * @param level Log level.
104 * @param buffer Log message.
105 * @param len Message length.
106 */
107PJ_DECL(void) pj_log_write(int level, const char *buffer, int len);
108
109
110#if PJ_LOG_MAX_LEVEL >= 1
111
112/**
113 * Change log output function. The front-end logging functions will call
114 * this function to write the actual message to the desired device.
115 * By default, the front-end functions use pj_log_write() to write
116 * the messages, unless it's changed by calling this function.
117 *
118 * @param func The function that will be called to write the log
119 * messages to the desired device.
120 */
121PJ_DECL(void) pj_log_set_log_func( pj_log_func *func );
122
123/**
124 * Get the current log output function that is used to write log messages.
125 *
126 * @return Current log output function.
127 */
128PJ_DECL(pj_log_func*) pj_log_get_log_func(void);
129
130/**
131 * Set maximum log level. Application can call this function to set
132 * the desired level of verbosity of the logging messages. The bigger the
133 * value, the more verbose the logging messages will be printed. However,
134 * the maximum level of verbosity can not exceed compile time value of
135 * PJ_LOG_MAX_LEVEL.
136 *
137 * @param level The maximum level of verbosity of the logging
138 * messages (6=very detailed..1=error only, 0=disabled)
139 */
140PJ_DECL(void) pj_log_set_level(int level);
141
142/**
143 * Get current maximum log verbositylevel.
144 *
145 * @return Current log maximum level.
146 */
147PJ_DECL(int) pj_log_get_level(void);
148
149/**
150 * Set log decoration. The log decoration flag controls what are printed
151 * to output device alongside the actual message. For example, application
152 * can specify that date/time information should be displayed with each
153 * log message.
154 *
155 * @param decor Bitmask combination of #pj_log_decoration to control
156 * the layout of the log message.
157 */
158PJ_DECL(void) pj_log_set_decor(unsigned decor);
159
160/**
161 * Get current log decoration flag.
162 *
163 * @return Log decoration flag.
164 */
165PJ_DECL(unsigned) pj_log_get_decor(void);
166
167
168#else /* #if PJ_LOG_MAX_LEVEL >= 1 */
169
170/**
171 * Change log output function. The front-end logging functions will call
172 * this function to write the actual message to the desired device.
173 * By default, the front-end functions use pj_log_write() to write
174 * the messages, unless it's changed by calling this function.
175 *
176 * @param func The function that will be called to write the log
177 * messages to the desired device.
178 */
179# define pj_log_set_log_func(func)
180
181/**
182 * Set maximum log level. Application can call this function to set
183 * the desired level of verbosity of the logging messages. The bigger the
184 * value, the more verbose the logging messages will be printed. However,
185 * the maximum level of verbosity can not exceed compile time value of
186 * PJ_LOG_MAX_LEVEL.
187 *
188 * @param level The maximum level of verbosity of the logging
189 * messages (6=very detailed..1=error only, 0=disabled)
190 */
191# define pj_log_set_level(level)
192
193/**
194 * Set log decoration. The log decoration flag controls what are printed
195 * to output device alongside the actual message. For example, application
196 * can specify that date/time information should be displayed with each
197 * log message.
198 *
199 * @param decor Bitmask combination of #pj_log_decoration to control
200 * the layout of the log message.
201 */
202# define pj_log_set_decor(decor)
203
204#endif /* #if PJ_LOG_MAX_LEVEL >= 1 */
205
206/**
207 * @}
208 */
209
210///////////////////////////////////////////////////////////////////////////////
211/*
212 * Log functions implementation prototypes.
213 * These functions are called by PJ_LOG macros according to verbosity
214 * level specified when calling the macro. Applications should not normally
215 * need to call these functions directly.
216 */
217
218/**
219 * @def pj_log_wrapper_1(arg)
220 * Internal function to write log with verbosity 1. Will evaluate to
221 * empty expression if PJ_LOG_MAX_LEVEL is below 1.
222 * @param arg Log expression.
223 */
224#if PJ_LOG_MAX_LEVEL >= 1
225 #define pj_log_wrapper_1(arg) pj_log_1 arg
226 /** Internal function. */
227 PJ_DECL(void) pj_log_1(const char *src, const char *format, ...);
228#else
229 #define pj_log_wrapper_1(arg)
230#endif
231
232/**
233 * @def pj_log_wrapper_2(arg)
234 * Internal function to write log with verbosity 2. Will evaluate to
235 * empty expression if PJ_LOG_MAX_LEVEL is below 2.
236 * @param arg Log expression.
237 */
238#if PJ_LOG_MAX_LEVEL >= 2
239 #define pj_log_wrapper_2(arg) pj_log_2 arg
240 /** Internal function. */
241 PJ_DECL(void) pj_log_2(const char *src, const char *format, ...);
242#else
243 #define pj_log_wrapper_2(arg)
244#endif
245
246/**
247 * @def pj_log_wrapper_3(arg)
248 * Internal function to write log with verbosity 3. Will evaluate to
249 * empty expression if PJ_LOG_MAX_LEVEL is below 3.
250 * @param arg Log expression.
251 */
252#if PJ_LOG_MAX_LEVEL >= 3
253 #define pj_log_wrapper_3(arg) pj_log_3 arg
254 /** Internal function. */
255 PJ_DECL(void) pj_log_3(const char *src, const char *format, ...);
256#else
257 #define pj_log_wrapper_3(arg)
258#endif
259
260/**
261 * @def pj_log_wrapper_4(arg)
262 * Internal function to write log with verbosity 4. Will evaluate to
263 * empty expression if PJ_LOG_MAX_LEVEL is below 4.
264 * @param arg Log expression.
265 */
266#if PJ_LOG_MAX_LEVEL >= 4
267 #define pj_log_wrapper_4(arg) pj_log_4 arg
268 /** Internal function. */
269 PJ_DECL(void) pj_log_4(const char *src, const char *format, ...);
270#else
271 #define pj_log_wrapper_4(arg)
272#endif
273
274/**
275 * @def pj_log_wrapper_5(arg)
276 * Internal function to write log with verbosity 5. Will evaluate to
277 * empty expression if PJ_LOG_MAX_LEVEL is below 5.
278 * @param arg Log expression.
279 */
280#if PJ_LOG_MAX_LEVEL >= 5
281 #define pj_log_wrapper_5(arg) pj_log_5 arg
282 /** Internal function. */
283 PJ_DECL(void) pj_log_5(const char *src, const char *format, ...);
284#else
285 #define pj_log_wrapper_5(arg)
286#endif
287
288/**
289 * @def pj_log_wrapper_6(arg)
290 * Internal function to write log with verbosity 6. Will evaluate to
291 * empty expression if PJ_LOG_MAX_LEVEL is below 6.
292 * @param arg Log expression.
293 */
294#if PJ_LOG_MAX_LEVEL >= 6
295 #define pj_log_wrapper_6(arg) pj_log_6 arg
296 /** Internal function. */
297 PJ_DECL(void) pj_log_6(const char *src, const char *format, ...);
298#else
299 #define pj_log_wrapper_6(arg)
300#endif
301
302
303PJ_END_DECL
304
305#endif /* __PJ_LOG_H__ */
306