dprint.h
512dcd98
 /*
  * $Id$
7dd0b342
  *
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
  * This file is part of ser, a free SIP server.
  *
  * ser 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
  *
  * For a license to use the ser software under conditions
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
  * ser 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
512dcd98
  */
 
 
7dd0b342
 
512dcd98
 #ifndef dprint_h
 #define dprint_h
 
efeaaf53
 #include <syslog.h>
7ac069af
 #include "cfg_core.h"
efeaaf53
 
 
 #define L_ALERT -3
 #define L_CRIT  -2
 #define L_ERR   -1
58d0d1b5
 #define L_DEFAULT 0
efeaaf53
 #define L_WARN   1
 #define L_NOTICE 2
 #define L_INFO   3
 #define L_DBG    4
 
4e2fdd79
 /* vars:*/
 
 extern int log_stderr;
2d90691c
 extern volatile int dprint_crit; /* protection against "simultaneous"
 									printing from signal handlers */
efeaaf53
 
96d09107
 #ifdef NO_SIG_DEBUG
 #define DPRINT_NON_CRIT		(1)
 #define DPRINT_CRIT_ENTER
 #define DPRINT_CRIT_EXIT
 #else
2d90691c
 #define DPRINT_NON_CRIT		(dprint_crit==0)
 #define DPRINT_CRIT_ENTER	(dprint_crit++)
 #define DPRINT_CRIT_EXIT	(dprint_crit--)
96d09107
 #endif
efeaaf53
 
 #define DPRINT_LEV	1
 /* priority at which we log */
 #define DPRINT_PRIO LOG_DEBUG
512dcd98
 
 
 void dprint (char* format, ...);
 
26456ace
 int str2facility(char *s);
0d09b88d
 int log_facility_fixup(void *handle, str *name, void **val);
26456ace
 
a86d53b0
 /* C >= 99 has __func__, older gcc versions have __FUNCTION__ */
 #if __STDC_VERSION__ < 199901L
 # if __GNUC__ >= 2
 #  define _FUNC_NAME_ __FUNCTION__
 # else
 #  define _FUNC_NAME_ ""
 # endif
 #else
 # define _FUNC_NAME_ __func__
 #endif
 
 
 #define XCT2STR(i) #i
 #define CT2STR(l) XCT2STR(l)
 
 #define LOC_INFO	__FILE__ ":" CT2STR(__LINE__) ": "
 
 
26456ace
 
512dcd98
 #ifdef NO_DEBUG
51eadd0c
 	#ifdef __SUNPRO_C
 		#define DPrint(...)
 	#else
 		#define DPrint(fmt, args...)
 	#endif
512dcd98
 #else
