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