pkg: fix wrong package name, closes FS#148, reported from Andrew Pogrebennyk
[sip-router] / dprint.h
1 /*
2  * Copyright (C) 2001-2003 FhG Fokus
3  *
4  * This file is part of ser, a free SIP server.
5  *
6  * ser 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  * ser 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
21 /**
22  * @file
23  * @brief SIP-router 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 && defined __FUNCTION__
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 #       else
63 #               define LOC_INFO         "<core>: "
64 #       endif
65 #else
66 #       define XCT2STR(i) #i
67 #       define CT2STR(l)  XCT2STR(l)
68 #
69 #       ifdef MOD_NAME
70 #               define LOC_INFO         MOD_NAME " [" __FILE__ ":" CT2STR(__LINE__) "]: "
71 #       else
72 #               define LOC_INFO         "<core> [" __FILE__ ":" CT2STR(__LINE__) "]: "
73 #       endif
74 #
75 #       ifdef NO_LOG
76 #               undef NO_LOG
77 #       endif
78 #endif /* NO_DEBUG */
79
80
81 /*
82  * Log levels
83  */
84 #define L_ALERT         -5
85 #define L_BUG           -4
86 #define L_CRIT2         -3  /* like L_CRIT, but adds prefix */
87 #define L_CRIT          -2  /* no prefix added */
88 #define L_ERR           -1
89 #define L_WARN          0
90 #define L_NOTICE        1
91 #define L_INFO          2
92 #define L_DBG           3
93
94 /** @brief This is the facility value used to indicate that the caller of the macro
95  * did not override the facility. Value 0 (the defaul) is LOG_KERN on Linux
96  */
97 #define DEFAULT_FACILITY 0
98
99 #define LOG_LEVEL2NAME(level)   (log_level_info[(level) - (L_ALERT)].name)
100 #define LOG2SYSLOG_LEVEL(level) \
101         (log_level_info[(level) - (L_ALERT)].syslog_level)
102
103
104 /** @brief my_pid(), process_no are from pt.h but we cannot \#include it here
105    because of circular dependencies */
106 extern int process_no;
107 extern int my_pid();
108
109 /** @brief non-zero if logging to stderr instead to the syslog */
110 extern int log_stderr;
111
112 /** @brief maps log levels to their string name and corresponding syslog level */
113
114 struct log_level_info {
115         char *name;
116         int syslog_level;
117 };
118
119 /** @brief per process debug level handling */
120 int get_debug_level(void);
121 void set_local_debug_level(int level);
122 void reset_local_debug_level(void);
123
124 #define is_printable(level) (get_debug_level()>=(level))
125 extern struct log_level_info log_level_info[];
126 extern char *log_name;
127
128 #ifndef NO_SIG_DEBUG
129 /** @brief protection against "simultaneous" printing from signal handlers */
130 extern volatile int dprint_crit; 
131 #endif
132
133 int str2facility(char *s);
134 int log_facility_fixup(void *handle, str *gname, str *name, void **val);
135
136
137 /** @brief
138  * General logging macros
139  *
140  * LOG_(level, prefix, fmt, ...) prints "printf"-formatted log message to
141  * stderr (if `log_stderr' is non-zero) or to syslog.  Note that `fmt' must
142  * be constant. `prefix' is added to the beginning of the message.
143  *
144  * LOG(level, fmt, ...) is same as LOG_() with LOC_INFO prefix.
145  */
146 #ifdef NO_LOG
147
148 #       ifdef __SUNPRO_C
149 #               define LOG_(facility, level, prefix, fmt, ...)
150 #               define LOG(level, fmt, ...)
151 #               define LOG_FC(facility, level, fmt, ...)
152 #       else
153 #               define LOG_(facility, level, prefix, fmt, args...)
154 #               define LOG(level, fmt, args...)
155 #               define LOG_FC(facility, level, fmt, args...)
156 #       endif
157
158 #else
159
160 #       ifdef NO_SIG_DEBUG
161 #               define DPRINT_NON_CRIT          (1)
162 #               define DPRINT_CRIT_ENTER
163 #               define DPRINT_CRIT_EXIT
164 #       else
165 #               define DPRINT_NON_CRIT          (dprint_crit==0)
166 #               define DPRINT_CRIT_ENTER        (dprint_crit++)
167 #               define DPRINT_CRIT_EXIT         (dprint_crit--)
168 #       endif
169
170 #       ifdef __SUNPRO_C
171 #               define LOG_(facility, level, prefix, fmt, ...) \
172                         do { \
173                                 if (unlikely(get_debuglevel() >= (level) && \
174                                                 DPRINT_NON_CRIT)) { \
175                                         DPRINT_CRIT_ENTER; \
176                                         if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
177                                                 if (unlikely(log_stderr)) { \
178                                                         fprintf(stderr, "%2d(%d) %s: %s" fmt, \
179                                                                         process_no, my_pid(), \
180                                                                         LOG_LEVEL2NAME(level), (prefix), \
181                                                                         __VA_ARGS__); \
182                                                 } else { \
183                                                         syslog(LOG2SYSLOG_LEVEL(level) | \
184                                                                    (((facility) != DEFAULT_FACILITY) ? \
185                                                                         (facility) : \
186                                                                         cfg_get(core, core_cfg, log_facility)), \
187                                                                         "%s: %s" fmt, LOG_LEVEL2NAME(level),\
188                                                                         (prefix), __VA_ARGS__); \
189                                                 } \
190                                         } else { \
191                                                 if (log_stderr) { \
192                                                         fprintf(stderr, "%2d(%d) %s" fmt, \
193                                                                         process_no, my_pid(), \
194                                                                         (prefix),  __VA_ARGS__); \
195                                                 } else { \
196                                                         if ((level)<L_ALERT) \
197                                                                 syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
198                                                                            (((facility) != DEFAULT_FACILITY) ? \
199                                                                                 (facility) : \
200                                                                                 cfg_get(core, core_cfg, log_facility)),\
201                                                                            "%s" fmt, (prefix), __VA_ARGS__); \
202                                                         else \
203                                                                 syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
204                                                                            (((facility) != DEFAULT_FACILITY) ? \
205                                                                                 (facility) : \
206                                                                                 cfg_get(core, core_cfg, log_facility)),\
207                                                                            "%s" fmt, (prefix), __VA_ARGS__); \
208                                                 } \
209                                         } \
210                                         DPRINT_CRIT_EXIT; \
211                                 } \
212                         } while(0)
213                         
214 #               ifdef LOG_FUNC_NAME
215 #                       define LOG(level, fmt, ...) \
216         LOG_(DEFAULT_FACILITY, (level), LOC_INFO, "%s(): " fmt,\
217                                 _FUNC_NAME_, __VA_ARGS__)
218
219 #                       define LOG_FC(facility, level, fmt, ...) \
220         LOG_((facility), (level), LOC_INFO, "%s(): " fmt,\
221                                 _FUNC_NAME_, __VA_ARGS__)
222 #               else /* LOG_FUNC_NAME */
223
224 #                       define LOG(level, fmt, ...) \
225         LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt, __VA_ARGS__)
226
227 #                       define LOG_FC(facility, level, fmt, ...) \
228         LOG_((facility), (level), LOC_INFO, fmt, __VA_ARGS__)
229
230 #               endif /* LOG_FUNC_NAME */
231
232 #       else /* ! __SUNPRO_C */
233 #               define LOG_(facility, level, prefix, fmt, args...) \
234                         do { \
235                                 if (get_debug_level() >= (level) && \
236                                                 DPRINT_NON_CRIT) { \
237                                         DPRINT_CRIT_ENTER; \
238                                         if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
239                                                 if (unlikely(log_stderr)) { \
240                                                         fprintf(stderr, "%2d(%d) %s: %s" fmt, \
241                                                                         process_no, my_pid(), \
242                                                                         LOG_LEVEL2NAME(level), \
243                                                                         (prefix) , ## args);\
244                                                 } else { \
245                                                         syslog(LOG2SYSLOG_LEVEL(level) |\
246                                                                    (((facility) != DEFAULT_FACILITY) ? \
247                                                                         (facility) : \
248                                                                         cfg_get(core, core_cfg, log_facility)), \
249                                                                         "%s: %s" fmt, LOG_LEVEL2NAME(level),\
250                                                                         (prefix) , ## args); \
251                                                 } \
252                                         } else { \
253                                                 if (log_stderr) { \
254                                                         fprintf(stderr, "%2d(%d) %s" fmt, \
255                                                                                 process_no, my_pid(), \
256                                                                                 (prefix) , ## args); \
257                                                 } else { \
258                                                         if ((level)<L_ALERT) \
259                                                                 syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
260                                                                            (((facility) != DEFAULT_FACILITY) ? \
261                                                                                 (facility) : \
262                                                                                 cfg_get(core, core_cfg, log_facility)),\
263                                                                                 "%s" fmt, (prefix) , ## args); \
264                                                         else \
265                                                                 syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
266                                                                            (((facility) != DEFAULT_FACILITY) ? \
267                                                                                 (facility) : \
268                                                                                 cfg_get(core, core_cfg, log_facility)),\
269                                                                                 "%s" fmt, (prefix) , ## args); \
270                                                 } \
271                                         } \
272                                         DPRINT_CRIT_EXIT; \
273                                 } \
274                         } while(0)
275                         
276 #               ifdef LOG_FUNC_NAME
277 #                       define LOG(level, fmt, args...) \
278         LOG_(DEFAULT_FACILITY, (level), LOC_INFO, "%s(): " fmt ,\
279                         _FUNC_NAME_, ## args)
280
281 #                       define LOG_FC(facility, level, fmt, args...) \
282         LOG_((facility), (level), LOC_INFO, "%s(): " fmt , _FUNC_NAME_, ## args)
283
284 #               else /* LOG_FUNC_NAME */
285 #                       define LOG(level, fmt, args...) \
286         LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt , ## args)
287 #                       define LOG_FC(facility, level, fmt, args...) \
288         LOG_((facility), (level), LOC_INFO, fmt , ## args)
289
290 #               endif /* LOG_FUNC_NAME */
291 #       endif /* __SUNPRO_C */
292 #endif /* NO_LOG */
293
294
295 /** @name SimpleLog
296  * Simplier, prefered logging macros for constant log level
297  */
298 /*@ { */
299 #ifdef __SUNPRO_C
300 #       define ALERT(...)  LOG(L_ALERT,  __VA_ARGS__)
301 #       define BUG(...)    LOG(L_BUG,   __VA_ARGS__)
302 #       define ERR(...)    LOG(L_ERR,    __VA_ARGS__)
303 #       define WARN(...)   LOG(L_WARN,   __VA_ARGS__)
304 #       define NOTICE(...) LOG(L_NOTICE, __VA_ARGS__)
305 #       define INFO(...)   LOG(L_INFO,   __VA_ARGS__)
306 #       define CRIT(...)    LOG(L_CRIT2,   __VA_ARGS__)
307
308 #       ifdef NO_DEBUG
309 #               define DBG(...)
310 #       else
311 #               define DBG(...)    LOG(L_DBG, __VA_ARGS__)
312 #       endif           
313 /*@ } */
314
315 /* obsolete, do not use */
316 #       define DEBUG(...) DBG(__VA_ARGS__)
317
318 #else /* ! __SUNPRO_C */
319 #       define ALERT(fmt, args...)  LOG(L_ALERT,  fmt , ## args)
320 #       define BUG(fmt, args...)    LOG(L_BUG,   fmt , ## args)
321 #       define ERR(fmt, args...)    LOG(L_ERR,    fmt , ## args)
322 #       define WARN(fmt, args...)   LOG(L_WARN,   fmt , ## args)
323 #       define NOTICE(fmt, args...) LOG(L_NOTICE, fmt , ## args)
324 #       define INFO(fmt, args...)   LOG(L_INFO,   fmt , ## args)
325 #       define CRIT(fmt, args...)   LOG(L_CRIT2,   fmt , ## args)
326
327 #       ifdef NO_DEBUG
328 #               define DBG(fmt, args...)
329 #       else
330 #               define DBG(fmt, args...)    LOG(L_DBG, fmt , ## args)
331 #       endif           
332
333 /* obsolete, do not use */
334 #       define DEBUG(fmt, args...) DBG(fmt , ## args)
335                 
336 #endif /* __SUNPRO_C */
337
338
339 /* kamailio/openser compatibility */
340
341 #define LM_GEN1 LOG
342 #define LM_GEN2 LOG_FC
343 #define LM_ALERT ALERT
344 #define LM_CRIT  CRIT
345 #define LM_ERR ERR
346 #define LM_WARN WARN
347 #define LM_NOTICE NOTICE
348 #define LM_INFO INFO
349 #define LM_DBG DEBUG
350
351 #endif /* !dprint_h */