51eadd0c
 	#ifdef __SUNPRO_C
 		#define DPrint( ...) \
 			do{ \
7ac069af
 				if ((cfg_get(core, core_cfg, debug)>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
2d90691c
 					DPRINT_CRIT_ENTER; \
51eadd0c
 					if (log_stderr){ \
 						dprint (__VA_ARGS__); \
 					}else{ \
0d09b88d
 						syslog(DPRINT_LEV|cfg_get(core, core_cfg, log_facility), \
 							__VA_ARGS__); \
51eadd0c
 					}\
2d90691c
 					DPRINT_CRIT_EXIT; \
51eadd0c
 				} \
 			}while(0)
 	#else
 			#define DPrint(fmt,args...) \
 			do{ \
7ac069af
 				if ((cfg_get(core, core_cfg, debug)>=DPRINT_LEV) && DPRINT_NON_CRIT){ \
2d90691c
 					DPRINT_CRIT_ENTER; \
51eadd0c
 					if (log_stderr){ \
 						dprint (fmt, ## args); \
 					}else{ \
0d09b88d
 						syslog(DPRINT_LEV|cfg_get(core, core_cfg, log_facility), \
 							fmt, ## args); \
51eadd0c
 					}\
2d90691c
 					DPRINT_CRIT_EXIT; \
51eadd0c
 				} \
 			}while(0)
 	#endif
efeaaf53
 
512dcd98
 #endif
 
1fb7b1aa
 #ifndef NO_DEBUG
 	#undef NO_LOG
 #endif
512dcd98
 
1fb7b1aa
 #ifdef NO_LOG
51eadd0c
 	#ifdef __SUNPRO_C
 		#define LOG(lev, ...)
 	#else
 		#define LOG(lev, fmt, args...)
 	#endif
1fb7b1aa
 #else
51eadd0c
 	#ifdef __SUNPRO_C
 		#define LOG(lev, ...) \
 			do { \
7ac069af
 				if ((cfg_get(core, core_cfg, debug)>=(lev)) && DPRINT_NON_CRIT){ \
2d90691c
 					DPRINT_CRIT_ENTER; \
51eadd0c
 					if (log_stderr) dprint (__VA_ARGS__); \
db501a6a
 					else { \
51eadd0c
 						switch(lev){ \
 							case L_CRIT: \
0d09b88d
 								syslog(LOG_CRIT|cfg_get(core, core_cfg, log_facility), \
 									__VA_ARGS__); \
51eadd0c
 								break; \
 							case L_ALERT: \
0d09b88d
 								syslog(LOG_ALERT|cfg_get(core, core_cfg, log_facility), \
 									__VA_ARGS__); \
51eadd0c
 								break; \
 							case L_ERR: \
0d09b88d
 								syslog(LOG_ERR|cfg_get(core, core_cfg, log_facility), \
 									__VA_ARGS__); \
51eadd0c
 								break; \
 							case L_WARN: \
0d09b88d
 								syslog(LOG_WARNING|cfg_get(core, core_cfg, log_facility), \
 									__VA_ARGS__);\
51eadd0c
 								break; \
 							case L_NOTICE: \
0d09b88d
 								syslog(LOG_NOTICE|cfg_get(core, core_cfg, log_facility), \
 									__VA_ARGS__); \
51eadd0c
 								break; \
 							case L_INFO: \
0d09b88d
 								syslog(LOG_INFO|cfg_get(core, core_cfg, log_facility), \
 									__VA_ARGS__); \
51eadd0c
 								break; \
 							case L_DBG: \
0d09b88d
 								syslog(LOG_DEBUG|cfg_get(core, core_cfg, log_facility), \
 									__VA_ARGS__); \
51eadd0c
 								break; \
 						} \
 					} \
2d90691c
 					DPRINT_CRIT_EXIT; \
51eadd0c
 				} \
 			}while(0)
 	#else
 		#define LOG(lev, fmt, args...) \
efeaaf53
 			do { \
7ac069af
 				if ((cfg_get(core, core_cfg, debug)>=(lev)) && DPRINT_NON_CRIT){ \
2d90691c
 					DPRINT_CRIT_ENTER; \
efeaaf53
 					if (log_stderr) dprint (fmt, ## args); \
db501a6a
 					else { \
efeaaf53
 						switch(lev){ \
 							case L_CRIT: \
0d09b88d
 								syslog(LOG_CRIT|cfg_get(core, core_cfg, log_facility), \
 									fmt, ##args); \
efeaaf53
 								break; \
 							case L_ALERT: \
0d09b88d
 								syslog(LOG_ALERT|cfg_get(core, core_cfg, log_facility), \
 									fmt, ##args); \
efeaaf53
 								break; \
 							case L_ERR: \
0d09b88d
 								syslog(LOG_ERR|cfg_get(core, core_cfg, log_facility), \
 									fmt, ##args); \
efeaaf53
 								break; \
 							case L_WARN: \
0d09b88d
 								syslog(LOG_WARNING|cfg_get(core, core_cfg, log_facility), \
 									fmt, ##args);\
efeaaf53
 								break; \
 							case L_NOTICE: \
0d09b88d
 								syslog(LOG_NOTICE|cfg_get(core, core_cfg, log_facility), \
 									fmt, ##args); \
efeaaf53
 								break; \
 							case L_INFO: \
0d09b88d
 								syslog(LOG_INFO|cfg_get(core, core_cfg, log_facility), \
 									fmt, ##args); \
efeaaf53
 								break; \
 							case L_DBG: \
0d09b88d
 								syslog(LOG_DEBUG|cfg_get(core, core_cfg, log_facility), \
 									fmt, ##args); \
efeaaf53
 								break; \
 						} \
 					} \
2d90691c
 					DPRINT_CRIT_EXIT; \
efeaaf53
 				} \
 			}while(0)
51eadd0c
 	#endif /*SUN_PRO_C*/
1fb7b1aa
 #endif
 
efeaaf53
 
1fb7b1aa
 #ifdef NO_DEBUG
51eadd0c
 	#ifdef __SUNPRO_C
 		#define DBG(...)
 	#else
 		#define DBG(fmt, args...)
 	#endif
1fb7b1aa
 #else
51eadd0c
 	#ifdef __SUNPRO_C
 		#define DBG(...) LOG(L_DBG, __VA_ARGS__)
 	#else
 		#define DBG(fmt, args...) LOG(L_DBG, fmt, ## args)
 	#endif
1fb7b1aa
 #endif
efeaaf53
 
a86d53b0
 #ifdef __SUNPRO_C
0bec02f3
 		#define DEBUG(...) DBG("DEBUG: "          LOC_INFO __VA_ARGS__)
a86d53b0
 		#define ERR(...)  LOG(L_ERR, "ERROR: "    LOC_INFO __VA_ARGS__)
 		#define WARN(...) LOG(L_WARN, "WARNING: " LOC_INFO __VA_ARGS__)
 		#define INFO(...) LOG(L_INFO, "INFO: "    LOC_INFO __VA_ARGS__)
 		#define BUG(...) LOG(L_CRIT, "BUG: "      LOC_INFO __VA_ARGS__)
4f69388d
 		#define NOTICE(...) LOG(L_NOTICE, "NOTICE: " LOC_INFO __VA_ARGS__)
 		#define ALERT(...) LOG(L_ALERT, "ALERT: " LOC_INFO __VA_ARGS__)
 		#define CRIT(...) LOG(L_CRIT, "CRITICAL: " LOC_INFO __VA_ARGS__)
a86d53b0
 #else
0bec02f3
 		#define DEBUG(fmt, args...) DBG("DEBUG: "       LOC_INFO fmt, ## args)
a86d53b0
 		#define ERR(fmt, args...) LOG(L_ERR, "ERROR: "  LOC_INFO fmt, ## args)
 		#define WARN(fmt, args...) LOG(L_WARN, "WARN: " LOC_INFO fmt, ## args)
 		#define INFO(fmt, args...) LOG(L_INFO, "INFO: " LOC_INFO fmt, ## args)
 		#define BUG(fmt, args...) LOG(L_CRIT, "BUG: "   LOC_INFO fmt, ## args)
4f69388d
 		#define NOTICE(fmt, args...) \
 			LOG(L_NOTICE, "NOTICE: " LOC_INFO fmt, ## args)
 		#define ALERT(fmt, args...) \
 			LOG(L_ALERT, "ALERT: " LOC_INFO fmt, ## args)
 		#define CRIT(fmt, args...) \
 			LOG(L_CRIT, "CRITICAL: " LOC_INFO fmt, ## args)
a86d53b0
 #endif
 
4f69388d
 /* kamailio/openser compatibility */
 
 #define LM_GEN1 LOG
 
 #define LM_ALERT ALERT
 #define LM_CRIT  CRIT
 #define LM_ERR ERR
 #define LM_WARN WARN
 #define LM_NOTICE
 #define LM_INFO INFO
 #define LM_DBG DEBUG
 
a86d53b0
 
512dcd98
 #endif /* ifndef dprint_h */