blob: 76d609ddb79ab2c13e0c45b5d31bbe55b43fac5a [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
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
30PJ_BEGIN_DECL
31
32/**
33 * @defgroup PJ_MISC Miscelaneous
Benny Prijono9033e312005-11-21 02:08:39 +000034 */
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 * - @ref page_pjlib_samples_log_c.
63 *
64 */
65
66/**
67 * Log decoration flag, to be specified with #pj_log_set_decor().
68 */
69enum 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 [default: no] */
73 PJ_LOG_HAS_MONTH = 4, /**< Include month [default: no] */
74 PJ_LOG_HAS_DAY_OF_MON = 8, /**< Include day of month [default: no] */
75 PJ_LOG_HAS_TIME = 16, /**< Include time [default: 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].*/
Benny Prijono92ac4472006-07-22 13:42:56 +000079 PJ_LOG_HAS_CR = 256 /**< Include carriage return [no]. */
Benny Prijono9033e312005-11-21 02:08:39 +000080};
81
82/**
83 * Write log message.
84 * This is the main macro used to write text to the logging backend.
85 *
86 * @param level The logging verbosity level. Lower number indicates higher
87 * importance, with level zero indicates fatal error. Only
88 * numeral argument is permitted (e.g. not variable).
89 * @param arg Enclosed 'printf' like arguments, with the first
90 * argument is the sender, the second argument is format
91 * string and the following arguments are variable number of
92 * arguments suitable for the format string.
93 *
94 * Sample:
95 * \verbatim
96 PJ_LOG(2, (__FILE__, "current value is %d", value));
97 \endverbatim
98 * @hideinitializer
99 */
Benny Prijono505e82e2007-03-05 21:08:01 +0000100#define PJ_LOG(level,arg) do { \
101 if (level <= pj_log_get_level()) \
102 pj_log_wrapper_##level(arg); \
103 } while (0)
Benny Prijono9033e312005-11-21 02:08:39 +0000104
105/**
106 * Signature for function to be registered to the logging subsystem to
107 * write the actual log message to some output device.
108 *
109 * @param level Log level.
Benny Prijonoab168dd2007-12-12 14:06:31 +0000110 * @param data Log message, which will be NULL terminated.
Benny Prijono9033e312005-11-21 02:08:39 +0000111 * @param len Message length.
112 */
113typedef void pj_log_func(int level, const char *data, int len);
114
115/**
116 * Default logging writer function used by front end logger function.
Benny Prijonoccf95622006-02-07 18:48:01 +0000117 * This function will print the log message to stdout only.
Benny Prijono9033e312005-11-21 02:08:39 +0000118 * Application normally should NOT need to call this function, but
119 * rather use the PJ_LOG macro.
120 *
121 * @param level Log level.
122 * @param buffer Log message.
123 * @param len Message length.
124 */
125PJ_DECL(void) pj_log_write(int level, const char *buffer, int len);
126
127
128#if PJ_LOG_MAX_LEVEL >= 1
129
130/**
131 * Write to log.
132 *
133 * @param sender Source of the message.
134 * @param level Verbosity level.
135 * @param format Format.
136 * @param marker Marker.
137 */
138PJ_DECL(void) pj_log(const char *sender, int level,
139 const char *format, va_list marker);
140
141/**
142 * Change log output function. The front-end logging functions will call
143 * this function to write the actual message to the desired device.
144 * By default, the front-end functions use pj_log_write() to write
145 * the messages, unless it's changed by calling this function.
146 *
147 * @param func The function that will be called to write the log
148 * messages to the desired device.
149 */
150PJ_DECL(void) pj_log_set_log_func( pj_log_func *func );
151
152/**
153 * Get the current log output function that is used to write log messages.
154 *
155 * @return Current log output function.
156 */
157PJ_DECL(pj_log_func*) pj_log_get_log_func(void);
158
159/**
160 * Set maximum log level. Application can call this function to set
161 * the desired level of verbosity of the logging messages. The bigger the
162 * value, the more verbose the logging messages will be printed. However,
163 * the maximum level of verbosity can not exceed compile time value of
164 * PJ_LOG_MAX_LEVEL.
165 *
166 * @param level The maximum level of verbosity of the logging
167 * messages (6=very detailed..1=error only, 0=disabled)
168 */
169PJ_DECL(void) pj_log_set_level(int level);
170
171/**
172 * Get current maximum log verbositylevel.
173 *
174 * @return Current log maximum level.
175 */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000176#if 1
Benny Prijono9033e312005-11-21 02:08:39 +0000177PJ_DECL(int) pj_log_get_level(void);
Benny Prijono505e82e2007-03-05 21:08:01 +0000178#else
Benny Prijono8ab968f2007-07-20 08:08:30 +0000179PJ_DECL_DATA(int) pj_log_max_level;
Benny Prijono505e82e2007-03-05 21:08:01 +0000180#define pj_log_get_level() pj_log_max_level
181#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000182
183/**
184 * Set log decoration. The log decoration flag controls what are printed
185 * to output device alongside the actual message. For example, application
186 * can specify that date/time information should be displayed with each
187 * log message.
188 *
189 * @param decor Bitmask combination of #pj_log_decoration to control
190 * the layout of the log message.
191 */
192PJ_DECL(void) pj_log_set_decor(unsigned decor);
193
194/**
195 * Get current log decoration flag.
196 *
197 * @return Log decoration flag.
198 */
199PJ_DECL(unsigned) pj_log_get_decor(void);
200
201
202#else /* #if PJ_LOG_MAX_LEVEL >= 1 */
203
204/**
205 * Change log output function. The front-end logging functions will call
206 * this function to write the actual message to the desired device.
207 * By default, the front-end functions use pj_log_write() to write
208 * the messages, unless it's changed by calling this function.
209 *
210 * @param func The function that will be called to write the log
211 * messages to the desired device.
212 */
213# define pj_log_set_log_func(func)
214
215/**
216 * Set maximum log level. Application can call this function to set
217 * the desired level of verbosity of the logging messages. The bigger the
218 * value, the more verbose the logging messages will be printed. However,
219 * the maximum level of verbosity can not exceed compile time value of
220 * PJ_LOG_MAX_LEVEL.
221 *
222 * @param level The maximum level of verbosity of the logging
223 * messages (6=very detailed..1=error only, 0=disabled)
224 */
225# define pj_log_set_level(level)
226
227/**
228 * Set log decoration. The log decoration flag controls what are printed
229 * to output device alongside the actual message. For example, application
230 * can specify that date/time information should be displayed with each
231 * log message.
232 *
233 * @param decor Bitmask combination of #pj_log_decoration to control
234 * the layout of the log message.
235 */
236# define pj_log_set_decor(decor)
237
Benny Prijono4381efe2006-03-16 14:24:26 +0000238/**
239 * Get current maximum log verbositylevel.
240 *
241 * @return Current log maximum level.
242 */
243# define pj_log_get_level() 0
244
245/**
246 * Get current log decoration flag.
247 *
248 * @return Log decoration flag.
249 */
250# define pj_log_get_decor() 0
251
252
Benny Prijono9033e312005-11-21 02:08:39 +0000253#endif /* #if PJ_LOG_MAX_LEVEL >= 1 */
254
255/**
256 * @}
257 */
258
Benny Prijono92ac4472006-07-22 13:42:56 +0000259/* **************************************************************************/
Benny Prijono9033e312005-11-21 02:08:39 +0000260/*
261 * Log functions implementation prototypes.
262 * These functions are called by PJ_LOG macros according to verbosity
263 * level specified when calling the macro. Applications should not normally
264 * need to call these functions directly.
265 */
266
267/**
268 * @def pj_log_wrapper_1(arg)
269 * Internal function to write log with verbosity 1. Will evaluate to
270 * empty expression if PJ_LOG_MAX_LEVEL is below 1.
271 * @param arg Log expression.
272 */
273#if PJ_LOG_MAX_LEVEL >= 1
274 #define pj_log_wrapper_1(arg) pj_log_1 arg
275 /** Internal function. */
276 PJ_DECL(void) pj_log_1(const char *src, const char *format, ...);
277#else
278 #define pj_log_wrapper_1(arg)
279#endif
280
281/**
282 * @def pj_log_wrapper_2(arg)
283 * Internal function to write log with verbosity 2. Will evaluate to
284 * empty expression if PJ_LOG_MAX_LEVEL is below 2.
285 * @param arg Log expression.
286 */
287#if PJ_LOG_MAX_LEVEL >= 2
288 #define pj_log_wrapper_2(arg) pj_log_2 arg
289 /** Internal function. */
290 PJ_DECL(void) pj_log_2(const char *src, const char *format, ...);
291#else
292 #define pj_log_wrapper_2(arg)
293#endif
294
295/**
296 * @def pj_log_wrapper_3(arg)
297 * Internal function to write log with verbosity 3. Will evaluate to
298 * empty expression if PJ_LOG_MAX_LEVEL is below 3.
299 * @param arg Log expression.
300 */
301#if PJ_LOG_MAX_LEVEL >= 3
302 #define pj_log_wrapper_3(arg) pj_log_3 arg
303 /** Internal function. */
304 PJ_DECL(void) pj_log_3(const char *src, const char *format, ...);
305#else
306 #define pj_log_wrapper_3(arg)
307#endif
308
309/**
310 * @def pj_log_wrapper_4(arg)
311 * Internal function to write log with verbosity 4. Will evaluate to
312 * empty expression if PJ_LOG_MAX_LEVEL is below 4.
313 * @param arg Log expression.
314 */
315#if PJ_LOG_MAX_LEVEL >= 4
316 #define pj_log_wrapper_4(arg) pj_log_4 arg
317 /** Internal function. */
318 PJ_DECL(void) pj_log_4(const char *src, const char *format, ...);
319#else
320 #define pj_log_wrapper_4(arg)
321#endif
322
323/**
324 * @def pj_log_wrapper_5(arg)
325 * Internal function to write log with verbosity 5. Will evaluate to
326 * empty expression if PJ_LOG_MAX_LEVEL is below 5.
327 * @param arg Log expression.
328 */
329#if PJ_LOG_MAX_LEVEL >= 5
330 #define pj_log_wrapper_5(arg) pj_log_5 arg
331 /** Internal function. */
332 PJ_DECL(void) pj_log_5(const char *src, const char *format, ...);
333#else
334 #define pj_log_wrapper_5(arg)
335#endif
336
337/**
338 * @def pj_log_wrapper_6(arg)
339 * Internal function to write log with verbosity 6. Will evaluate to
340 * empty expression if PJ_LOG_MAX_LEVEL is below 6.
341 * @param arg Log expression.
342 */
343#if PJ_LOG_MAX_LEVEL >= 6
344 #define pj_log_wrapper_6(arg) pj_log_6 arg
345 /** Internal function. */
346 PJ_DECL(void) pj_log_6(const char *src, const char *format, ...);
347#else
348 #define pj_log_wrapper_6(arg)
349#endif
350
351
352PJ_END_DECL
353
354#endif /* __PJ_LOG_H__ */
355