blob: 48ac8d3f1aadd9b6114a77a36625564217deb7b1 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
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
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 */
101#define PJ_LOG(level,arg) pj_log_wrapper_##level(arg)
102
103/**
104 * Signature for function to be registered to the logging subsystem to
105 * write the actual log message to some output device.
106 *
107 * @param level Log level.
108 * @param data Log message.
109 * @param len Message length.
110 */
111typedef void pj_log_func(int level, const char *data, int len);
112
113/**
114 * Default logging writer function used by front end logger function.
Benny Prijonoccf95622006-02-07 18:48:01 +0000115 * This function will print the log message to stdout only.
Benny Prijono9033e312005-11-21 02:08:39 +0000116 * Application normally should NOT need to call this function, but
117 * rather use the PJ_LOG macro.
118 *
119 * @param level Log level.
120 * @param buffer Log message.
121 * @param len Message length.
122 */
123PJ_DECL(void) pj_log_write(int level, const char *buffer, int len);
124
125
126#if PJ_LOG_MAX_LEVEL >= 1
127
128/**
129 * Write to log.
130 *
131 * @param sender Source of the message.
132 * @param level Verbosity level.
133 * @param format Format.
134 * @param marker Marker.
135 */
136PJ_DECL(void) pj_log(const char *sender, int level,
137 const char *format, va_list marker);
138
139/**
140 * Change log output function. The front-end logging functions will call
141 * this function to write the actual message to the desired device.
142 * By default, the front-end functions use pj_log_write() to write
143 * the messages, unless it's changed by calling this function.
144 *
145 * @param func The function that will be called to write the log
146 * messages to the desired device.
147 */
148PJ_DECL(void) pj_log_set_log_func( pj_log_func *func );
149
150/**
151 * Get the current log output function that is used to write log messages.
152 *
153 * @return Current log output function.
154 */
155PJ_DECL(pj_log_func*) pj_log_get_log_func(void);
156
157/**
158 * Set maximum log level. Application can call this function to set
159 * the desired level of verbosity of the logging messages. The bigger the
160 * value, the more verbose the logging messages will be printed. However,
161 * the maximum level of verbosity can not exceed compile time value of
162 * PJ_LOG_MAX_LEVEL.
163 *
164 * @param level The maximum level of verbosity of the logging
165 * messages (6=very detailed..1=error only, 0=disabled)
166 */
167PJ_DECL(void) pj_log_set_level(int level);
168
169/**
170 * Get current maximum log verbositylevel.
171 *
172 * @return Current log maximum level.
173 */
174PJ_DECL(int) pj_log_get_level(void);
175
176/**
177 * Set log decoration. The log decoration flag controls what are printed
178 * to output device alongside the actual message. For example, application
179 * can specify that date/time information should be displayed with each
180 * log message.
181 *
182 * @param decor Bitmask combination of #pj_log_decoration to control
183 * the layout of the log message.
184 */
185PJ_DECL(void) pj_log_set_decor(unsigned decor);
186
187/**
188 * Get current log decoration flag.
189 *
190 * @return Log decoration flag.
191 */
192PJ_DECL(unsigned) pj_log_get_decor(void);
193
194
195#else /* #if PJ_LOG_MAX_LEVEL >= 1 */
196
197/**
198 * Change log output function. The front-end logging functions will call
199 * this function to write the actual message to the desired device.
200 * By default, the front-end functions use pj_log_write() to write
201 * the messages, unless it's changed by calling this function.
202 *
203 * @param func The function that will be called to write the log
204 * messages to the desired device.
205 */
206# define pj_log_set_log_func(func)
207
208/**
209 * Set maximum log level. Application can call this function to set
210 * the desired level of verbosity of the logging messages. The bigger the
211 * value, the more verbose the logging messages will be printed. However,
212 * the maximum level of verbosity can not exceed compile time value of
213 * PJ_LOG_MAX_LEVEL.
214 *
215 * @param level The maximum level of verbosity of the logging
216 * messages (6=very detailed..1=error only, 0=disabled)
217 */
218# define pj_log_set_level(level)
219
220/**
221 * Set log decoration. The log decoration flag controls what are printed
222 * to output device alongside the actual message. For example, application
223 * can specify that date/time information should be displayed with each
224 * log message.
225 *
226 * @param decor Bitmask combination of #pj_log_decoration to control
227 * the layout of the log message.
228 */
229# define pj_log_set_decor(decor)
230
Benny Prijono4381efe2006-03-16 14:24:26 +0000231/**
232 * Get current maximum log verbositylevel.
233 *
234 * @return Current log maximum level.
235 */
236# define pj_log_get_level() 0
237
238/**
239 * Get current log decoration flag.
240 *
241 * @return Log decoration flag.
242 */
243# define pj_log_get_decor() 0
244
245
Benny Prijono9033e312005-11-21 02:08:39 +0000246#endif /* #if PJ_LOG_MAX_LEVEL >= 1 */
247
248/**
249 * @}
250 */
251
Benny Prijono92ac4472006-07-22 13:42:56 +0000252/* **************************************************************************/
Benny Prijono9033e312005-11-21 02:08:39 +0000253/*
254 * Log functions implementation prototypes.
255 * These functions are called by PJ_LOG macros according to verbosity
256 * level specified when calling the macro. Applications should not normally
257 * need to call these functions directly.
258 */
259
260/**
261 * @def pj_log_wrapper_1(arg)
262 * Internal function to write log with verbosity 1. Will evaluate to
263 * empty expression if PJ_LOG_MAX_LEVEL is below 1.
264 * @param arg Log expression.
265 */
266#if PJ_LOG_MAX_LEVEL >= 1
267 #define pj_log_wrapper_1(arg) pj_log_1 arg
268 /** Internal function. */
269 PJ_DECL(void) pj_log_1(const char *src, const char *format, ...);
270#else
271 #define pj_log_wrapper_1(arg)
272#endif
273
274/**
275 * @def pj_log_wrapper_2(arg)
276 * Internal function to write log with verbosity 2. Will evaluate to
277 * empty expression if PJ_LOG_MAX_LEVEL is below 2.
278 * @param arg Log expression.
279 */
280#if PJ_LOG_MAX_LEVEL >= 2
281 #define pj_log_wrapper_2(arg) pj_log_2 arg
282 /** Internal function. */
283 PJ_DECL(void) pj_log_2(const char *src, const char *format, ...);
284#else
285 #define pj_log_wrapper_2(arg)
286#endif
287
288/**
289 * @def pj_log_wrapper_3(arg)
290 * Internal function to write log with verbosity 3. Will evaluate to
291 * empty expression if PJ_LOG_MAX_LEVEL is below 3.
292 * @param arg Log expression.
293 */
294#if PJ_LOG_MAX_LEVEL >= 3
295 #define pj_log_wrapper_3(arg) pj_log_3 arg
296 /** Internal function. */
297 PJ_DECL(void) pj_log_3(const char *src, const char *format, ...);
298#else
299 #define pj_log_wrapper_3(arg)
300#endif
301
302/**
303 * @def pj_log_wrapper_4(arg)
304 * Internal function to write log with verbosity 4. Will evaluate to
305 * empty expression if PJ_LOG_MAX_LEVEL is below 4.
306 * @param arg Log expression.
307 */
308#if PJ_LOG_MAX_LEVEL >= 4
309 #define pj_log_wrapper_4(arg) pj_log_4 arg
310 /** Internal function. */
311 PJ_DECL(void) pj_log_4(const char *src, const char *format, ...);
312#else
313 #define pj_log_wrapper_4(arg)
314#endif
315
316/**
317 * @def pj_log_wrapper_5(arg)
318 * Internal function to write log with verbosity 5. Will evaluate to
319 * empty expression if PJ_LOG_MAX_LEVEL is below 5.
320 * @param arg Log expression.
321 */
322#if PJ_LOG_MAX_LEVEL >= 5
323 #define pj_log_wrapper_5(arg) pj_log_5 arg
324 /** Internal function. */
325 PJ_DECL(void) pj_log_5(const char *src, const char *format, ...);
326#else
327 #define pj_log_wrapper_5(arg)
328#endif
329
330/**
331 * @def pj_log_wrapper_6(arg)
332 * Internal function to write log with verbosity 6. Will evaluate to
333 * empty expression if PJ_LOG_MAX_LEVEL is below 6.
334 * @param arg Log expression.
335 */
336#if PJ_LOG_MAX_LEVEL >= 6
337 #define pj_log_wrapper_6(arg) pj_log_6 arg
338 /** Internal function. */
339 PJ_DECL(void) pj_log_6(const char *src, const char *format, ...);
340#else
341 #define pj_log_wrapper_6(arg)
342#endif
343
344
345PJ_END_DECL
346
347#endif /* __PJ_LOG_H__ */
348