• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

dprint.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 FhG Fokus
00003  *
00004  * This file is part of ser, a free SIP server.
00005  *
00006  * ser is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version
00010  *
00011  * ser is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License 
00017  * along with this program; if not, write to the Free Software 
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
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 
00040 #ifdef NO_LOG_FUNC_NAME
00041 #       undef LOG_FUNC_NAME
00042 #else
00043 /* by default log the function name */
00044 #       define LOG_FUNC_NAME
00045 #endif /* NO_LOG_FUNC_NAME */
00046 
00047 /* C >= 99 has __func__, older gcc versions have __FUNCTION__ */
00048 #if __STDC_VERSION__ < 199901L
00049 #       if __GNUC__ >= 2 && defined __FUNCTION__
00050 #               define _FUNC_NAME_ __FUNCTION__
00051 #       else
00052 #               define _FUNC_NAME_ ""
00053 #               undef LOG_FUNC_NAME
00054 #       endif
00055 #else
00056 #       define _FUNC_NAME_ __func__
00057 #endif
00058 
00059 #ifdef NO_DEBUG
00060 #       ifdef MOD_NAME
00061 #               define LOC_INFO         MOD_NAME ": "
00062 #       else
00063 #               define LOC_INFO         "<core>: "
00064 #       endif
00065 #else
00066 #       define XCT2STR(i) #i
00067 #       define CT2STR(l)  XCT2STR(l)
00068 #
00069 #       ifdef MOD_NAME
00070 #               define LOC_INFO         MOD_NAME " [" __FILE__ ":" CT2STR(__LINE__) "]: "
00071 #       else
00072 #               define LOC_INFO         "<core> [" __FILE__ ":" CT2STR(__LINE__) "]: "
00073 #       endif
00074 #
00075 #       ifdef NO_LOG
00076 #               undef NO_LOG
00077 #       endif
00078 #endif /* NO_DEBUG */
00079 
00080 
00081 /*
00082  * Log levels
00083  */
00084 #define L_ALERT         -5
00085 #define L_BUG           -4
00086 #define L_CRIT2         -3  /* like L_CRIT, but adds prefix */
00087 #define L_CRIT          -2  /* no prefix added */
00088 #define L_ERR           -1
00089 #define L_WARN          0
00090 #define L_NOTICE        1
00091 #define L_INFO          2
00092 #define L_DBG           3
00093 
00097 #define DEFAULT_FACILITY 0
00098 
00099 #define LOG_LEVEL2NAME(level)   (log_level_info[(level) - (L_ALERT)].name)
00100 #define LOG2SYSLOG_LEVEL(level) \
00101         (log_level_info[(level) - (L_ALERT)].syslog_level)
00102 
00103 
00106 extern int process_no;
00107 extern int my_pid(void);
00108 
00110 extern int log_stderr;
00111 
00114 struct log_level_info {
00115         char *name;
00116         int syslog_level;
00117 };
00118 
00120 int get_debug_level(void);
00121 void set_local_debug_level(int level);
00122 void reset_local_debug_level(void);
00123 
00124 #define is_printable(level) (get_debug_level()>=(level))
00125 extern struct log_level_info log_level_info[];
00126 extern char *log_name;
00127 
00128 #ifndef NO_SIG_DEBUG
00129 
00130 extern volatile int dprint_crit; 
00131 #endif
00132 
00133 int str2facility(char *s);
00134 int log_facility_fixup(void *handle, str *gname, str *name, void **val);
00135 
00136 
00146 #ifdef NO_LOG
00147 
00148 #       ifdef __SUNPRO_C
00149 #               define LOG_(facility, level, prefix, fmt, ...)
00150 #               define LOG(level, fmt, ...)
00151 #               define LOG_FC(facility, level, fmt, ...)
00152 #       else
00153 #               define LOG_(facility, level, prefix, fmt, args...)
00154 #               define LOG(level, fmt, args...)
00155 #               define LOG_FC(facility, level, fmt, args...)
00156 #       endif
00157 
00158 #else
00159 
00160 #       ifdef NO_SIG_DEBUG
00161 #               define DPRINT_NON_CRIT          (1)
00162 #               define DPRINT_CRIT_ENTER
00163 #               define DPRINT_CRIT_EXIT
00164 #       else
00165 #               define DPRINT_NON_CRIT          (dprint_crit==0)
00166 #               define DPRINT_CRIT_ENTER        (dprint_crit++)
00167 #               define DPRINT_CRIT_EXIT         (dprint_crit--)
00168 #       endif
00169 
00170 #       ifdef __SUNPRO_C
00171 #               define LOG_(facility, level, prefix, fmt, ...) \
00172                         do { \
00173                                 if (unlikely(get_debuglevel() >= (level) && \
00174                                                 DPRINT_NON_CRIT)) { \
00175                                         DPRINT_CRIT_ENTER; \
00176                                         if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
00177                                                 if (unlikely(log_stderr)) { \
00178                                                         fprintf(stderr, "%2d(%d) %s: %s" fmt, \
00179                                                                         process_no, my_pid(), \
00180                                                                         LOG_LEVEL2NAME(level), (prefix), \
00181                                                                         __VA_ARGS__); \
00182                                                 } else { \
00183                                                         syslog(LOG2SYSLOG_LEVEL(level) | \
00184                                                                    (((facility) != DEFAULT_FACILITY) ? \
00185                                                                         (facility) : \
00186                                                                         cfg_get(core, core_cfg, log_facility)), \
00187                                                                         "%s: %s" fmt, LOG_LEVEL2NAME(level),\
00188                                                                         (prefix), __VA_ARGS__); \
00189                                                 } \
00190                                         } else { \
00191                                                 if (log_stderr) { \
00192                                                         fprintf(stderr, "%2d(%d) %s" fmt, \
00193                                                                         process_no, my_pid(), \
00194                                                                         (prefix),  __VA_ARGS__); \
00195                                                 } else { \
00196                                                         if ((level)<L_ALERT) \
00197                                                                 syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
00198                                                                            (((facility) != DEFAULT_FACILITY) ? \
00199                                                                                 (facility) : \
00200                                                                                 cfg_get(core, core_cfg, log_facility)),\
00201                                                                            "%s" fmt, (prefix), __VA_ARGS__); \
00202                                                         else \
00203                                                                 syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
00204                                                                            (((facility) != DEFAULT_FACILITY) ? \
00205                                                                                 (facility) : \
00206                                                                                 cfg_get(core, core_cfg, log_facility)),\
00207                                                                            "%s" fmt, (prefix), __VA_ARGS__); \
00208                                                 } \
00209                                         } \
00210                                         DPRINT_CRIT_EXIT; \
00211                                 } \
00212                         } while(0)
00213                         
00214 #               ifdef LOG_FUNC_NAME
00215 #                       define LOG(level, fmt, ...) \
00216         LOG_(DEFAULT_FACILITY, (level), LOC_INFO, "%s(): " fmt,\
00217                                 _FUNC_NAME_, __VA_ARGS__)
00218 
00219 #                       define LOG_FC(facility, level, fmt, ...) \
00220         LOG_((facility), (level), LOC_INFO, "%s(): " fmt,\
00221                                 _FUNC_NAME_, __VA_ARGS__)
00222 #               else /* LOG_FUNC_NAME */
00223 
00224 #                       define LOG(level, fmt, ...) \
00225         LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt, __VA_ARGS__)
00226 
00227 #                       define LOG_FC(facility, level, fmt, ...) \
00228         LOG_((facility), (level), LOC_INFO, fmt, __VA_ARGS__)
00229 
00230 #               endif /* LOG_FUNC_NAME */
00231 
00232 #       else /* ! __SUNPRO_C */
00233 #               define LOG_(facility, level, prefix, fmt, args...) \
00234                         do { \
00235                                 if (get_debug_level() >= (level) && \
00236                                                 DPRINT_NON_CRIT) { \
00237                                         DPRINT_CRIT_ENTER; \
00238                                         if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
00239                                                 if (unlikely(log_stderr)) { \
00240                                                         fprintf(stderr, "%2d(%d) %s: %s" fmt, \
00241                                                                         process_no, my_pid(), \
00242                                                                         LOG_LEVEL2NAME(level), \
00243                                                                         (prefix) , ## args);\
00244                                                 } else { \
00245                                                         syslog(LOG2SYSLOG_LEVEL(level) |\
00246                                                                    (((facility) != DEFAULT_FACILITY) ? \
00247                                                                         (facility) : \
00248                                                                         cfg_get(core, core_cfg, log_facility)), \
00249                                                                         "%s: %s" fmt, LOG_LEVEL2NAME(level),\
00250                                                                         (prefix) , ## args); \
00251                                                 } \
00252                                         } else { \
00253                                                 if (log_stderr) { \
00254                                                         fprintf(stderr, "%2d(%d) %s" fmt, \
00255                                                                                 process_no, my_pid(), \
00256                                                                                 (prefix) , ## args); \
00257                                                 } else { \
00258                                                         if ((level)<L_ALERT) \
00259                                                                 syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
00260                                                                            (((facility) != DEFAULT_FACILITY) ? \
00261                                                                                 (facility) : \
00262                                                                                 cfg_get(core, core_cfg, log_facility)),\
00263                                                                                 "%s" fmt, (prefix) , ## args); \
00264                                                         else \
00265                                                                 syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
00266                                                                            (((facility) != DEFAULT_FACILITY) ? \
00267                                                                                 (facility) : \
00268                                                                                 cfg_get(core, core_cfg, log_facility)),\
00269                                                                                 "%s" fmt, (prefix) , ## args); \
00270                                                 } \
00271                                         } \
00272                                         DPRINT_CRIT_EXIT; \
00273                                 } \
00274                         } while(0)
00275                         
00276 #               ifdef LOG_FUNC_NAME
00277 #                       define LOG(level, fmt, args...) \
00278         LOG_(DEFAULT_FACILITY, (level), LOC_INFO, "%s(): " fmt ,\
00279                         _FUNC_NAME_, ## args)
00280 
00281 #                       define LOG_FC(facility, level, fmt, args...) \
00282         LOG_((facility), (level), LOC_INFO, "%s(): " fmt , _FUNC_NAME_, ## args)
00283 
00284 #               else /* LOG_FUNC_NAME */
00285 #                       define LOG(level, fmt, args...) \
00286         LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt , ## args)
00287 #                       define LOG_FC(facility, level, fmt, args...) \
00288         LOG_((facility), (level), LOC_INFO, fmt , ## args)
00289 
00290 #               endif /* LOG_FUNC_NAME */
00291 #       endif /* __SUNPRO_C */
00292 #endif /* NO_LOG */
00293 
00294 
00298 /*@ { */
00299 #ifdef __SUNPRO_C
00300 #       define ALERT(...)  LOG(L_ALERT,  __VA_ARGS__)
00301 #       define BUG(...)    LOG(L_BUG,   __VA_ARGS__)
00302 #       define ERR(...)    LOG(L_ERR,    __VA_ARGS__)
00303 #       define WARN(...)   LOG(L_WARN,   __VA_ARGS__)
00304 #       define NOTICE(...) LOG(L_NOTICE, __VA_ARGS__)
00305 #       define INFO(...)   LOG(L_INFO,   __VA_ARGS__)
00306 #       define CRIT(...)    LOG(L_CRIT2,   __VA_ARGS__)
00307 
00308 #       ifdef NO_DEBUG
00309 #               define DBG(...)
00310 #       else
00311 #               define DBG(...)    LOG(L_DBG, __VA_ARGS__)
00312 #       endif           
00313 /*@ } */
00314 
00315 /* obsolete, do not use */
00316 #       define DEBUG(...) DBG(__VA_ARGS__)
00317 
00318 #else /* ! __SUNPRO_C */
00319 #       define ALERT(fmt, args...)  LOG(L_ALERT,  fmt , ## args)
00320 #       define BUG(fmt, args...)    LOG(L_BUG,   fmt , ## args)
00321 #       define ERR(fmt, args...)    LOG(L_ERR,    fmt , ## args)
00322 #       define WARN(fmt, args...)   LOG(L_WARN,   fmt , ## args)
00323 #       define NOTICE(fmt, args...) LOG(L_NOTICE, fmt , ## args)
00324 #       define INFO(fmt, args...)   LOG(L_INFO,   fmt , ## args)
00325 #       define CRIT(fmt, args...)   LOG(L_CRIT2,   fmt , ## args)
00326 
00327 #       ifdef NO_DEBUG
00328 #               define DBG(fmt, args...)
00329 #       else
00330 #               define DBG(fmt, args...)    LOG(L_DBG, fmt , ## args)
00331 #       endif           
00332 
00333 /* obsolete, do not use */
00334 #       define DEBUG(fmt, args...) DBG(fmt , ## args)
00335                 
00336 #endif /* __SUNPRO_C */
00337 
00338 
00339 /* kamailio/openser compatibility */
00340 
00341 #define LM_GEN1 LOG
00342 #define LM_GEN2 LOG_FC
00343 #define LM_ALERT ALERT
00344 #define LM_CRIT  CRIT
00345 #define LM_ERR ERR
00346 #define LM_WARN WARN
00347 #define LM_NOTICE NOTICE
00348 #define LM_INFO INFO
00349 #define LM_DBG DEBUG
00350 
00351 #endif /* !dprint_h */

Generated on Tue May 22 2012 13:10:06 for SIP Router by  doxygen 1.7.1