forked from kamailio/kamailio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dprint.h
425 lines (368 loc) · 12.8 KB
/
dprint.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* @brief Kamailio core :: debug printing
* @ingroup core
* Module: @ref core
*/
#ifndef dprint_h
#define dprint_h
#include <assert.h>
#include <syslog.h>
#include <stdio.h> /* stderr, fprintf() */
#include "compiler_opt.h"
#include "cfg_core.h"
/** if defined the function name will also be logged. */
#ifdef NO_LOG_FUNC_NAME
# undef LOG_FUNC_NAME
#else
/* by default log the function name */
# define LOG_FUNC_NAME
#endif /* NO_LOG_FUNC_NAME */
/* C >= 99 has __func__, older gcc versions have __FUNCTION__ */
#if __STDC_VERSION__ < 199901L
# if __GNUC__ >= 2
# define _FUNC_NAME_ __FUNCTION__
# else
# define _FUNC_NAME_ ""
# undef LOG_FUNC_NAME
# endif
#else
# define _FUNC_NAME_ __func__
#endif
#ifdef NO_DEBUG
# ifdef MOD_NAME
# define LOC_INFO MOD_NAME ": "
# define LOG_MNAME MOD_NAME
# else
# define LOC_INFO "<core>: "
# define LOG_MNAME "core"
# endif
#else
# define XCT2STR(i) #i
# define CT2STR(l) XCT2STR(l)
#
# ifdef MOD_NAME
# define LOC_INFO MOD_NAME " [" __FILE__ ":" CT2STR(__LINE__) "]: "
# define LOG_MNAME MOD_NAME
# else
# define LOC_INFO "<core> [" __FILE__ ":" CT2STR(__LINE__) "]: "
# define LOG_MNAME "core"
# endif
#
# ifdef NO_LOG
# undef NO_LOG
# endif
#endif /* NO_DEBUG */
#define LOG_MNAME_LEN (sizeof(LOG_MNAME)-1)
/*
* Log levels
*/
#define L_NPRL -6 /* (L_MIN-1) to skip printing level prefix */
#define L_MIN -5
#define L_ALERT -5
#define L_BUG -4
#define L_CRIT2 -3 /* like L_CRIT, but adds prefix */
#define L_CRIT -2 /* no prefix added */
#define L_ERR -1
#define L_WARN 0
#define L_NOTICE 1
#define L_INFO 2
#define L_DBG 3
#define L_MAX 3
#define L_OFFSET 42 /* needs to be added and then substracted
because L_WARN may be confused with NULL pointer
(e.g. fixup_dbg_sip_msg) */
/** @brief This is the facility value used to indicate that the caller of the macro
* did not override the facility. Value 0 (the defaul) is LOG_KERN on Linux
*/
#define DEFAULT_FACILITY 0
#define LOG_LEVEL2NAME(level) (log_level_info[(level) - (L_ALERT)].name)
#define LOG2SYSLOG_LEVEL(level) \
(log_level_info[(level) - (L_ALERT)].syslog_level)
/** @brief my_pid(), process_no are from pt.h but we cannot \#include it here
because of circular dependencies */
extern int process_no;
extern int my_pid(void);
/** @brief non-zero if logging to stderr instead to the syslog */
extern int log_stderr;
extern int log_color;
extern char *log_prefix_fmt;
extern str *log_prefix_val;
extern int log_prefix_mode;
extern char *_km_log_engine_type;
extern char *_km_log_engine_data;
typedef void (*km_log_f)(int, const char *, ...);
extern km_log_f _km_log_func;
void km_log_func_set(km_log_f f);
/** @brief maps log levels to their string name and corresponding syslog level */
struct log_level_info {
char *name;
int syslog_level;
};
/** @brief per process debug level handling */
int get_debug_level(char *mname, int mnlen);
int get_debug_facility(char *mname, int mnlen);
void set_local_debug_level(int level);
void set_local_debug_facility(int facility);
void reset_local_debug_level(void);
void reset_local_debug_facility(void);
typedef int (*get_module_debug_level_f)(char *mname, int mnlen, int *mlevel);
typedef int (*get_module_debug_facility_f)(char *mname, int mnlen, int *mfacility);
void set_module_debug_level_cb(get_module_debug_level_f f);
void set_module_debug_facility_cb(get_module_debug_facility_f f);
#define is_printable(level) (get_debug_level(LOG_MNAME, LOG_MNAME_LEN)>=(level))
extern struct log_level_info log_level_info[];
extern char *log_name;
#ifndef NO_SIG_DEBUG
/** @brief protection against "simultaneous" printing from signal handlers */
extern volatile int dprint_crit;
#endif
int str2facility(char *s);
char* facility2str(int fl, int *len);
int log_facility_fixup(void *handle, str *gname, str *name, void **val);
void dprint_color(int level);
void dprint_color_reset(void);
void dprint_color_update(int level, char f, char b);
void dprint_init_colors(void);
void dprint_term_color(char f, char b, str *obuf);
void log_prefix_init(void);
/** @brief
* General logging macros
*
* LOG_(level, prefix, fmt, ...) prints "printf"-formatted log message to
* stderr (if `log_stderr' is non-zero) or to syslog. Note that `fmt' must
* be constant. `prefix' is added to the beginning of the message.
*
* LOG(level, fmt, ...) is same as LOG_() with LOC_INFO prefix.
*/
#ifdef NO_LOG
# ifdef __SUNPRO_C
# define LOG__(facility, level, lname, prefix, ...)
# define LOG_(facility, level, prefix, ...)
# define LOG(level, fmt, ...)
# define LOG_FC(facility, level, ...)
# define LOG_LN(level, lname, ...)
# else
# define LOG__(facility, level, lname, prefix, fmt, args...)
# define LOG_(facility, level, prefix, fmt, args...)
# define LOG(level, fmt, args...)
# define LOG_FC(facility, level, fmt, args...)
# define LOG_LN(level, lname, fmt, args...)
# endif
#else
# ifdef NO_SIG_DEBUG
# define DPRINT_NON_CRIT (1)
# define DPRINT_CRIT_ENTER
# define DPRINT_CRIT_EXIT
# else
# define DPRINT_NON_CRIT (dprint_crit==0)
# define DPRINT_CRIT_ENTER (dprint_crit++)
# define DPRINT_CRIT_EXIT (dprint_crit--)
# endif
# ifdef __SUNPRO_C
# define LOG__(facility, level, lname, prefix, fmt, ...) \
do { \
if (get_debug_level(LOG_MNAME, LOG_MNAME_LEN) >= (level) && \
DPRINT_NON_CRIT) { \
int __llevel; \
__llevel = ((level)<L_ALERT)?L_ALERT:(((level)>L_DBG)?L_DBG:level); \
DPRINT_CRIT_ENTER; \
if (unlikely(log_stderr)) { \
if (unlikely(log_color)) dprint_color(__llevel); \
if(unlikely(log_prefix_val)) { \
fprintf(stderr, "%.*s%2d(%d) %s: %s" fmt, \
log_prefix_val->len, log_prefix_val->s, \
process_no, my_pid(), \
(lname)?(lname):LOG_LEVEL2NAME(__llevel), \
(prefix) , __VA_ARGS__);\
} else { \
fprintf(stderr, "%2d(%d) %s: %s" fmt, \
process_no, my_pid(), \
(lname)?(lname):LOG_LEVEL2NAME(__llevel), \
(prefix) , __VA_ARGS__);\
} \
if (unlikely(log_color)) dprint_color_reset(); \
} else { \
if(unlikely(log_prefix_val)) { \
_km_log_func(LOG2SYSLOG_LEVEL(__llevel) |\
(((facility) != DEFAULT_FACILITY) ? \
(facility) : \
get_debug_facility(LOG_MNAME, LOG_MNAME_LEN)), \
"%.*s%s: %s" fmt,\
log_prefix_val->len, log_prefix_val->s, \
(lname)?(lname):LOG_LEVEL2NAME(__llevel),\
(prefix) , __VA_ARGS__); \
} else { \
_km_log_func(LOG2SYSLOG_LEVEL(__llevel) |\
(((facility) != DEFAULT_FACILITY) ? \
(facility) : \
get_debug_facility(LOG_MNAME, LOG_MNAME_LEN)), \
"%s: %s" fmt,\
(lname)?(lname):LOG_LEVEL2NAME(__llevel),\
(prefix) , __VA_ARGS__); \
} \
} \
DPRINT_CRIT_EXIT; \
} \
} while(0)
# define LOG_(facility, level, ...) \
LOG__(facility, level, NULL, __VA_ARGS__, NULL)
# ifdef LOG_FUNC_NAME
# define LOG(level, ...) \
_LOG(level, __VA_ARGS__, NULL)
# define _LOG(level, fmt, ...) \
LOG_(DEFAULT_FACILITY, (level), LOC_INFO, "%s(): " fmt,\
_FUNC_NAME_, __VA_ARGS__)
# define LOG_FC(facility, level, ...) \
_LOG_FC(facility, level, __VA_ARGS__, NULL)
# define _LOG_FC(facility, level, fmt, ...) \
LOG_((facility), (level), LOC_INFO, "%s(): " fmt , _FUNC_NAME_, __VA_ARGS__)
# define LOG_LN(level, lname, ...) \
_LOG_LN(level, lname, __VA_ARGS__, NULL)
# define _LOG_LN(level, lname, fmt, ...) \
LOG__(DEFAULT_FACILITY, (level), (lname), LOC_INFO, "%s(): " fmt,\
_FUNC_NAME_, __VA_ARGS__)
# else /* LOG_FUNC_NAME */
# define LOG(level, ...) \
LOG_(DEFAULT_FACILITY, (level), LOC_INFO, __VA_ARGS__, NULL)
# define LOG_FC(facility, level, ...) \
LOG_((facility), (level), LOC_INFO, __VA_ARGS__, NULL)
# define LOG_LN(level, lname, ...) \
LOG__(DEFAULT_FACILITY, (level), (lname), LOC_INFO, __VA_ARGS__, NULL)
# endif /* LOG_FUNC_NAME */
# else /* ! __SUNPRO_C */
# define LOG__(facility, level, lname, prefix, fmt, args...) \
do { \
if (get_debug_level(LOG_MNAME, LOG_MNAME_LEN) >= (level) && \
DPRINT_NON_CRIT) { \
int __llevel; \
__llevel = ((level)<L_ALERT)?L_ALERT:(((level)>L_DBG)?L_DBG:level); \
DPRINT_CRIT_ENTER; \
if (unlikely(log_stderr)) { \
if (unlikely(log_color)) dprint_color(__llevel); \
if(unlikely(log_prefix_val)) { \
fprintf(stderr, "%.*s%2d(%d) %s: %s" fmt, \
log_prefix_val->len, log_prefix_val->s, \
process_no, my_pid(), \
(lname)?(lname):LOG_LEVEL2NAME(__llevel), \
(prefix) , ## args);\
} else { \
fprintf(stderr, "%2d(%d) %s: %s" fmt, \
process_no, my_pid(), \
(lname)?(lname):LOG_LEVEL2NAME(__llevel), \
(prefix) , ## args);\
} \
if (unlikely(log_color)) dprint_color_reset(); \
} else { \
if(unlikely(log_prefix_val)) { \
_km_log_func(LOG2SYSLOG_LEVEL(__llevel) |\
(((facility) != DEFAULT_FACILITY) ? \
(facility) : \
get_debug_facility(LOG_MNAME, LOG_MNAME_LEN)), \
"%.*s%s: %s" fmt,\
log_prefix_val->len, log_prefix_val->s, \
(lname)?(lname):LOG_LEVEL2NAME(__llevel),\
(prefix) , ## args); \
} else { \
_km_log_func(LOG2SYSLOG_LEVEL(__llevel) |\
(((facility) != DEFAULT_FACILITY) ? \
(facility) : \
get_debug_facility(LOG_MNAME, LOG_MNAME_LEN)), \
"%s: %s" fmt,\
(lname)?(lname):LOG_LEVEL2NAME(__llevel),\
(prefix) , ## args); \
} \
} \
DPRINT_CRIT_EXIT; \
} \
} while(0)
# define LOG_(facility, level, prefix, fmt, args...) \
LOG__(facility, level, NULL, prefix, fmt, ## args)
# ifdef LOG_FUNC_NAME
# define LOG(level, fmt, args...) \
LOG_(DEFAULT_FACILITY, (level), LOC_INFO, "%s(): " fmt ,\
_FUNC_NAME_, ## args)
# define LOG_FC(facility, level, fmt, args...) \
LOG_((facility), (level), LOC_INFO, "%s(): " fmt , _FUNC_NAME_, ## args)
# define LOG_LN(level, lname, fmt, args...) \
LOG__(DEFAULT_FACILITY, (level), (lname), LOC_INFO, "%s(): " fmt ,\
_FUNC_NAME_, ## args)
# else /* LOG_FUNC_NAME */
# define LOG(level, fmt, args...) \
LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt , ## args)
# define LOG_FC(facility, level, fmt, args...) \
LOG_((facility), (level), LOC_INFO, fmt , ## args)
# define LOG_LN(level, lname, fmt, args...) \
LOG__(DEFAULT_FACILITY, (level), (lname), LOC_INFO, fmt , ## args)
# endif /* LOG_FUNC_NAME */
# endif /* __SUNPRO_C */
#endif /* NO_LOG */
/** @name SimpleLog
* Simplier, prefered logging macros for constant log level
*/
/*@ { */
#ifdef __SUNPRO_C
# define NPRL(...) LOG(L_NPRL, __VA_ARGS__)
# define ALERT(...) LOG(L_ALERT, __VA_ARGS__)
# define BUG(...) LOG(L_BUG, __VA_ARGS__)
# define ERR(...) LOG(L_ERR, __VA_ARGS__)
# define WARN(...) LOG(L_WARN, __VA_ARGS__)
# define NOTICE(...) LOG(L_NOTICE, __VA_ARGS__)
# define INFO(...) LOG(L_INFO, __VA_ARGS__)
# define CRIT(...) LOG(L_CRIT2, __VA_ARGS__)
# ifdef NO_DEBUG
# define DBG(...)
# else
# define DBG(...) LOG(L_DBG, __VA_ARGS__)
# endif
/*@ } */
/* obsolete, do not use */
# define DEBUG(...) DBG(__VA_ARGS__)
#else /* ! __SUNPRO_C */
# define NPRL(fmt, args...) LOG(L_NPRL, fmt , ## args)
# define ALERT(fmt, args...) LOG(L_ALERT, fmt , ## args)
# define BUG(fmt, args...) LOG(L_BUG, fmt , ## args)
# define ERR(fmt, args...) LOG(L_ERR, fmt , ## args)
# define WARN(fmt, args...) LOG(L_WARN, fmt , ## args)
# define NOTICE(fmt, args...) LOG(L_NOTICE, fmt , ## args)
# define INFO(fmt, args...) LOG(L_INFO, fmt , ## args)
# define CRIT(fmt, args...) LOG(L_CRIT2, fmt , ## args)
# ifdef NO_DEBUG
# define DBG(fmt, args...)
# else
# define DBG(fmt, args...) LOG(L_DBG, fmt , ## args)
# endif
/* obsolete, do not use */
# define DEBUG(fmt, args...) DBG(fmt , ## args)
#endif /* __SUNPRO_C */
/* kamailio/openser compatibility */
#define LM_GEN1 LOG
#define LM_GEN2 LOG_FC
#define LM_NPRL NPRL
#define LM_ALERT ALERT
#define LM_CRIT CRIT
#define LM_ERR ERR
#define LM_WARN WARN
#define LM_NOTICE NOTICE
#define LM_INFO INFO
#define LM_DBG DEBUG
#endif /* !dprint_h */