blob: 3f001e16c629f1c10fefcf138791a3759c944968 [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] */
83 PJ_LOG_HAS_LEVEL_TEXT = 2048 /**< Include level text string [no] */
Benny Prijono9033e312005-11-21 02:08:39 +000084};
85
86/**
87 * Write log message.
88 * This is the main macro used to write text to the logging backend.
89 *
90 * @param level The logging verbosity level. Lower number indicates higher
91 * importance, with level zero indicates fatal error. Only
92 * numeral argument is permitted (e.g. not variable).
93 * @param arg Enclosed 'printf' like arguments, with the first
94 * argument is the sender, the second argument is format
95 * string and the following arguments are variable number of
96 * arguments suitable for the format string.
97 *
98 * Sample:
99 * \verbatim
100 PJ_LOG(2, (__FILE__, "current value is %d", value));
101 \endverbatim
102 * @hideinitializer
103 */
Benny Prijono505e82e2007-03-05 21:08:01 +0000104#define PJ_LOG(level,arg) do { \
105 if (level <= pj_log_get_level()) \
106 pj_log_wrapper_##level(arg); \
107 } while (0)
Benny Prijono9033e312005-11-21 02:08:39 +0000108
109/**
110 * Signature for function to be registered to the logging subsystem to
111 * write the actual log message to some output device.
112 *
113 * @param level Log level.
Benny Prijonoab168dd2007-12-12 14:06:31 +0000114 * @param data Log message, which will be NULL terminated.
Benny Prijono9033e312005-11-21 02:08:39 +0000115 * @param len Message length.
116 */
117typedef void pj_log_func(int level, const char *data, int len);
118
119/**
120 * Default logging writer function used by front end logger function.
Benny Prijonoccf95622006-02-07 18:48:01 +0000121 * This function will print the log message to stdout only.
Benny Prijono9033e312005-11-21 02:08:39 +0000122 * Application normally should NOT need to call this function, but
123 * rather use the PJ_LOG macro.
124 *
125 * @param level Log level.
126 * @param buffer Log message.
127 * @param len Message length.
128 */
129PJ_DECL(void) pj_log_write(int level, const char *buffer, int len);
130
131
132#if PJ_LOG_MAX_LEVEL >= 1
133
134/**
135 * Write to log.
136 *
137 * @param sender Source of the message.
138 * @param level Verbosity level.
139 * @param format Format.
140 * @param marker Marker.
141 */
142PJ_DECL(void) pj_log(const char *sender, int level,
143 const char *format, va_list marker);
144
145/**
146 * Change log output function. The front-end logging functions will call
147 * this function to write the actual message to the desired device.
148 * By default, the front-end functions use pj_log_write() to write
149 * the messages, unless it's changed by calling this function.
150 *
151 * @param func The function that will be called to write the log
152 * messages to the desired device.
153 */
154PJ_DECL(void) pj_log_set_log_func( pj_log_func *func );
155
156/**
157 * Get the current log output function that is used to write log messages.
158 *
159 * @return Current log output function.
160 */
161PJ_DECL(pj_log_func*) pj_log_get_log_func(void);
162
163/**
164 * Set maximum log level. Application can call this function to set
165 * the desired level of verbosity of the logging messages. The bigger the
166 * value, the more verbose the logging messages will be printed. However,
167 * the maximum level of verbosity can not exceed compile time value of
168 * PJ_LOG_MAX_LEVEL.
169 *
170 * @param level The maximum level of verbosity of the logging
171 * messages (6=very detailed..1=error only, 0=disabled)
172 */
173PJ_DECL(void) pj_log_set_level(int level);
174
175/**
176 * Get current maximum log verbositylevel.
177 *
178 * @return Current log maximum level.
179 */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000180#if 1
Benny Prijono9033e312005-11-21 02:08:39 +0000181PJ_DECL(int) pj_log_get_level(void);
Benny Prijono505e82e2007-03-05 21:08:01 +0000182#else
Benny Prijono8ab968f2007-07-20 08:08:30 +0000183PJ_DECL_DATA(int) pj_log_max_level;
Benny Prijono505e82e2007-03-05 21:08:01 +0000184#define pj_log_get_level() pj_log_max_level
185#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000186
187/**
188 * Set log decoration. The log decoration flag controls what are printed
189 * to output device alongside the actual message. For example, application
190 * can specify that date/time information should be displayed with each
191 * log message.
192 *
193 * @param decor Bitmask combination of #pj_log_decoration to control
194 * the layout of the log message.
195 */
196PJ_DECL(void) pj_log_set_decor(unsigned decor);
197
198/**
199 * Get current log decoration flag.
200 *
201 * @return Log decoration flag.
202 */
203PJ_DECL(unsigned) pj_log_get_decor(void);
204
205
Benny Prijonod6e362a2008-07-19 17:53:47 +0000206/**
207 * Set color of log messages.
208 *
209 * @param level Log level which color will be changed.
210 * @param color Desired color.
211 */
212PJ_DECL(void) pj_log_set_color(int level, pj_color_t color);
213
214/**
215 * Get color of log messages.
216 *
217 * @param level Log level which color will be returned.
218 * @return Log color.
219 */
220PJ_DECL(pj_color_t) pj_log_get_color(int level);
221
222
Benny Prijono9033e312005-11-21 02:08:39 +0000223#else /* #if PJ_LOG_MAX_LEVEL >= 1 */
224
225/**
226 * Change log output function. The front-end logging functions will call
227 * this function to write the actual message to the desired device.
228 * By default, the front-end functions use pj_log_write() to write
229 * the messages, unless it's changed by calling this function.
230 *
231 * @param func The function that will be called to write the log
232 * messages to the desired device.
233 */
234# define pj_log_set_log_func(func)
235
236/**
237 * Set maximum log level. Application can call this function to set
238 * the desired level of verbosity of the logging messages. The bigger the
239 * value, the more verbose the logging messages will be printed. However,
240 * the maximum level of verbosity can not exceed compile time value of
241 * PJ_LOG_MAX_LEVEL.
242 *
243 * @param level The maximum level of verbosity of the logging
244 * messages (6=very detailed..1=error only, 0=disabled)
245 */
246# define pj_log_set_level(level)
247
248/**
249 * Set log decoration. The log decoration flag controls what are printed
250 * to output device alongside the actual message. For example, application
251 * can specify that date/time information should be displayed with each
252 * log message.
253 *
254 * @param decor Bitmask combination of #pj_log_decoration to control
255 * the layout of the log message.
256 */
257# define pj_log_set_decor(decor)
258
Benny Prijono4381efe2006-03-16 14:24:26 +0000259/**
Benny Prijonod6e362a2008-07-19 17:53:47 +0000260 * Set color of log messages.
261 *
262 * @param level Log level which color will be changed.
263 * @param color Desired color.
264 */
265# define pj_log_set_color(level, color)
266
267/**
Benny Prijono4381efe2006-03-16 14:24:26 +0000268 * Get current maximum log verbositylevel.
269 *
270 * @return Current log maximum level.
271 */
272# define pj_log_get_level() 0
273
274/**
275 * Get current log decoration flag.
276 *
277 * @return Log decoration flag.
278 */
279# define pj_log_get_decor() 0
280
Benny Prijonod6e362a2008-07-19 17:53:47 +0000281/**
282 * Get color of log messages.
283 *
284 * @param level Log level which color will be returned.
285 * @return Log color.
286 */
287# define pj_log_get_color(level) 0
288
Benny Prijono4381efe2006-03-16 14:24:26 +0000289
Benny Prijono9033e312005-11-21 02:08:39 +0000290#endif /* #if PJ_LOG_MAX_LEVEL >= 1 */
291
292/**
293 * @}
294 */
295
Benny Prijono92ac4472006-07-22 13:42:56 +0000296/* **************************************************************************/
Benny Prijono9033e312005-11-21 02:08:39 +0000297/*
298 * Log functions implementation prototypes.
299 * These functions are called by PJ_LOG macros according to verbosity
300 * level specified when calling the macro. Applications should not normally
301 * need to call these functions directly.
302 */
303
304/**
305 * @def pj_log_wrapper_1(arg)
306 * Internal function to write log with verbosity 1. Will evaluate to
307 * empty expression if PJ_LOG_MAX_LEVEL is below 1.
308 * @param arg Log expression.
309 */
310#if PJ_LOG_MAX_LEVEL >= 1
311 #define pj_log_wrapper_1(arg) pj_log_1 arg
312 /** Internal function. */
313 PJ_DECL(void) pj_log_1(const char *src, const char *format, ...);
314#else
315 #define pj_log_wrapper_1(arg)
316#endif
317
318/**
319 * @def pj_log_wrapper_2(arg)
320 * Internal function to write log with verbosity 2. Will evaluate to
321 * empty expression if PJ_LOG_MAX_LEVEL is below 2.
322 * @param arg Log expression.
323 */
324#if PJ_LOG_MAX_LEVEL >= 2
325 #define pj_log_wrapper_2(arg) pj_log_2 arg
326 /** Internal function. */
327 PJ_DECL(void) pj_log_2(const char *src, const char *format, ...);
328#else
329 #define pj_log_wrapper_2(arg)
330#endif
331
332/**
333 * @def pj_log_wrapper_3(arg)
334 * Internal function to write log with verbosity 3. Will evaluate to
335 * empty expression if PJ_LOG_MAX_LEVEL is below 3.
336 * @param arg Log expression.
337 */
338#if PJ_LOG_MAX_LEVEL >= 3
339 #define pj_log_wrapper_3(arg) pj_log_3 arg
340 /** Internal function. */
341 PJ_DECL(void) pj_log_3(const char *src, const char *format, ...);
342#else
343 #define pj_log_wrapper_3(arg)
344#endif
345
346/**
347 * @def pj_log_wrapper_4(arg)
348 * Internal function to write log with verbosity 4. Will evaluate to
349 * empty expression if PJ_LOG_MAX_LEVEL is below 4.
350 * @param arg Log expression.
351 */
352#if PJ_LOG_MAX_LEVEL >= 4
353 #define pj_log_wrapper_4(arg) pj_log_4 arg
354 /** Internal function. */
355 PJ_DECL(void) pj_log_4(const char *src, const char *format, ...);
356#else
357 #define pj_log_wrapper_4(arg)
358#endif
359
360/**
361 * @def pj_log_wrapper_5(arg)
362 * Internal function to write log with verbosity 5. Will evaluate to
363 * empty expression if PJ_LOG_MAX_LEVEL is below 5.
364 * @param arg Log expression.
365 */
366#if PJ_LOG_MAX_LEVEL >= 5
367 #define pj_log_wrapper_5(arg) pj_log_5 arg
368 /** Internal function. */
369 PJ_DECL(void) pj_log_5(const char *src, const char *format, ...);
370#else
371 #define pj_log_wrapper_5(arg)
372#endif
373
374/**
375 * @def pj_log_wrapper_6(arg)
376 * Internal function to write log with verbosity 6. Will evaluate to
377 * empty expression if PJ_LOG_MAX_LEVEL is below 6.
378 * @param arg Log expression.
379 */
380#if PJ_LOG_MAX_LEVEL >= 6
381 #define pj_log_wrapper_6(arg) pj_log_6 arg
382 /** Internal function. */
383 PJ_DECL(void) pj_log_6(const char *src, const char *format, ...);
384#else
385 #define pj_log_wrapper_6(arg)
386#endif
387
388
389PJ_END_DECL
390
391#endif /* __PJ_LOG_H__ */
392