blob: b7a258f7e199d6eba74b033896dad8686171a59d [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
Benny Prijonob1a3e732009-08-05 10:58:02 +0000223/**
224 * Internal function to be called by pj_init()
225 */
226pj_status_t pj_log_init(void);
Benny Prijonod6e362a2008-07-19 17:53:47 +0000227
Benny Prijono9033e312005-11-21 02:08:39 +0000228#else /* #if PJ_LOG_MAX_LEVEL >= 1 */
229
230/**
231 * Change log output function. The front-end logging functions will call
232 * this function to write the actual message to the desired device.
233 * By default, the front-end functions use pj_log_write() to write
234 * the messages, unless it's changed by calling this function.
235 *
236 * @param func The function that will be called to write the log
237 * messages to the desired device.
238 */
239# define pj_log_set_log_func(func)
240
241/**
242 * Set maximum log level. Application can call this function to set
243 * the desired level of verbosity of the logging messages. The bigger the
244 * value, the more verbose the logging messages will be printed. However,
245 * the maximum level of verbosity can not exceed compile time value of
246 * PJ_LOG_MAX_LEVEL.
247 *
248 * @param level The maximum level of verbosity of the logging
249 * messages (6=very detailed..1=error only, 0=disabled)
250 */
251# define pj_log_set_level(level)
252
253/**
254 * Set log decoration. The log decoration flag controls what are printed
255 * to output device alongside the actual message. For example, application
256 * can specify that date/time information should be displayed with each
257 * log message.
258 *
259 * @param decor Bitmask combination of #pj_log_decoration to control
260 * the layout of the log message.
261 */
262# define pj_log_set_decor(decor)
263
Benny Prijono4381efe2006-03-16 14:24:26 +0000264/**
Benny Prijonod6e362a2008-07-19 17:53:47 +0000265 * Set color of log messages.
266 *
267 * @param level Log level which color will be changed.
268 * @param color Desired color.
269 */
270# define pj_log_set_color(level, color)
271
272/**
Benny Prijono4381efe2006-03-16 14:24:26 +0000273 * Get current maximum log verbositylevel.
274 *
275 * @return Current log maximum level.
276 */
277# define pj_log_get_level() 0
278
279/**
280 * Get current log decoration flag.
281 *
282 * @return Log decoration flag.
283 */
284# define pj_log_get_decor() 0
285
Benny Prijonod6e362a2008-07-19 17:53:47 +0000286/**
287 * Get color of log messages.
288 *
289 * @param level Log level which color will be returned.
290 * @return Log color.
291 */
292# define pj_log_get_color(level) 0
293
Benny Prijono4381efe2006-03-16 14:24:26 +0000294
Benny Prijonob1a3e732009-08-05 10:58:02 +0000295/**
296 * Internal.
297 */
298# define pj_log_init() PJ_SUCCESS
299
Benny Prijono9033e312005-11-21 02:08:39 +0000300#endif /* #if PJ_LOG_MAX_LEVEL >= 1 */
301
302/**
303 * @}
304 */
305
Benny Prijono92ac4472006-07-22 13:42:56 +0000306/* **************************************************************************/
Benny Prijono9033e312005-11-21 02:08:39 +0000307/*
308 * Log functions implementation prototypes.
309 * These functions are called by PJ_LOG macros according to verbosity
310 * level specified when calling the macro. Applications should not normally
311 * need to call these functions directly.
312 */
313
314/**
315 * @def pj_log_wrapper_1(arg)
316 * Internal function to write log with verbosity 1. Will evaluate to
317 * empty expression if PJ_LOG_MAX_LEVEL is below 1.
318 * @param arg Log expression.
319 */
320#if PJ_LOG_MAX_LEVEL >= 1
321 #define pj_log_wrapper_1(arg) pj_log_1 arg
322 /** Internal function. */
323 PJ_DECL(void) pj_log_1(const char *src, const char *format, ...);
324#else
325 #define pj_log_wrapper_1(arg)
326#endif
327
328/**
329 * @def pj_log_wrapper_2(arg)
330 * Internal function to write log with verbosity 2. Will evaluate to
331 * empty expression if PJ_LOG_MAX_LEVEL is below 2.
332 * @param arg Log expression.
333 */
334#if PJ_LOG_MAX_LEVEL >= 2
335 #define pj_log_wrapper_2(arg) pj_log_2 arg
336 /** Internal function. */
337 PJ_DECL(void) pj_log_2(const char *src, const char *format, ...);
338#else
339 #define pj_log_wrapper_2(arg)
340#endif
341
342/**
343 * @def pj_log_wrapper_3(arg)
344 * Internal function to write log with verbosity 3. Will evaluate to
345 * empty expression if PJ_LOG_MAX_LEVEL is below 3.
346 * @param arg Log expression.
347 */
348#if PJ_LOG_MAX_LEVEL >= 3
349 #define pj_log_wrapper_3(arg) pj_log_3 arg
350 /** Internal function. */
351 PJ_DECL(void) pj_log_3(const char *src, const char *format, ...);
352#else
353 #define pj_log_wrapper_3(arg)
354#endif
355
356/**
357 * @def pj_log_wrapper_4(arg)
358 * Internal function to write log with verbosity 4. Will evaluate to
359 * empty expression if PJ_LOG_MAX_LEVEL is below 4.
360 * @param arg Log expression.
361 */
362#if PJ_LOG_MAX_LEVEL >= 4
363 #define pj_log_wrapper_4(arg) pj_log_4 arg
364 /** Internal function. */
365 PJ_DECL(void) pj_log_4(const char *src, const char *format, ...);
366#else
367 #define pj_log_wrapper_4(arg)
368#endif
369
370/**
371 * @def pj_log_wrapper_5(arg)
372 * Internal function to write log with verbosity 5. Will evaluate to
373 * empty expression if PJ_LOG_MAX_LEVEL is below 5.
374 * @param arg Log expression.
375 */
376#if PJ_LOG_MAX_LEVEL >= 5
377 #define pj_log_wrapper_5(arg) pj_log_5 arg
378 /** Internal function. */
379 PJ_DECL(void) pj_log_5(const char *src, const char *format, ...);
380#else
381 #define pj_log_wrapper_5(arg)
382#endif
383
384/**
385 * @def pj_log_wrapper_6(arg)
386 * Internal function to write log with verbosity 6. Will evaluate to
387 * empty expression if PJ_LOG_MAX_LEVEL is below 6.
388 * @param arg Log expression.
389 */
390#if PJ_LOG_MAX_LEVEL >= 6
391 #define pj_log_wrapper_6(arg) pj_log_6 arg
392 /** Internal function. */
393 PJ_DECL(void) pj_log_6(const char *src, const char *format, ...);
394#else
395 #define pj_log_wrapper_6(arg)
396#endif
397
398
399PJ_END_DECL
400
401#endif /* __PJ_LOG_H__ */
402