blob: 4b2083c2e49a958573e2789fb4c6ae7a0f867640 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#ifndef __PJ_LOG_H__
21#define __PJ_LOG_H__
22
23/**
24 * @file log.h
25 * @brief Logging Utility.
26 */
27
28#include <pj/types.h>
29#include <stdarg.h>
30
31PJ_BEGIN_DECL
32
33/**
34 * @defgroup PJ_MISC Miscelaneous
Benny Prijono9033e312005-11-21 02:08:39 +000035 */
36
37/**
38 * @defgroup PJ_LOG Logging Facility
39 * @ingroup PJ_MISC
40 * @{
41 *
42 * The PJLIB logging facility is a configurable, flexible, and convenient
43 * way to write logging or trace information.
44 *
45 * To write to the log, one uses construct like below:
46 *
47 * <pre>
48 * ...
49 * PJ_LOG(3, ("main.c", "Starting hello..."));
50 * ...
51 * PJ_LOG(3, ("main.c", "Hello world from process %d", pj_getpid()));
52 * ...
53 * </pre>
54 *
55 * In the above example, the number @b 3 controls the verbosity level of
56 * the information (which means "information", by convention). The string
57 * "main.c" specifies the source or sender of the message.
58 *
59 *
60 * \section pj_log_quick_sample_sec Examples
61 *
62 * For examples, see:
63 * - @ref page_pjlib_samples_log_c.
64 *
65 */
66
67/**
68 * Log decoration flag, to be specified with #pj_log_set_decor().
69 */
70enum pj_log_decoration
71{
Benny Prijonod6e362a2008-07-19 17:53:47 +000072 PJ_LOG_HAS_DAY_NAME = 1, /**< Include day name [default: no] */
73 PJ_LOG_HAS_YEAR = 2, /**< Include year digit [no] */
74 PJ_LOG_HAS_MONTH = 4, /**< Include month [no] */
75 PJ_LOG_HAS_DAY_OF_MON = 8, /**< Include day of month [no] */
76 PJ_LOG_HAS_TIME = 16, /**< Include time [yes] */
77 PJ_LOG_HAS_MICRO_SEC = 32, /**< Include microseconds [yes] */
78 PJ_LOG_HAS_SENDER = 64, /**< Include sender in the log [yes] */
79 PJ_LOG_HAS_NEWLINE = 128, /**< Terminate each call with newline [yes] */
80 PJ_LOG_HAS_CR = 256, /**< Include carriage return [no] */
81 PJ_LOG_HAS_SPACE = 512, /**< Include two spaces before log [yes] */
Benny Prijono901a2c32008-07-28 21:15:04 +000082 PJ_LOG_HAS_COLOR = 1024, /**< Colorize logs [yes on win32] */
Benny Prijonof940be42009-07-21 12:20:17 +000083 PJ_LOG_HAS_LEVEL_TEXT = 2048, /**< Include level text string [no] */
84 PJ_LOG_HAS_THREAD_ID = 4096 /**< Include thread identification [no] */
Benny Prijono9033e312005-11-21 02:08:39 +000085};
86
87/**
88 * Write log message.
89 * This is the main macro used to write text to the logging backend.
90 *
91 * @param level The logging verbosity level. Lower number indicates higher
92 * importance, with level zero indicates fatal error. Only
93 * numeral argument is permitted (e.g. not variable).
94 * @param arg Enclosed 'printf' like arguments, with the first
95 * argument is the sender, the second argument is format
96 * string and the following arguments are variable number of
97 * arguments suitable for the format string.
98 *
99 * Sample:
100 * \verbatim
101 PJ_LOG(2, (__FILE__, "current value is %d", value));
102 \endverbatim
103 * @hideinitializer
104 */
Benny Prijono505e82e2007-03-05 21:08:01 +0000105#define PJ_LOG(level,arg) do { \
106 if (level <= pj_log_get_level()) \
107 pj_log_wrapper_##level(arg); \
108 } while (0)
Benny Prijono9033e312005-11-21 02:08:39 +0000109
110/**
111 * Signature for function to be registered to the logging subsystem to
112 * write the actual log message to some output device.
113 *
114 * @param level Log level.
Benny Prijonoab168dd2007-12-12 14:06:31 +0000115 * @param data Log message, which will be NULL terminated.
Benny Prijono9033e312005-11-21 02:08:39 +0000116 * @param len Message length.
117 */
118typedef void pj_log_func(int level, const char *data, int len);
119
120/**
121 * Default logging writer function used by front end logger function.
Benny Prijonoccf95622006-02-07 18:48:01 +0000122 * This function will print the log message to stdout only.
Benny Prijono9033e312005-11-21 02:08:39 +0000123 * Application normally should NOT need to call this function, but
124 * rather use the PJ_LOG macro.
125 *
126 * @param level Log level.
127 * @param buffer Log message.
128 * @param len Message length.
129 */
130PJ_DECL(void) pj_log_write(int level, const char *buffer, int len);
131
132
133#if PJ_LOG_MAX_LEVEL >= 1
134
135/**
136 * Write to log.
137 *
138 * @param sender Source of the message.
139 * @param level Verbosity level.
140 * @param format Format.
141 * @param marker Marker.
142 */
143PJ_DECL(void) pj_log(const char *sender, int level,
144 const char *format, va_list marker);
145
146/**
147 * Change log output function. The front-end logging functions will call
148 * this function to write the actual message to the desired device.
149 * By default, the front-end functions use pj_log_write() to write
150 * the messages, unless it's changed by calling this function.
151 *
152 * @param func The function that will be called to write the log
153 * messages to the desired device.
154 */
155PJ_DECL(void) pj_log_set_log_func( pj_log_func *func );
156
157/**
158 * Get the current log output function that is used to write log messages.
159 *
160 * @return Current log output function.
161 */
162PJ_DECL(pj_log_func*) pj_log_get_log_func(void);
163
164/**
165 * Set maximum log level. Application can call this function to set
166 * the desired level of verbosity of the logging messages. The bigger the
167 * value, the more verbose the logging messages will be printed. However,
168 * the maximum level of verbosity can not exceed compile time value of
169 * PJ_LOG_MAX_LEVEL.
170 *
171 * @param level The maximum level of verbosity of the logging
172 * messages (6=very detailed..1=error only, 0=disabled)
173 */
174PJ_DECL(void) pj_log_set_level(int level);
175
176/**
177 * Get current maximum log verbositylevel.
178 *
179 * @return Current log maximum level.
180 */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000181#if 1
Benny Prijono9033e312005-11-21 02:08:39 +0000182PJ_DECL(int) pj_log_get_level(void);
Benny Prijono505e82e2007-03-05 21:08:01 +0000183#else
Benny Prijono8ab968f2007-07-20 08:08:30 +0000184PJ_DECL_DATA(int) pj_log_max_level;
Benny Prijono505e82e2007-03-05 21:08:01 +0000185#define pj_log_get_level() pj_log_max_level
186#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000187
188/**
189 * Set log decoration. The log decoration flag controls what are printed
190 * to output device alongside the actual message. For example, application
191 * can specify that date/time information should be displayed with each
192 * log message.
193 *
194 * @param decor Bitmask combination of #pj_log_decoration to control
195 * the layout of the log message.
196 */
197PJ_DECL(void) pj_log_set_decor(unsigned decor);
198
199/**
200 * Get current log decoration flag.
201 *
202 * @return Log decoration flag.
203 */
204PJ_DECL(unsigned) pj_log_get_decor(void);
205
206
Benny Prijonod6e362a2008-07-19 17:53:47 +0000207/**
208 * Set color of log messages.
209 *
210 * @param level Log level which color will be changed.
211 * @param color Desired color.
212 */
213PJ_DECL(void) pj_log_set_color(int level, pj_color_t color);
214
215/**
216 * Get color of log messages.
217 *
218 * @param level Log level which color will be returned.
219 * @return Log color.
220 */
221PJ_DECL(pj_color_t) pj_log_get_color(int level);
222
223
Benny Prijono9033e312005-11-21 02:08:39 +0000224#else /* #if PJ_LOG_MAX_LEVEL >= 1 */
225
226/**
227 * Change log output function. The front-end logging functions will call
228 * this function to write the actual message to the desired device.
229 * By default, the front-end functions use pj_log_write() to write
230 * the messages, unless it's changed by calling this function.
231 *
232 * @param func The function that will be called to write the log
233 * messages to the desired device.
234 */
235# define pj_log_set_log_func(func)
236
237/**
238 * Set maximum log level. Application can call this function to set
239 * the desired level of verbosity of the logging messages. The bigger the
240 * value, the more verbose the logging messages will be printed. However,
241 * the maximum level of verbosity can not exceed compile time value of
242 * PJ_LOG_MAX_LEVEL.
243 *
244 * @param level The maximum level of verbosity of the logging
245 * messages (6=very detailed..1=error only, 0=disabled)
246 */
247# define pj_log_set_level(level)
248
249/**
250 * Set log decoration. The log decoration flag controls what are printed
251 * to output device alongside the actual message. For example, application
252 * can specify that date/time information should be displayed with each
253 * log message.
254 *
255 * @param decor Bitmask combination of #pj_log_decoration to control
256 * the layout of the log message.
257 */
258# define pj_log_set_decor(decor)
259
Benny Prijono4381efe2006-03-16 14:24:26 +0000260/**
Benny Prijonod6e362a2008-07-19 17:53:47 +0000261 * Set color of log messages.
262 *
263 * @param level Log level which color will be changed.
264 * @param color Desired color.
265 */
266# define pj_log_set_color(level, color)
267
268/**
Benny Prijono4381efe2006-03-16 14:24:26 +0000269 * Get current maximum log verbositylevel.
270 *
271 * @return Current log maximum level.
272 */
273# define pj_log_get_level() 0
274
275/**
276 * Get current log decoration flag.
277 *
278 * @return Log decoration flag.
279 */
280# define pj_log_get_decor() 0
281
Benny Prijonod6e362a2008-07-19 17:53:47 +0000282/**
283 * Get color of log messages.
284 *
285 * @param level Log level which color will be returned.
286 * @return Log color.
287 */
288# define pj_log_get_color(level) 0
289
Benny Prijono4381efe2006-03-16 14:24:26 +0000290
Benny Prijono9033e312005-11-21 02:08:39 +0000291#endif /* #if PJ_LOG_MAX_LEVEL >= 1 */
292
293/**
294 * @}
295 */
296
Benny Prijono92ac4472006-07-22 13:42:56 +0000297/* **************************************************************************/
Benny Prijono9033e312005-11-21 02:08:39 +0000298/*
299 * Log functions implementation prototypes.
300 * These functions are called by PJ_LOG macros according to verbosity
301 * level specified when calling the macro. Applications should not normally
302 * need to call these functions directly.
303 */
304
305/**
306 * @def pj_log_wrapper_1(arg)
307 * Internal function to write log with verbosity 1. Will evaluate to
308 * empty expression if PJ_LOG_MAX_LEVEL is below 1.
309 * @param arg Log expression.
310 */
311#if PJ_LOG_MAX_LEVEL >= 1
312 #define pj_log_wrapper_1(arg) pj_log_1 arg
313 /** Internal function. */
314 PJ_DECL(void) pj_log_1(const char *src, const char *format, ...);
315#else
316 #define pj_log_wrapper_1(arg)
317#endif
318
319/**
320 * @def pj_log_wrapper_2(arg)
321 * Internal function to write log with verbosity 2. Will evaluate to
322 * empty expression if PJ_LOG_MAX_LEVEL is below 2.
323 * @param arg Log expression.
324 */
325#if PJ_LOG_MAX_LEVEL >= 2
326 #define pj_log_wrapper_2(arg) pj_log_2 arg
327 /** Internal function. */
328 PJ_DECL(void) pj_log_2(const char *src, const char *format, ...);
329#else
330 #define pj_log_wrapper_2(arg)
331#endif
332
333/**
334 * @def pj_log_wrapper_3(arg)
335 * Internal function to write log with verbosity 3. Will evaluate to
336 * empty expression if PJ_LOG_MAX_LEVEL is below 3.
337 * @param arg Log expression.
338 */
339#if PJ_LOG_MAX_LEVEL >= 3
340 #define pj_log_wrapper_3(arg) pj_log_3 arg
341 /** Internal function. */
342 PJ_DECL(void) pj_log_3(const char *src, const char *format, ...);
343#else
344 #define pj_log_wrapper_3(arg)
345#endif
346
347/**
348 * @def pj_log_wrapper_4(arg)
349 * Internal function to write log with verbosity 4. Will evaluate to
350 * empty expression if PJ_LOG_MAX_LEVEL is below 4.
351 * @param arg Log expression.
352 */
353#if PJ_LOG_MAX_LEVEL >= 4
354 #define pj_log_wrapper_4(arg) pj_log_4 arg
355 /** Internal function. */
356 PJ_DECL(void) pj_log_4(const char *src, const char *format, ...);
357#else
358 #define pj_log_wrapper_4(arg)
359#endif
360
361/**
362 * @def pj_log_wrapper_5(arg)
363 * Internal function to write log with verbosity 5. Will evaluate to
364 * empty expression if PJ_LOG_MAX_LEVEL is below 5.
365 * @param arg Log expression.
366 */
367#if PJ_LOG_MAX_LEVEL >= 5
368 #define pj_log_wrapper_5(arg) pj_log_5 arg
369 /** Internal function. */
370 PJ_DECL(void) pj_log_5(const char *src, const char *format, ...);
371#else
372 #define pj_log_wrapper_5(arg)
373#endif
374
375/**
376 * @def pj_log_wrapper_6(arg)
377 * Internal function to write log with verbosity 6. Will evaluate to
378 * empty expression if PJ_LOG_MAX_LEVEL is below 6.
379 * @param arg Log expression.
380 */
381#if PJ_LOG_MAX_LEVEL >= 6
382 #define pj_log_wrapper_6(arg) pj_log_6 arg
383 /** Internal function. */
384 PJ_DECL(void) pj_log_6(const char *src, const char *format, ...);
385#else
386 #define pj_log_wrapper_6(arg)
387#endif
388
389
390PJ_END_DECL
391
392#endif /* __PJ_LOG_H__ */
393