- cfg_core.c and cfg_core.h are added to the repository as a placeholder
[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
29
30 #ifndef dprint_h
31 #define dprint_h
32
33 #include <syslog.h>
34 #include "cfg_core.h"
35
36
37 #define L_ALERT -3
38 #define L_CRIT  -2
39 #define L_ERR   -1
40 #define L_DEFAULT 0
41 #define L_WARN   1
42 #define L_NOTICE 2
43 #define L_INFO   3
44 #define L_DBG    4
45
46 /* vars:*/
47
48 extern int log_stderr;
49 extern int log_facility;
50 extern volatile int dprint_crit; /* protection against "simultaneous"
51                                                                         printing from signal handlers */
52
53 #ifdef NO_SIG_DEBUG
54 #define DPRINT_NON_CRIT         (1)
55 #define DPRINT_CRIT_ENTER
56 #define DPRINT_CRIT_EXIT
57 #else
58 #define DPRINT_NON_CRIT         (dprint_crit==0)
59 #define DPRINT_CRIT_ENTER       (dprint_crit++)
60 #define DPRINT_CRIT_EXIT        (dprint_crit--)
61 #endif
62
63 #define DPRINT_LEV      1
64 /* priority at which we log */
65 #define DPRINT_PRIO LOG_DEBUG
66
67
68 void dprint (char* format, ...);
69
70 int str2facility(char *s);
71
72 /* C >= 99 has __func__, older gcc versions have __FUNCTION__ */
73 #if __STDC_VERSION__ < 199901L
74 # if __GNUC__ >= 2
75 #  define _FUNC_NAME_ __FUNCTION__
76 # else
77 #  define _FUNC_NAME_ ""
78 # endif
79 #else
80 # define _FUNC_NAME_ __func__
81 #endif
82
83
84 #define XCT2STR(i) #i
85 #define CT2STR(l) XCT2STR(l)
86
87 #define LOC_INFO        __FILE__ ":" CT2STR(__LINE__) ": "
88
89
90
91 #ifdef NO_DEBUG
92         #ifdef __SUNPRO_C
93                 #define DPrint(...)
94         #else
95                 #define DPrint(fmt, args...)
96         #endif
97 #else
98         #ifdef __SUNPRO_C
99                 #define DPrint( ...) \
100                         do{ \
101                                 if ((cfg_get(core, core_cfg, debug)>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
102                                         DPRINT_CRIT_ENTER; \
103                                         if (log_stderr){ \
104                                                 dprint (__VA_ARGS__); \
105                                         }else{ \
106                                                 syslog(DPRINT_LEV|log_facility,  __VA_ARGS__); \
107                                         }\
108                                         DPRINT_CRIT_EXIT; \
109                                 } \
110                         }while(0)
111         #else
112                         #define DPrint(fmt,args...) \
113                         do{ \
114                                 if ((cfg_get(core, core_cfg, debug)>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
115                                         DPRINT_CRIT_ENTER; \
116                                         if (log_stderr){ \
117                                                 dprint (fmt, ## args); \
118                                         }else{ \
119                                                 syslog(DPRINT_LEV|log_facility, fmt, ## args); \
120                                         }\
121                                         DPRINT_CRIT_EXIT; \
122                                 } \
123                         }while(0)
124         #endif
125
126 #endif
127
128 #ifndef NO_DEBUG
129         #undef NO_LOG
130 #endif
131
132 #ifdef NO_LOG
133         #ifdef __SUNPRO_C
134                 #define LOG(lev, ...)
135         #else
136                 #define LOG(lev, fmt, args...)
137         #endif
138 #else
139         #ifdef __SUNPRO_C
140                 #define LOG(lev, ...) \
141                         do { \
142                                 if ((cfg_get(core, core_cfg, debug)>=(lev)) && DPRINT_NON_CRIT){ \
143                                         DPRINT_CRIT_ENTER; \
144                                         if (log_stderr) dprint (__VA_ARGS__); \
145                                         else { \
146                                                 switch(lev){ \
147                                                         case L_CRIT: \
148                                                                 syslog(LOG_CRIT|log_facility, __VA_ARGS__); \
149                                                                 break; \
150                                                         case L_ALERT: \
151                                                                 syslog(LOG_ALERT|log_facility, __VA_ARGS__); \
152                                                                 break; \
153                                                         case L_ERR: \
154                                                                 syslog(LOG_ERR|log_facility, __VA_ARGS__); \
155                                                                 break; \
156                                                         case L_WARN: \
157                                                                 syslog(LOG_WARNING|log_facility, __VA_ARGS__);\
158                                                                 break; \
159                                                         case L_NOTICE: \
160                                                                 syslog(LOG_NOTICE|log_facility, __VA_ARGS__); \
161                                                                 break; \
162                                                         case L_INFO: \
163                                                                 syslog(LOG_INFO|log_facility, __VA_ARGS__); \
164                                                                 break; \
165                                                         case L_DBG: \
166                                                                 syslog(LOG_DEBUG|log_facility, __VA_ARGS__); \
167                                                                 break; \
168                                                 } \
169                                         } \
170                                         DPRINT_CRIT_EXIT; \
171                                 } \
172                         }while(0)
173         #else
174                 #define LOG(lev, fmt, args...) \
175                         do { \
176                                 if ((cfg_get(core, core_cfg, debug)>=(lev)) && DPRINT_NON_CRIT){ \
177                                         DPRINT_CRIT_ENTER; \
178                                         if (log_stderr) dprint (fmt, ## args); \
179                                         else { \
180                                                 switch(lev){ \
181                                                         case L_CRIT: \
182                                                                 syslog(LOG_CRIT|log_facility, fmt, ##args); \
183                                                                 break; \
184                                                         case L_ALERT: \
185                                                                 syslog(LOG_ALERT|log_facility, fmt, ##args); \
186                                                                 break; \
187                                                         case L_ERR: \
188                                                                 syslog(LOG_ERR|log_facility, fmt, ##args); \
189                                                                 break; \
190                                                         case L_WARN: \
191                                                                 syslog(LOG_WARNING|log_facility, fmt, ##args);\
192                                                                 break; \
193                                                         case L_NOTICE: \
194                                                                 syslog(LOG_NOTICE|log_facility, fmt, ##args); \
195                                                                 break; \
196                                                         case L_INFO: \
197                                                                 syslog(LOG_INFO|log_facility, fmt, ##args); \
198                                                                 break; \
199                                                         case L_DBG: \
200                                                                 syslog(LOG_DEBUG|log_facility, fmt, ##args); \
201                                                                 break; \
202                                                 } \
203                                         } \
204                                         DPRINT_CRIT_EXIT; \
205                                 } \
206                         }while(0)
207         #endif /*SUN_PRO_C*/
208 #endif
209
210
211 #ifdef NO_DEBUG
212         #ifdef __SUNPRO_C
213                 #define DBG(...)
214         #else
215                 #define DBG(fmt, args...)
216         #endif
217 #else
218         #ifdef __SUNPRO_C
219                 #define DBG(...) LOG(L_DBG, __VA_ARGS__)
220         #else
221                 #define DBG(fmt, args...) LOG(L_DBG, fmt, ## args)
222         #endif
223 #endif
224
225 #ifdef __SUNPRO_C
226                 #define DEBUG(...) DBG("DEBUG: "          LOC_INFO __VA_ARGS__)
227                 #define ERR(...)  LOG(L_ERR, "ERROR: "    LOC_INFO __VA_ARGS__)
228                 #define WARN(...) LOG(L_WARN, "WARNING: " LOC_INFO __VA_ARGS__)
229                 #define INFO(...) LOG(L_INFO, "INFO: "    LOC_INFO __VA_ARGS__)
230                 #define BUG(...) LOG(L_CRIT, "BUG: "      LOC_INFO __VA_ARGS__)
231 #else
232                 #define DEBUG(fmt, args...) DBG("DEBUG: "       LOC_INFO fmt, ## args)
233                 #define ERR(fmt, args...) LOG(L_ERR, "ERROR: "  LOC_INFO fmt, ## args)
234                 #define WARN(fmt, args...) LOG(L_WARN, "WARN: " LOC_INFO fmt, ## args)
235                 #define INFO(fmt, args...) LOG(L_INFO, "INFO: " LOC_INFO fmt, ## args)
236                 #define BUG(fmt, args...) LOG(L_CRIT, "BUG: "   LOC_INFO fmt, ## args)
237 #endif
238
239
240 #endif /* ifndef dprint_h */