Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

dprint.h

Go to the documentation of this file.
00001 /*
00002  * $Id: dprint.h,v 1.24 2009/05/07 11:50:41 andrei Exp $
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of ser, a free SIP server.
00007  *
00008  * ser is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * For a license to use the ser software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * ser is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License 
00024  * along with this program; if not, write to the Free Software 
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00027 
00028 #ifndef dprint_h
00029 #define dprint_h
00030 
00031 #include <assert.h>
00032 #include <syslog.h>
00033 #include <stdio.h> /* stderr, fprintf() */
00034 
00035 #include "compiler_opt.h"
00036 #include "cfg_core.h"
00037 
00038 
00039 /* C >= 99 has __func__, older gcc versions have __FUNCTION__ */
00040 #if __STDC_VERSION__ < 199901L
00041 #       if __GNUC__ >= 2
00042 #               define _FUNC_NAME_ __FUNCTION__
00043 #       else
00044 #               define _FUNC_NAME_ ""
00045 #       endif
00046 #else
00047 #       define _FUNC_NAME_ __func__
00048 #endif
00049 
00050 #ifdef NO_DEBUG
00051 #       ifdef MOD_NAME
00052 #               define LOC_INFO         MOD_NAME ": "
00053 #       else
00054 #               define LOC_INFO         "<core>: "
00055 #       endif
00056 #else
00057 #       define XCT2STR(i) #i
00058 #       define CT2STR(l)  XCT2STR(l)
00059 #
00060 #       ifdef MOD_NAME
00061 #               define LOC_INFO         MOD_NAME " [" __FILE__ ":" CT2STR(__LINE__) "]: "
00062 #       else
00063 #               define LOC_INFO         "<core> [" __FILE__ ":" CT2STR(__LINE__) "]: "
00064 #       endif
00065 #
00066 #       ifdef NO_LOG
00067 #               undef NO_LOG
00068 #       endif
00069 #endif /* NO_DEBUG */
00070 
00071 
00072 /*
00073  * Log levels
00074  */
00075 #define L_ALERT         -4
00076 #define L_BUG           -3
00077 #define L_CRIT          -2
00078 #define L_ERR           -1
00079 #define L_WARN          0
00080 #define L_NOTICE        1
00081 #define L_INFO          2
00082 #define L_DBG           3
00083 
00084 #define LOG_LEVEL2NAME(level)   (log_level_info[(level) - (L_ALERT)].name)
00085 #define LOG2SYSLOG_LEVEL(level) \
00086         (log_level_info[(level) - (L_ALERT)].syslog_level)
00087 
00088 
00089 /* my_pid(), process_no are from pt.h but we cannot #include it here
00090    because of circular dependencies */
00091 extern int process_no;
00092 extern int my_pid();
00093 
00094 /* non-zero if logging to stderr instead to the syslog */
00095 extern int log_stderr;
00096 
00097 /* maps log levels to their string name and corresponding syslog level */
00098 
00099 struct log_level_info {
00100         char *name;
00101         int syslog_level;
00102 };
00103 
00104 extern struct log_level_info log_level_info[];
00105 
00106 #ifndef NO_SIG_DEBUG
00107 /* protection against "simultaneous" printing from signal handlers */
00108 extern volatile int dprint_crit; 
00109 #endif
00110 
00111 int str2facility(char *s);
00112 int log_facility_fixup(void *handle, str *gname, str *name, void **val);
00113 
00114 
00115 /*
00116  * General logging macros
00117  *
00118  * LOG_(level, prefix, fmt, ...) prints "printf"-formatted log message to
00119  * stderr (if `log_stderr' is non-zero) or to syslog.  Note that `fmt' must
00120  * be constant. `prefix' is added to the beginning of the message.
00121  *
00122  * LOG(level, fmt, ...) is same as LOG_() with LOC_INFO prefix.
00123  */
00124 #ifdef NO_LOG
00125 
00126 #       ifdef __SUNPRO_C
00127 #               define LOG_(level, prefix, fmt, ...)
00128 #               define LOG(level, fmt, ...)
00129 #       else
00130 #               define LOG_(level, prefix, fmt, args...)
00131 #               define LOG(level, fmt, args...)
00132 #       endif
00133 
00134 #else
00135 
00136 #       ifdef NO_SIG_DEBUG
00137 #               define DPRINT_NON_CRIT          (1)
00138 #               define DPRINT_CRIT_ENTER
00139 #               define DPRINT_CRIT_EXIT
00140 #       else
00141 #               define DPRINT_NON_CRIT          (dprint_crit==0)
00142 #               define DPRINT_CRIT_ENTER        (dprint_crit++)
00143 #               define DPRINT_CRIT_EXIT         (dprint_crit--)
00144 #       endif
00145 
00146 #       ifdef __SUNPRO_C
00147 #               define LOG_(level, prefix, fmt, ...) \
00148                         do { \
00149                                 if (unlikely(cfg_get(core, core_cfg, debug) >= (level) && \
00150                                                 DPRINT_NON_CRIT)) { \
00151                                         DPRINT_CRIT_ENTER; \
00152                                         if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
00153                                                 if (unlikely(log_stderr)) { \
00154                                                         fprintf(stderr, "%2d(%d) %s: %s" fmt, \
00155                                                                         process_no, my_pid(), \
00156                                                                         LOG_LEVEL2NAME(level), (prefix), \
00157                                                                         __VA_ARGS__); \
00158                                                 } else { \
00159                                                         syslog(LOG2SYSLOG_LEVEL(level) | \
00160                                                                         cfg_get(core, core_cfg, log_facility),\
00161                                                                         "%s: %s" fmt, LOG_LEVEL2NAME(level),\
00162                                                                         (prefix), __VA_ARGS__); \
00163                                                 } \
00164                                         } else { \
00165                                                 if (log_stderr) { \
00166                                                         fprintf(stderr, "%2d(%d) %s" fmt, \
00167                                                                         process_no, my_pid(), \
00168                                                                         (prefix),  __VA_ARGS__); \
00169                                                 } else { \
00170                                                         if ((level)<L_ALERT) \
00171                                                                 syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
00172                                                                                 cfg_get(core, core_cfg, log_facility),\
00173                                                                                 "%s" fmt, (prefix), __VA_ARGS__); \
00174                                                         else \
00175                                                                 syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
00176                                                                                 cfg_get(core, core_cfg, log_facility),\
00177                                                                                 "%s" fmt, (prefix), __VA_ARGS__); \
00178                                                 } \
00179                                         } \
00180                                         DPRINT_CRIT_EXIT; \
00181                                 } \
00182                         } while(0)
00183                         
00184 #               define LOG(level, fmt, ...)  LOG_((level), LOC_INFO, fmt, __VA_ARGS__)
00185 
00186 #       else /* ! __SUNPRO_C */
00187 #               define LOG_(level, prefix, fmt, args...) \
00188                         do { \
00189                                 if (cfg_get(core, core_cfg, debug) >= (level) && \
00190                                                 DPRINT_NON_CRIT) { \
00191                                         DPRINT_CRIT_ENTER; \
00192                                         if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
00193                                                 if (unlikely(log_stderr)) { \
00194                                                         fprintf(stderr, "%2d(%d) %s: %s" fmt, \
00195                                                                         process_no, my_pid(), \
00196                                                                         LOG_LEVEL2NAME(level),(prefix), ## args);\
00197                                                 } else { \
00198                                                         syslog(LOG2SYSLOG_LEVEL(level) |\
00199                                                                         cfg_get(core, core_cfg, log_facility), \
00200                                                                         "%s: %s" fmt, LOG_LEVEL2NAME(level),\
00201                                                                         (prefix), ## args); \
00202                                                 } \
00203                                         } else { \
00204                                                 if (log_stderr) { \
00205                                                         fprintf(stderr, "%2d(%d) %s" fmt, \
00206                                                                                 process_no, my_pid(), \
00207                                                                                 (prefix), ## args); \
00208                                                 } else { \
00209                                                         if ((level)<L_ALERT) \
00210                                                                 syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
00211                                                                                 cfg_get(core, core_cfg, log_facility),\
00212                                                                                 "%s" fmt, (prefix), ## args); \
00213                                                         else \
00214                                                                 syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
00215                                                                                 cfg_get(core, core_cfg, log_facility),\
00216                                                                                 "%s" fmt, (prefix), ## args); \
00217                                                 } \
00218                                         } \
00219                                         DPRINT_CRIT_EXIT; \
00220                                 } \
00221                         } while(0)
00222                         
00223 #               define LOG(level, fmt, args...)  LOG_((level), LOC_INFO, fmt, ## args)
00224                 
00225 #       endif /* __SUNPRO_C */
00226 #endif /* NO_LOG */
00227 
00228 
00229 /*
00230  * Simplier, prefered logging macros for constant log level
00231  */
00232 #ifdef __SUNPRO_C
00233 #       define ALERT(...)  LOG(L_ALERT,  __VA_ARGS__)
00234 #       define BUG(...)    LOG(L_BUG,   __VA_ARGS__)
00235 #       define ERR(...)    LOG(L_ERR,    __VA_ARGS__)
00236 #       define WARN(...)   LOG(L_WARN,   __VA_ARGS__)
00237 #       define NOTICE(...) LOG(L_NOTICE, __VA_ARGS__)
00238 #       define INFO(...)   LOG(L_INFO,   __VA_ARGS__)
00239 
00240 #       ifdef NO_DEBUG
00241 #               define DBG(...)
00242 #       else
00243 #               define DBG(...)    LOG(L_DBG, __VA_ARGS__)
00244 #       endif           
00245 
00246 /* obsolete, do not use */
00247 #       define DEBUG(...) DBG(__VA_ARGS__)
00248                 
00249 #else
00250 #       define ALERT(fmt, args...)  LOG(L_ALERT,  fmt, ## args)
00251 #       define BUG(fmt, args...)    LOG(L_BUG,   fmt, ## args)
00252 #       define ERR(fmt, args...)    LOG(L_ERR,    fmt, ## args)
00253 #       define WARN(fmt, args...)   LOG(L_WARN,   fmt, ## args)
00254 #       define NOTICE(fmt, args...) LOG(L_NOTICE, fmt, ## args)
00255 #       define INFO(fmt, args...)   LOG(L_INFO,   fmt, ## args)
00256 
00257 #       ifdef NO_DEBUG
00258 #               define DBG(fmt, args...)
00259 #       else
00260 #               define DBG(fmt, args...)    LOG(L_DBG, fmt, ## args)
00261 #       endif           
00262 
00263 /* obsolete, do not use */
00264 #       define DEBUG(fmt, args...) DBG(fmt, ## args)
00265                 
00266 #endif /* __SUNPRO_C */
00267 
00268 
00269 #endif /* !dprint_h */

Generated on Thu Sep 9 04:15:42 2010 for SIPExpressRouter by  doxygen 1.3.9.1