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