Debian Codegraph @

Xpjproject/pjlib/include/pj/log.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #ifndef __PJ_LOG_H__
20 #define __PJ_LOG_H__
21
22 /**
23 * @file log.h
24 * @brief Logging Utility.
25 */
26
27 #include <pj/types.h>
28 #include <stdarg.h>
29
30 PJ_BEGIN_DECL
31
32 /**
33 * @defgroup PJ_MISC Miscelaneous
34 */
35
36 /**
37 * @defgroup PJ_LOG Logging Facility
38 * @ingroup PJ_MISC
39 * @{
40 *
41 * The PJLIB logging facility is a configurable, flexible, and convenient
42 * way to write logging or trace information.
43 *
44 * To write to the log, one uses construct like below:
45 *
46 * <pre>
47 * ...
48 * PJ_LOG(3, ("main.c", "Starting hello..."));
49 * ...
50 * PJ_LOG(3, ("main.c", "Hello world from process %d", pj_getpid()));
51 * ...
52 * </pre>
53 *
54 * In the above example, the number @b 3 controls the verbosity level of
55 * the information (which means "information", by convention). The string
56 * "main.c" specifies the source or sender of the message.
57 *
58 *
59 * \section pj_log_quick_sample_sec Examples
60 *
61 * For examples, see:
62 * - Simple Log sample: \src{pjlib/src/pjlib-samples/log.c}
63 *
64 */
65
66 /**
67 * Log decoration flag, to be specified with #pj_log_set_decor().
68 */
69 enum pj_log_decoration
70 {
71 PJ_LOG_HAS_DAY_NAME = 1, /**< Include day name [default: no] */
72 PJ_LOG_HAS_YEAR = 2, /**< Include year digit [no] */
73 PJ_LOG_HAS_MONTH = 4, /**< Include month [no] */
74 PJ_LOG_HAS_DAY_OF_MON = 8, /**< Include day of month [no] */
75 PJ_LOG_HAS_TIME = 16, /**< Include time [yes] */
76 PJ_LOG_HAS_MICRO_SEC = 32, /**< Include microseconds [yes] */
77 PJ_LOG_HAS_SENDER = 64, /**< Include sender in the log [yes] */
78 PJ_LOG_HAS_NEWLINE = 128, /**< Terminate each call with newline [yes] */
79 PJ_LOG_HAS_CR = 256, /**< Include carriage return [no] */
80 PJ_LOG_HAS_SPACE = 512, /**< Include two spaces before log [yes] */
81 PJ_LOG_HAS_COLOR = 1024, /**< Colorize logs [yes on win32] */
82 PJ_LOG_HAS_LEVEL_TEXT = 2048, /**< Include level text string [no] */
83 PJ_LOG_HAS_THREAD_ID = 4096, /**< Include thread identification [no] */
84 PJ_LOG_HAS_THREAD_SWC = 8192, /**< Add mark when thread has switched [yes]*/
85 PJ_LOG_HAS_INDENT =16384 /**< Indentation. Say yes! [yes] */
86 };
87
88 /**
89 * Write log message.
90 * This is the main macro used to write text to the logging backend.
91 *
92 * @param level The logging verbosity level. Lower number indicates higher
93 * importance, with level zero indicates fatal error. Only
94 * numeral argument is permitted (e.g. not variable).
95 * @param arg Enclosed 'printf' like arguments, with the first
96 * argument is the sender, the second argument is format
97 * string and the following arguments are variable number of
98 * arguments suitable for the format string.
99 *
100 * Sample:
101 * \verbatim
102 PJ_LOG(2, (__FILE__, "current value is %d", value));
103 \endverbatim
104 * @hideinitializer
105 */
106 #define PJ_LOG(level,arg) do { \
107 if (level <= PJ_LOG_MAX_LEVEL && level <= pj_log_get_level()) { \
108 pj_log_wrapper_##level(arg); \
109 } \
110 } while (0)
111
112 /**
113 * Write log message.
114 *
115 * @param LVL The logging verbosity level. Lower number indicates higher
116 * importance, with level zero indicates fatal error. Variable
117 * argument is not permitted, the argument may be a numeral
118 * constant or a numeral value macros.
119 * @param src The first argument is the sender.
120 * @param format The second argument is 'printf' like format string and
121 * the following arguments are variable number of
122 * arguments suitable for the format string.
123 *
124 * Sample:
125 * \verbatim
126 #ifdef PJ_DEBUG
127 # define MY_LOG_LEVEL 2
128 #else
129 # define MY_LOG_LEVEL 6
130 #endif
131 PJ_LOG_(MY_LOG_LEVEL, __FILE__, "current value is %d", value);
132 \endverbatim
133 * @hideinitializer
134 */
135 #define PJ_LOG_(LVL, src, format, ...) PJ_LOG(LVL,(src, format, __VA_ARGS__))
136
137 /**
138 * Signature for function to be registered to the logging subsystem to
139 * write the actual log message to some output device.
140 *
141 * @param level Log level.
142 * @param data Log message, which will be NULL terminated.
143 * @param len Message length.
144 */
145 typedef void pj_log_func(int level, const char *data, int len);
146
147 /**
148 * Default logging writer function used by front end logger function.
149 * This function will print the log message to stdout only.
150 * Application normally should NOT need to call this function, but
151 * rather use the PJ_LOG macro.
152 *
153 * @param level Log level.
154 * @param buffer Log message.
155 * @param len Message length.
156 */
157 PJ_DECL(void) pj_log_write(int level, const char *buffer, int len);
158
159
160 #if PJ_LOG_MAX_LEVEL >= 1
161
162 /**
163 * Write to log.
164 *
165 * @param sender Source of the message.
166 * @param level Verbosity level.
167 * @param format Format.
168 * @param marker Marker.
169 */
170 PJ_DECL(void) pj_log(const char *sender, int level,
171 PJ_PRINT_PARAM_DECOR const char *format, va_list marker);
172
173 /**
174 * Change log output function. The front-end logging functions will call
175 * this function to write the actual message to the desired device.
176 * By default, the front-end functions use pj_log_write() to write
177 * the messages, unless it's changed by calling this function.
178 *
179 * @param func The function that will be called to write the log
180 * messages to the desired device.
181 */
182 PJ_DECL(void) pj_log_set_log_func( pj_log_func *func );
183
184 /**
185 * Get the current log output function that is used to write log messages.
186 *
187 * @return Current log output function.
188 */
189 PJ_DECL(pj_log_func*) pj_log_get_log_func(void);
190
191 /**
192 * Set maximum log level. Application can call this function to set
193 * the desired level of verbosity of the logging messages. The bigger the
194 * value, the more verbose the logging messages will be printed. However,
195 * the maximum level of verbosity can not exceed compile time value of
196 * PJ_LOG_MAX_LEVEL.
197 *
198 * @param level The maximum level of verbosity of the logging
199 * messages (6=very detailed..1=error only, 0=disabled)
200 */
201 PJ_DECL(void) pj_log_set_level(int level);
202
203 /**
204 * Get current maximum log verbositylevel.
205 *
206 * @return Current log maximum level.
207 */
208 #if 1
209 PJ_DECL(int) pj_log_get_level(void);
210 #else
211 PJ_DECL_DATA(int) pj_log_max_level;
212 #define pj_log_get_level() pj_log_max_level
213 #endif
214
215 /**
216 * Set log decoration. The log decoration flag controls what are printed
217 * to output device alongside the actual message. For example, application
218 * can specify that date/time information should be displayed with each
219 * log message.
220 *
221 * @param decor Bitmask combination of #pj_log_decoration to control
222 * the layout of the log message.
223 */
224 PJ_DECL(void) pj_log_set_decor(unsigned decor);
225
226 /**
227 * Get current log decoration flag.
228 *
229 * @return Log decoration flag.
230 */
231 PJ_DECL(unsigned) pj_log_get_decor(void);
232
233 /**
234 * Add indentation to log message. Indentation will add PJ_LOG_INDENT_CHAR
235 * before the message, and is useful to show the depth of function calls.
236 *
237 * @param indent The indentation to add or substract. Positive value
238 * adds current indent, negative value subtracts current
239 * indent.
240 */
241 PJ_DECL(void) pj_log_add_indent(int indent);
242
243 /**
244 * Set indentation to specific value.
245 *
246 * @param indent The indentation value.
247 */
248 PJ_DECL(void) pj_log_set_indent(int indent);
249
250 /**
251 * Get current indentation value.
252 *
253 * @return Current indentation value.
254 */
255 PJ_DECL(int) pj_log_get_indent(void);
256
257 /**
258 * Push indentation to the right by default value (PJ_LOG_INDENT_SIZE).
259 */
260 PJ_DECL(void) pj_log_push_indent(void);
261
262 /**
263 * Pop indentation (to the left) by default value (PJ_LOG_INDENT_SIZE).
264 */
265 PJ_DECL(void) pj_log_pop_indent(void);
266
267 /**
268 * Set color of log messages.
269 *
270 * @param level Log level which color will be changed.
271 * @param color Desired color.
272 */
273 PJ_DECL(void) pj_log_set_color(int level, pj_color_t color);
274
275 /**
276 * Get color of log messages.
277 *
278 * @param level Log level which color will be returned.
279 * @return Log color.
280 */
281 PJ_DECL(pj_color_t) pj_log_get_color(int level);
282
283 /**
284 * Internal function to be called by pj_init()
285 */
286 pj_status_t pj_log_init(void);
287
288 #else /* #if PJ_LOG_MAX_LEVEL >= 1 */
289
290 /**
291 * Change log output function. The front-end logging functions will call
292 * this function to write the actual message to the desired device.
293 * By default, the front-end functions use pj_log_write() to write
294 * the messages, unless it's changed by calling this function.
295 *
296 * @param func The function that will be called to write the log
297 * messages to the desired device.
298 */
299 # define pj_log_set_log_func(func)
300
301 /**
302 * Get the current log output function that is used to write log messages.
303 *
304 * @return Current log output function.
305 */
306 # define pj_log_get_log_func() NULL
307
308 /**
309 * Write to log.
310 *
311 * @param sender Source of the message.
312 * @param level Verbosity level.
313 * @param format Format.
314 * @param marker Marker.
315 */
316 # define pj_log(sender, level, format, marker)
317
318 /**
319 * Set maximum log level. Application can call this function to set
320 * the desired level of verbosity of the logging messages. The bigger the
321 * value, the more verbose the logging messages will be printed. However,
322 * the maximum level of verbosity can not exceed compile time value of
323 * PJ_LOG_MAX_LEVEL.
324 *
325 * @param level The maximum level of verbosity of the logging
326 * messages (6=very detailed..1=error only, 0=disabled)
327 */
328 # define pj_log_set_level(level)
329
330 /**
331 * Set log decoration. The log decoration flag controls what are printed
332 * to output device alongside the actual message. For example, application
333 * can specify that date/time information should be displayed with each
334 * log message.
335 *
336 * @param decor Bitmask combination of #pj_log_decoration to control
337 * the layout of the log message.
338 */
339 # define pj_log_set_decor(decor)
340
341 /**
342 * Add indentation to log message. Indentation will add PJ_LOG_INDENT_CHAR
343 * before the message, and is useful to show the depth of function calls.
344 *
345 * @param indent The indentation to add or substract. Positive value
346 * adds current indent, negative value subtracts current
347 * indent.
348 */
349 # define pj_log_add_indent(indent)
350
351 /**
352 * Set indentation to specific value.
353 *
354 * @param indent The indentation value.
355 */
356 # define pj_log_set_indent(indent)
357
358 /**
359 * Get current indentation value.
360 *
361 * @return Current indentation value.
362 */
363 # define pj_log_get_indent() 0
364
365 /**
366 * Push indentation to the right by default value (PJ_LOG_INDENT_SIZE).
367 */
368 # define pj_log_push_indent()
369
370 /**
371 * Pop indentation (to the left) by default value (PJ_LOG_INDENT_SIZE).
372 */
373 # define pj_log_pop_indent()
374
375 /**
376 * Set color of log messages.
377 *
378 * @param level Log level which color will be changed.
379 * @param color Desired color.
380 */
381 # define pj_log_set_color(level, color)
382
383 /**
384 * Get current maximum log verbositylevel.
385 *
386 * @return Current log maximum level.
387 */
388 # define pj_log_get_level() 0
389
390 /**
391 * Get current log decoration flag.
392 *
393 * @return Log decoration flag.
394 */
395 # define pj_log_get_decor() 0
396
397 /**
398 * Get color of log messages.
399 *
400 * @param level Log level which color will be returned.
401 * @return Log color.
402 */
403 # define pj_log_get_color(level) 0
404
405
406 /**
407 * Internal.
408 */
409 # define pj_log_init() PJ_SUCCESS
410
411 #endif /* #if PJ_LOG_MAX_LEVEL >= 1 */
412
413 /**
414 * @}
415 */
416
417 /* **************************************************************************/
418 /*
419 * Log functions implementation prototypes.
420 * These functions are called by PJ_LOG macros according to verbosity
421 * level specified when calling the macro. Applications should not normally
422 * need to call these functions directly.
423 */
424
425 /**
426 * @def pj_log_wrapper_1(arg)
427 * Internal function to write log with verbosity 1. Will evaluate to
428 * empty expression if PJ_LOG_MAX_LEVEL is below 1.
429 * @param arg Log expression.
430 */
431 #if PJ_LOG_MAX_LEVEL >= 1
432 #define pj_log_wrapper_1(arg) pj_log_1 arg
433 /** Internal function. */
434 PJ_DECL(void) pj_log_1(const char *src,
435 PJ_PRINT_PARAM_DECOR const char *format, ...)
436 PJ_PRINT_FUNC_DECOR(2);
437 #else
438 #define pj_log_wrapper_1(arg)
439 #endif
440
441 /**
442 * @def pj_log_wrapper_2(arg)
443 * Internal function to write log with verbosity 2. Will evaluate to
444 * empty expression if PJ_LOG_MAX_LEVEL is below 2.
445 * @param arg Log expression.
446 */
447 #if PJ_LOG_MAX_LEVEL >= 2
448 #define pj_log_wrapper_2(arg) pj_log_2 arg
449 /** Internal function. */
450 PJ_DECL(void) pj_log_2(const char *src,
451 PJ_PRINT_PARAM_DECOR const char *format, ...)
452 PJ_PRINT_FUNC_DECOR(2);
453 #else
454 #define pj_log_wrapper_2(arg)
455 #endif
456
457 /**
458 * @def pj_log_wrapper_3(arg)
459 * Internal function to write log with verbosity 3. Will evaluate to
460 * empty expression if PJ_LOG_MAX_LEVEL is below 3.
461 * @param arg Log expression.
462 */
463 #if PJ_LOG_MAX_LEVEL >= 3
464 #define pj_log_wrapper_3(arg) pj_log_3 arg
465 /** Internal function. */
466 PJ_DECL(void) pj_log_3(const char *src,
467 PJ_PRINT_PARAM_DECOR const char *format, ...)
468 PJ_PRINT_FUNC_DECOR(2);
469 #else
470 #define pj_log_wrapper_3(arg)
471 #endif
472
473 /**
474 * @def pj_log_wrapper_4(arg)
475 * Internal function to write log with verbosity 4. Will evaluate to
476 * empty expression if PJ_LOG_MAX_LEVEL is below 4.
477 * @param arg Log expression.
478 */
479 #if PJ_LOG_MAX_LEVEL >= 4
480 #define pj_log_wrapper_4(arg) pj_log_4 arg
481 /** Internal function. */
482 PJ_DECL(void) pj_log_4(const char *src,
483 PJ_PRINT_PARAM_DECOR const char *format, ...)
484 PJ_PRINT_FUNC_DECOR(2);
485 #else
486 #define pj_log_wrapper_4(arg)
487 #endif
488
489 /**
490 * @def pj_log_wrapper_5(arg)
491 * Internal function to write log with verbosity 5. Will evaluate to
492 * empty expression if PJ_LOG_MAX_LEVEL is below 5.
493 * @param arg Log expression.
494 */
495 #if PJ_LOG_MAX_LEVEL >= 5
496 #define pj_log_wrapper_5(arg) pj_log_5 arg
497 /** Internal function. */
498 PJ_DECL(void) pj_log_5(const char *src,
499 PJ_PRINT_PARAM_DECOR const char *format, ...)
500 PJ_PRINT_FUNC_DECOR(2);
501 #else
502 #define pj_log_wrapper_5(arg)
503 #endif
504
505 /**
506 * @def pj_log_wrapper_6(arg)
507 * Internal function to write log with verbosity 6. Will evaluate to
508 * empty expression if PJ_LOG_MAX_LEVEL is below 6.
509 * @param arg Log expression.
510 */
511 #if PJ_LOG_MAX_LEVEL >= 6
512 #define pj_log_wrapper_6(arg) pj_log_6 arg
513 /** Internal function. */
514 PJ_DECL(void) pj_log_6(const char *src,
515 PJ_PRINT_PARAM_DECOR const char *format, ...)
516 PJ_PRINT_FUNC_DECOR(2);
517 #else
518 #define pj_log_wrapper_6(arg)
519 #endif
520
521
522 PJ_END_DECL
523
524 #endif /* __PJ_LOG_H__ */
525