forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dprint.h
445 lines (382 loc) · 11.1 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of opensips, a free SIP server.
*
* opensips 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
*
* opensips 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 OpenSIPS Debug console print functions
* \see syslog.h
*/
/*! \page DebugLogFunction Description of the logging functions:
*
* A) macros to log on a predefine log level and with standard prefix
* for with additional info: [time]
* No dynamic FMT is accepted (due macro processing).
* LM_ALERT( fmt, ....)
* LM_CRIT( fmt, ....)
* LM_ERR( fmt, ...)
* LM_WARN( fmt, ...)
* LM_NOTICE( fmt, ...)
* LM_INFO( fmt, ...)
* LM_DBG( fmt, ...)
* B) macros for generic logging ; no additional information is added;
* Works with dynamic FMT.
* LM_GEN1( log_level, fmt, ....)
* LM_GEN2( log_facility, log_level, fmt, ...)
*/
#ifndef dprint_h
#define dprint_h
#include <syslog.h>
#include <time.h>
#define L_ALERT -3 /*!< Alert level */
#define L_CRIT -2 /*!< Critical level */
#define L_ERR -1 /*!< Error level */
#define L_WARN 1 /*!< Warning level */
#define L_NOTICE 2 /*!< Notice level */
#define L_INFO 3 /*!< Info level */
#define L_DBG 4 /*!< Debug level */
#ifdef __SUNPRO_C
#define DP_PREFIX
#else
#define DP_PREFIX "%s [%d] "
#endif
#define DP_ALERT_TEXT "ALERT:"
#define DP_CRIT_TEXT "CRITICAL:"
#define DP_ERR_TEXT "ERROR:"
#define DP_WARN_TEXT "WARNING:"
#define DP_NOTICE_TEXT "NOTICE:"
#define DP_INFO_TEXT "INFO:"
#define DP_DBG_TEXT "DBG:"
#define DP_ALERT_PREFIX DP_PREFIX DP_ALERT_TEXT
#define DP_CRIT_PREFIX DP_PREFIX DP_CRIT_TEXT
#define DP_ERR_PREFIX DP_PREFIX DP_ERR_TEXT
#define DP_WARN_PREFIX DP_PREFIX DP_WARN_TEXT
#define DP_NOTICE_PREFIX DP_PREFIX DP_NOTICE_TEXT
#define DP_INFO_PREFIX DP_PREFIX DP_INFO_TEXT
#define DP_DBG_PREFIX DP_PREFIX DP_DBG_TEXT
#define DPRINT_LEV L_ERR
#ifndef MOD_NAME
#define MOD_NAME core
#endif
#ifndef NO_DEBUG
#undef NO_LOG
#endif
/* vars:*/
extern int *log_level;
extern int log_stderr;
extern int log_facility;
extern char* log_name;
extern char ctime_buf[];
/*
* must be called after init_multi_proc_support()
* must be called once for each OpenSIPS process
*/
int init_log_level(void);
/* must be called once, before the "pt" process table is freed */
void cleanup_log_level(void);
int dp_my_pid(void);
void dprint (char* format, ...);
int str2facility(char *s);
void __set_proc_log_level(int proc_idx, int level);
void __set_proc_default_log_level(int proc_idx, int level);
/* set the current and default log levels for all OpenSIPS processes */
void set_global_log_level(int level);
/* set the log level of the current process */
void set_proc_log_level(int level);
/* changes the logging level to the default value for the current process */
void reset_proc_log_level(void);
static inline char* dp_time(void)
{
time_t ltime;
time(<ime);
ctime_r( <ime, ctime_buf);
ctime_buf[19] = 0; /* remove year*/
return ctime_buf+4; /* remove name of day*/
}
#define is_printable(_level) (((int)(*log_level)) >= ((int)(_level)))
#if defined __GNUC__
#define __DP_FUNC __FUNCTION__
#elif defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
#define __DP_FUNC __func__
#else
#define __DP_FUNC ((__const char *) 0)
#endif
#ifdef NO_LOG
#ifdef __SUNPRO_C
#define LM_GEN2(facility, lev, ...)
#define LM_GEN1(lev, ...)
#define LM_ALERT( ...)
#define LM_CRIT( ...)
#define LM_ERR( ...)
#define LM_WARN( ...)
#define LM_NOTICE( ...)
#define LM_INFO( ...)
#define LM_DBG( ...)
#else
#define LM_GEN2(facility, lev, fmt, args...)
#define LM_GEN1(lev, fmt, args...)
#define LM_ALERT(fmt, args...)
#define LM_CRIT(fmt, args...)
#define LM_ERR(fmt, args...)
#define LM_WARN(fmt, args...)
#define LM_NOTICE(fmt, args...)
#define LM_INFO(fmt, args...)
#define LM_DBG(fmt, args...)
#endif
#else /* NO_LOG */
#ifdef __SUNPRO_C
#define LOG_PREFIX_UTIL2(_n) #_n
#define LOG_PREFIX_UTIL(_n) LOG_PREFIX_UTIL2(_n)
#define LOG_PREFIX LOG_PREFIX_UTIL(MOD_NAME) ": "
#define MY_DPRINT( ...) \
dprint( LOG_PREFIX __VA_ARGS__ ) \
#define MY_SYSLOG( _log_level, ...) \
syslog( (_log_level)|log_facility, \
LOG_PREFIX __VA_ARGS__);\
#define LM_GEN1(_lev, ...) \
LM_GEN2( log_facility, _lev, __VA_ARGS__)
#define LM_GEN2( _facility, _lev, ...) \
do { \
if (is_printable(_lev)){ \
if (log_stderr) \
dprint( DP_PREFIX fmt, dp_time(), \
dp_my_pid(), __VA_ARGS__ ); \
else { \
switch(_lev){ \
case L_CRIT: \
syslog(LOG_CRIT|_facility, __VA_ARGS__); \
break; \
case L_ALERT: \
syslog(LOG_ALERT|_facility, __VA_ARGS__); \
break; \
case L_ERR: \
syslog(LOG_ERR|_facility, __VA_ARGS__); \
break; \
case L_WARN: \
syslog(LOG_WARNING|_facility, __VA_ARGS__);\
break; \
case L_NOTICE: \
syslog(LOG_NOTICE|_facility, __VA_ARGS__); \
break; \
case L_INFO: \
syslog(LOG_INFO|_facility, __VA_ARGS__); \
break; \
case L_DBG: \
syslog(LOG_DEBUG|_facility, __VA_ARGS__); \
break; \
default: \
if (_lev > L_DBG) \
syslog(LOG_DEBUG|_facility, __VA_ARGS__); \
break; \
} \
} \
} \
}while(0)
#define LM_ALERT( ...) \
do { \
if (is_printable(L_ALERT)){ \
if (log_stderr)\
MY_DPRINT( DP_ALERT_PREFIX __VA_ARGS__);\
else \
MY_SYSLOG( LOG_ALERT, DP_ALERT_TEXT __VA_ARGS__);\
} \
}while(0)
#define LM_CRIT( ...) \
do { \
if (is_printable(L_CRIT)){ \
if (log_stderr)\
MY_DPRINT( DP_CRIT_PREFIX __VA_ARGS__);\
else \
MY_SYSLOG( LOG_CRIT, DP_CRIT_TEXT __VA_ARGS__);\
} \
}while(0)
#define LM_ERR( ...) \
do { \
if (is_printable(L_ERR)){ \
if (log_stderr)\
MY_DPRINT( DP_ERR_PREFIX __VA_ARGS__);\
else \
MY_SYSLOG( LOG_ERR, DP_ERR_TEXT __VA_ARGS__);\
} \
}while(0)
#define LM_WARN( ...) \
do { \
if (is_printable(L_WARN)){ \
if (log_stderr)\
MY_DPRINT( DP_WARN_PREFIX __VA_ARGS__);\
else \
MY_SYSLOG( LOG_WARNING, DP_WARN_TEXT __VA_ARGS__);\
} \
}while(0)
#define LM_NOTICE( ...) \
do { \
if (is_printable(L_NOTICE)){ \
if (log_stderr)\
MY_DPRINT( DP_NOTICE_PREFIX __VA_ARGS__);\
else \
MY_SYSLOG( LOG_NOTICE, DP_NOTICE_TEXT __VA_ARGS__);\
} \
}while(0)
#define LM_INFO( ...) \
do { \
if (is_printable(L_INFO)){ \
if (log_stderr)\
MY_DPRINT( DP_INFO_PREFIX __VA_ARGS__);\
else \
MY_SYSLOG( LOG_INFO, DP_INFO_TEXT __VA_ARGS__);\
} \
}while(0)
#ifdef NO_DEBUG
#define LM_DBG( ...)
#else
#define LM_DBG( ...) \
do { \
if (is_printable(L_DBG)){ \
if (log_stderr)\
MY_DPRINT( DP_DBG_PREFIX __VA_ARGS__);\
else \
MY_SYSLOG( LOG_DEBUG, DP_DBG_TEXT __VA_ARGS__);\
} \
}while(0)
#endif /*NO_DEBUG*/
#else /*SUN_PRO_C*/
#define LOG_PREFIX_UTIL2(_n) #_n
#define LOG_PREFIX_UTIL(_n) LOG_PREFIX_UTIL2(_n)
#define LOG_PREFIX LOG_PREFIX_UTIL(MOD_NAME) ":%s: "
#define MY_DPRINT( _prefix, _fmt, args...) \
dprint( _prefix LOG_PREFIX _fmt, dp_time(), \
dp_my_pid(), __DP_FUNC, ## args) \
#define MY_SYSLOG( _log_level, _prefix, _fmt, args...) \
syslog( (_log_level)|log_facility, \
_prefix LOG_PREFIX _fmt, __DP_FUNC, ##args);\
#define LM_GEN1(_lev, args...) \
LM_GEN2( log_facility, _lev, ##args)
#define LM_GEN2( _facility, _lev, fmt, args...) \
do { \
if (is_printable(_lev)){ \
if (log_stderr) \
dprint( DP_PREFIX fmt, dp_time(), \
dp_my_pid(), ## args); \
else { \
switch(_lev){ \
case L_CRIT: \
syslog(LOG_CRIT|_facility, fmt, ##args); \
break; \
case L_ALERT: \
syslog(LOG_ALERT|_facility, fmt, ##args); \
break; \
case L_ERR: \
syslog(LOG_ERR|_facility, fmt, ##args); \
break; \
case L_WARN: \
syslog(LOG_WARNING|_facility, fmt, ##args);\
break; \
case L_NOTICE: \
syslog(LOG_NOTICE|_facility, fmt, ##args); \
break; \
case L_INFO: \
syslog(LOG_INFO|_facility, fmt, ##args); \
break; \
case L_DBG: \
syslog(LOG_DEBUG|_facility, fmt, ##args); \
break; \
default: \
if (_lev > L_DBG) \
syslog(LOG_DEBUG|_facility, fmt, ##args); \
break; \
} \
} \
} \
}while(0)
#define LM_ALERT( fmt, args...) \
do { \
if (is_printable(L_ALERT)){ \
if (log_stderr)\
MY_DPRINT( DP_ALERT_PREFIX, fmt, ##args);\
else \
MY_SYSLOG( LOG_ALERT, DP_ALERT_TEXT, fmt, ##args);\
} \
}while(0)
#define LM_CRIT( fmt, args...) \
do { \
if (is_printable(L_CRIT)){ \
if (log_stderr)\
MY_DPRINT( DP_CRIT_PREFIX, fmt, ##args);\
else \
MY_SYSLOG( LOG_CRIT, DP_CRIT_TEXT, fmt, ##args);\
} \
}while(0)
#define LM_ERR( fmt, args...) \
do { \
if (is_printable(L_ERR)){ \
if (log_stderr)\
MY_DPRINT( DP_ERR_PREFIX, fmt, ##args);\
else \
MY_SYSLOG( LOG_ERR, DP_ERR_TEXT, fmt, ##args);\
} \
}while(0)
#define LM_WARN( fmt, args...) \
do { \
if (is_printable(L_WARN)){ \
if (log_stderr)\
MY_DPRINT( DP_WARN_PREFIX, fmt, ##args);\
else \
MY_SYSLOG( LOG_WARNING, DP_WARN_TEXT, fmt, ##args);\
} \
}while(0)
#define LM_NOTICE( fmt, args...) \
do { \
if (is_printable(L_NOTICE)){ \
if (log_stderr)\
MY_DPRINT( DP_NOTICE_PREFIX, fmt, ##args);\
else \
MY_SYSLOG( LOG_NOTICE, DP_NOTICE_TEXT, fmt, ##args);\
} \
}while(0)
#define LM_INFO( fmt, args...) \
do { \
if (is_printable(L_INFO)){ \
if (log_stderr)\
MY_DPRINT( DP_INFO_PREFIX, fmt, ##args);\
else \
MY_SYSLOG( LOG_INFO, DP_INFO_TEXT, fmt, ##args);\
} \
}while(0)
#ifdef NO_DEBUG
#define LM_DBG( fmt, args...)
#else
#define LM_DBG( fmt, args...) \
do { \
if (is_printable(L_DBG)){ \
if (log_stderr)\
MY_DPRINT( DP_DBG_PREFIX, fmt, ##args);\
else \
MY_SYSLOG( LOG_DEBUG, DP_DBG_TEXT, fmt, ##args);\
} \
}while(0)
#endif /*NO_DEBUG*/
#endif /*SUN_PRO_C*/
#endif
#define LM_BUG(format, args...) \
do { \
LM_CRIT("\n>>> " format"\nIt seems you have hit a programming bug.\n" \
"Please help us make OpenSIPS better by reporting it at " \
"https://github.com/OpenSIPS/opensips/issues\n\n", ##args); \
} while (0)
#endif /* ifndef dprint_h */