Merge commit 'origin/ser_core_cvs'
[sip-router] / dprint.h
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License 
24  * along with this program; if not, write to the Free Software 
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 /* C >= 99 has __func__, older gcc versions have __FUNCTION__ */
40 #if __STDC_VERSION__ < 199901L
41 #       if __GNUC__ >= 2
42 #               define _FUNC_NAME_ __FUNCTION__
43 #       else
44 #               define _FUNC_NAME_ ""
45 #       endif
46 #else
47 #       define _FUNC_NAME_ __func__
48 #endif
49
50 #ifdef NO_DEBUG
51 #       ifdef MOD_NAME
52 #               define LOC_INFO         MOD_NAME ": "
53 #       else
54 #               define LOC_INFO         "<core>: "
55 #       endif
56 #else
57 #       define XCT2STR(i) #i
58 #       define CT2STR(l)  XCT2STR(l)
59 #
60 #       ifdef MOD_NAME
61 #               define LOC_INFO         MOD_NAME " [" __FILE__ ":" CT2STR(__LINE__) "]: "
62 #       else
63 #               define LOC_INFO         "<core> [" __FILE__ ":" CT2STR(__LINE__) "]: "
64 #       endif
65 #
66 #       ifdef NO_LOG
67 #               undef NO_LOG
68 #       endif
69 #endif /* NO_DEBUG */
70
71
72 /*
73  * Log levels
74  */
75 #define L_ALERT         -5
76 #define L_BUG           -4
77 #define L_CRIT2         -3  /* like L_CRIT, but adds prefix */
78 #define L_CRIT          -2  /* no prefix added */
79 #define L_ERR           -1
80 #define L_WARN          0
81 #define L_NOTICE        1
82 #define L_INFO          2
83 #define L_DBG           3
84
85 /* This is the facility value used to indicate that the caller of the macro
86  * did not override the facility. Value 0 (the defaul) is LOG_KERN on Linux
87  */
88 #define DEFAULT_FACILITY 0
89
90 #define LOG_LEVEL2NAME(level)   (log_level_info[(level) - (L_ALERT)].name)
91 #define LOG2SYSLOG_LEVEL(level) \
92         (log_level_info[(level) - (L_ALERT)].syslog_level)
93
94
95 /* my_pid(), process_no are from pt.h but we cannot #include it here
96    because of circular dependencies */
97 extern int process_no;
98 extern int my_pid();
99
100 /* non-zero if logging to stderr instead to the syslog */
101 extern int log_stderr;
102
103 /* maps log levels to their string name and corresponding syslog level */
104
105 struct log_level_info {
106         char *name;
107         int syslog_level;
108 };
109
110 #define is_printable(level) (cfg_get(core, core_cfg, debug)>=(level))
111 extern struct log_level_info log_level_info[];
112
113 #ifndef NO_SIG_DEBUG
114 /* protection against "simultaneous" printing from signal handlers */
115 extern volatile int dprint_crit; 
116 #endif
117
118 int str2facility(char *s);
119 int log_facility_fixup(void *handle, str *gname, str *name, void **val);
120
121
122 /*
123  * General logging macros
124  *
125  * LOG_(level, prefix, fmt, ...) prints "printf"-formatted log message to
126  * stderr (if `log_stderr' is non-zero) or to syslog.  Note that `fmt' must
127  * be constant. `prefix' is added to the beginning of the message.
128  *
129  * LOG(level, fmt, ...) is same as LOG_() with LOC_INFO prefix.
130  */
131 #ifdef NO_LOG
132
133 #       ifdef __SUNPRO_C
134 #               define LOG_(facility, level, prefix, fmt, ...)
135 #               define LOG(level, fmt, ...)
136 #               define LOG_FC(facility, level, fmt, ...)
137 #       else
138 #               define LOG_(facility, level, prefix, fmt, args...)
139 #               define LOG(level, fmt, args...)
140 #               define LOG_FC(facility, level, fmt, args...)
141 #       endif
142
143 #else
144
145 #       ifdef NO_SIG_DEBUG
146 #               define DPRINT_NON_CRIT          (1)
147 #               define DPRINT_CRIT_ENTER
148 #               define DPRINT_CRIT_EXIT
149 #       else
150 #               define DPRINT_NON_CRIT          (dprint_crit==0)
151 #               define DPRINT_CRIT_ENTER        (dprint_crit++)
152 #               define DPRINT_CRIT_EXIT         (dprint_crit--)
153 #       endif
154
155 #       ifdef __SUNPRO_C
156 #               define LOG_(facility, level, prefix, fmt, ...) \
157                         do { \
158                                 if (unlikely(cfg_get(core, core_cfg, debug) >= (level) && \
159                                                 DPRINT_NON_CRIT)) { \
160                                         DPRINT_CRIT_ENTER; \
161                                         if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
162                                                 if (unlikely(log_stderr)) { \
163                                                         fprintf(stderr, "%2d(%d) %s: %s" fmt, \
164                                                                         process_no, my_pid(), \
165                                                                         LOG_LEVEL2NAME(level), (prefix), \
166                                                                         __VA_ARGS__); \
167                                                 } else { \
168                                                         syslog(LOG2SYSLOG_LEVEL(level) | \
169                                                                    (((facility) != DEFAULT_FACILITY) ? \
170                                                                         (facility) : \
171                                                                         cfg_get(core, core_cfg, log_facility)), \
172                                                                         "%s: %s" fmt, LOG_LEVEL2NAME(level),\
173                                                                         (prefix), __VA_ARGS__); \
174                                                 } \
175                                         } else { \
176                                                 if (log_stderr) { \
177                                                         fprintf(stderr, "%2d(%d) %s" fmt, \
178                                                                         process_no, my_pid(), \
179                                                                         (prefix),  __VA_ARGS__); \
180                                                 } else { \
181                                                         if ((level)<L_ALERT) \
182                                                                 syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
183                                                                            (((facility) != DEFAULT_FACILITY) ? \
184                                                                                 (facility) : \
185                                                                                 cfg_get(core, core_cfg, log_facility)),\
186                                                                            "%s" fmt, (prefix), __VA_ARGS__); \
187                                                         else \
188                                                                 syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
189                                                                            (((facility) != DEFAULT_FACILITY) ? \
190                                                                                 (facility) : \
191                                                                                 cfg_get(core, core_cfg, log_facility)),\
192                                                                            "%s" fmt, (prefix), __VA_ARGS__); \
193                                                 } \
194                                         } \
195                                         DPRINT_CRIT_EXIT; \
196                                 } \
197                         } while(0)
198                         
199 #               define LOG(level, fmt, ...) \
200         LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt, __VA_ARGS__)
201 #               define LOG_FC(facility, level, fmt, ...) \
202         LOG_((facility), (level), LOC_INFO, fmt, __VA_ARGS__)
203
204 #       else /* ! __SUNPRO_C */
205 #               define LOG_(facility, level, prefix, fmt, args...) \
206                         do { \
207                                 if (cfg_get(core, core_cfg, debug) >= (level) && \
208                                                 DPRINT_NON_CRIT) { \
209                                         DPRINT_CRIT_ENTER; \
210                                         if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
211                                                 if (unlikely(log_stderr)) { \
212                                                         fprintf(stderr, "%2d(%d) %s: %s" fmt, \
213                                                                         process_no, my_pid(), \
214                                                                         LOG_LEVEL2NAME(level),(prefix), ## args);\
215                                                 } else { \
216                                                         syslog(LOG2SYSLOG_LEVEL(level) |\
217                                                                    (((facility) != DEFAULT_FACILITY) ? \
218                                                                         (facility) : \
219                                                                         cfg_get(core, core_cfg, log_facility)), \
220                                                                         "%s: %s" fmt, LOG_LEVEL2NAME(level),\
221                                                                         (prefix), ## args); \
222                                                 } \
223                                         } else { \
224                                                 if (log_stderr) { \
225                                                         fprintf(stderr, "%2d(%d) %s" fmt, \
226                                                                                 process_no, my_pid(), \
227                                                                                 (prefix), ## args); \
228                                                 } else { \
229                                                         if ((level)<L_ALERT) \
230                                                                 syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
231                                                                            (((facility) != DEFAULT_FACILITY) ? \
232                                                                                 (facility) : \
233                                                                                 cfg_get(core, core_cfg, log_facility)),\
234                                                                                 "%s" fmt, (prefix), ## args); \
235                                                         else \
236                                                                 syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
237                                                                            (((facility) != DEFAULT_FACILITY) ? \
238                                                                                 (facility) : \
239                                                                                 cfg_get(core, core_cfg, log_facility)),\
240                                                                                 "%s" fmt, (prefix), ## args); \
241                                                 } \
242                                         } \
243                                         DPRINT_CRIT_EXIT; \
244                                 } \
245                         } while(0)
246                         
247 #               define LOG(level, fmt, args...) \
248         LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt, ## args)
249 #               define LOG_FC(facility, level, fmt, args...) \
250         LOG_((facility), (level), LOC_INFO, fmt, ## args)
251                 
252 #       endif /* __SUNPRO_C */
253 #endif /* NO_LOG */
254
255
256 /*
257  * Simplier, prefered logging macros for constant log level
258  */
259 #ifdef __SUNPRO_C
260 #       define ALERT(...)  LOG(L_ALERT,  __VA_ARGS__)
261 #       define BUG(...)    LOG(L_BUG,   __VA_ARGS__)
262 #       define ERR(...)    LOG(L_ERR,    __VA_ARGS__)
263 #       define WARN(...)   LOG(L_WARN,   __VA_ARGS__)
264 #       define NOTICE(...) LOG(L_NOTICE, __VA_ARGS__)
265 #       define INFO(...)   LOG(L_INFO,   __VA_ARGS__)
266 #       define CRIT(...)    LOG(L_CRIT2,   __VA_ARGS__)
267
268 #       ifdef NO_DEBUG
269 #               define DBG(...)
270 #       else
271 #               define DBG(...)    LOG(L_DBG, __VA_ARGS__)
272 #       endif           
273
274 /* obsolete, do not use */
275 #       define DEBUG(...) DBG(__VA_ARGS__)
276
277 #else /* ! __SUNPRO_C */
278 #       define ALERT(fmt, args...)  LOG(L_ALERT,  fmt, ## args)
279 #       define BUG(fmt, args...)    LOG(L_BUG,   fmt, ## args)
280 #       define ERR(fmt, args...)    LOG(L_ERR,    fmt, ## args)
281 #       define WARN(fmt, args...)   LOG(L_WARN,   fmt, ## args)
282 #       define NOTICE(fmt, args...) LOG(L_NOTICE, fmt, ## args)
283 #       define INFO(fmt, args...)   LOG(L_INFO,   fmt, ## args)
284 #       define CRIT(fmt, args...)   LOG(L_CRIT2,   fmt, ## args)
285
286 #       ifdef NO_DEBUG
287 #               define DBG(fmt, args...)
288 #       else
289 #               define DBG(fmt, args...)    LOG(L_DBG, fmt, ## args)
290 #       endif           
291
292 /* obsolete, do not use */
293 #       define DEBUG(fmt, args...) DBG(fmt, ## args)
294                 
295 #endif /* __SUNPRO_C */
296
297
298 /* kamailio/openser compatibility */
299
300 #define LM_GEN1 LOG
301 #define LM_GEN2 LOG_FC
302 #define LM_ALERT ALERT
303 #define LM_CRIT  CRIT
304 #define LM_ERR ERR
305 #define LM_WARN WARN
306 #define LM_NOTICE NOTICE
307 #define LM_INFO INFO
308 #define LM_DBG DEBUG
309
310 #endif /* !dprint_h */