dprint.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * [email protected]
  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. #ifndef dprint_h
  28. #define dprint_h
  29. #include <assert.h>
  30. #include <syslog.h>
  31. #include <stdio.h> /* stderr, fprintf() */
  32. #include "compiler_opt.h"
  33. #include "cfg_core.h"
  34. /* C >= 99 has __func__, older gcc versions have __FUNCTION__ */
  35. #if __STDC_VERSION__ < 199901L
  36. # if __GNUC__ >= 2
  37. # define _FUNC_NAME_ __FUNCTION__
  38. # else
  39. # define _FUNC_NAME_ ""
  40. # endif
  41. #else
  42. # define _FUNC_NAME_ __func__
  43. #endif
  44. #ifdef NO_DEBUG
  45. # ifdef MOD_NAME
  46. # define LOC_INFO MOD_NAME ": "
  47. # else
  48. # define LOC_INFO "<core>: "
  49. # endif
  50. #else
  51. # define XCT2STR(i) #i
  52. # define CT2STR(l) XCT2STR(l)
  53. #
  54. # ifdef MOD_NAME
  55. # define LOC_INFO MOD_NAME " [" __FILE__ ":" CT2STR(__LINE__) "]: "
  56. # else
  57. # define LOC_INFO "<core> [" __FILE__ ":" CT2STR(__LINE__) "]: "
  58. # endif
  59. #
  60. # ifdef NO_LOG
  61. # undef NO_LOG
  62. # endif
  63. #endif /* NO_DEBUG */
  64. /*
  65. * Log levels
  66. */
  67. #define L_ALERT -3
  68. #define L_CRIT -2
  69. #define L_ERR -1
  70. #define L_WARN 0
  71. #define L_NOTICE 1
  72. #define L_INFO 2
  73. #define L_DBG 3
  74. #define LOG_LEVEL2NAME(level) (log_level_info[(level) - (L_ALERT)].name)
  75. #define LOG2SYSLOG_LEVEL(level) \
  76. (log_level_info[(level) - (L_ALERT)].syslog_level)
  77. /* my_pid(), process_no are from pt.h but we cannot #include it here
  78. because of circular dependencies */
  79. extern int process_no;
  80. extern int my_pid();
  81. /* non-zero if logging to stderr instead to the syslog */
  82. extern int log_stderr;
  83. /* maps log levels to their string name and corresponding syslog level */
  84. struct log_level_info {
  85. char *name;
  86. int syslog_level;
  87. };
  88. extern struct log_level_info log_level_info[];
  89. #ifndef NO_SIG_DEBUG
  90. /* protection against "simultaneous" printing from signal handlers */
  91. extern volatile int dprint_crit;
  92. #endif
  93. int str2facility(char *s);
  94. int log_facility_fixup(void *handle, str *gname, str *name, void **val);
  95. /*
  96. * General logging macros
  97. *
  98. * LOG_(level, prefix, fmt, ...) prints "printf"-formatted log message to
  99. * stderr (if `log_stderr' is non-zero) or to syslog. Note that `fmt' must
  100. * be constant. `prefix' is added to the beginning of the message.
  101. *
  102. * LOG(level, fmt, ...) is same as LOG_() with LOC_INFO prefix.
  103. */
  104. #ifdef NO_LOG
  105. # ifdef __SUNPRO_C
  106. # define LOG_(level, prefix, fmt, ...)
  107. # define LOG(level, fmt, ...)
  108. # else
  109. # define LOG_(level, prefix, fmt, args...)
  110. # define LOG(level, fmt, args...)
  111. # endif
  112. #else
  113. # ifdef NO_SIG_DEBUG
  114. # define DPRINT_NON_CRIT (1)
  115. # define DPRINT_CRIT_ENTER
  116. # define DPRINT_CRIT_EXIT
  117. # else
  118. # define DPRINT_NON_CRIT (dprint_crit==0)
  119. # define DPRINT_CRIT_ENTER (dprint_crit++)
  120. # define DPRINT_CRIT_EXIT (dprint_crit--)
  121. # endif
  122. # ifdef __SUNPRO_C
  123. # define LOG_(level, prefix, fmt, ...) \
  124. do { \
  125. if (unlikely(cfg_get(core, core_cfg, debug) >= (level) && \
  126. DPRINT_NON_CRIT)) { \
  127. DPRINT_CRIT_ENTER; \
  128. if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
  129. if (unlikely(log_stderr)) { \
  130. fprintf(stderr, "%2d(%d) %s: %s" fmt, \
  131. process_no, my_pid(), \
  132. LOG_LEVEL2NAME(level), (prefix), \
  133. __VA_ARGS__); \
  134. } else { \
  135. syslog(LOG2SYSLOG_LEVEL(level) | \
  136. cfg_get(core, core_cfg, log_facility),\
  137. "%s: %s" fmt, LOG_LEVEL2NAME(level),\
  138. (prefix), __VA_ARGS__); \
  139. } \
  140. } else { \
  141. if (log_stderr) { \
  142. fprintf(stderr, "%2d(%d) %s" fmt, \
  143. process_no, my_pid(), \
  144. (prefix), __VA_ARGS__); \
  145. } else { \
  146. if ((level)<L_ALERT) \
  147. syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
  148. cfg_get(core, core_cfg, log_facility),\
  149. "%s" fmt, (prefix), __VA_ARGS__); \
  150. else \
  151. syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
  152. cfg_get(core, core_cfg, log_facility),\
  153. "%s" fmt, (prefix), __VA_ARGS__); \
  154. } \
  155. } \
  156. DPRINT_CRIT_EXIT; \
  157. } \
  158. } while(0)
  159. # define LOG(level, fmt, ...) LOG_((level), LOC_INFO, fmt, __VA_ARGS__)
  160. # else /* ! __SUNPRO_C */
  161. # define LOG_(level, prefix, fmt, args...) \
  162. do { \
  163. if (cfg_get(core, core_cfg, debug) >= (level) && \
  164. DPRINT_NON_CRIT) { \
  165. DPRINT_CRIT_ENTER; \
  166. if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
  167. if (unlikely(log_stderr)) { \
  168. fprintf(stderr, "%2d(%d) %s: %s" fmt, \
  169. process_no, my_pid(), \
  170. LOG_LEVEL2NAME(level),(prefix), ## args);\
  171. } else { \
  172. syslog(LOG2SYSLOG_LEVEL(level) |\
  173. cfg_get(core, core_cfg, log_facility), \
  174. "%s: %s" fmt, LOG_LEVEL2NAME(level),\
  175. (prefix), ## args); \
  176. } \
  177. } else { \
  178. if (log_stderr) { \
  179. fprintf(stderr, "%2d(%d) %s" fmt, \
  180. process_no, my_pid(), \
  181. (prefix), ## args); \
  182. } else { \
  183. if ((level)<L_ALERT) \
  184. syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
  185. cfg_get(core, core_cfg, log_facility),\
  186. "%s" fmt, (prefix), ## args); \
  187. else \
  188. syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
  189. cfg_get(core, core_cfg, log_facility),\
  190. "%s" fmt, (prefix), ## args); \
  191. } \
  192. } \
  193. DPRINT_CRIT_EXIT; \
  194. } \
  195. } while(0)
  196. # define LOG(level, fmt, args...) LOG_((level), LOC_INFO, fmt, ## args)
  197. # endif /* __SUNPRO_C */
  198. #endif /* NO_LOG */
  199. /*
  200. * Simplier, prefered logging macros for constant log level
  201. */
  202. #ifdef __SUNPRO_C
  203. # define ALERT(...) LOG(L_ALERT, __VA_ARGS__)
  204. # define BUG(...) LOG(L_CRIT, __VA_ARGS__)
  205. # define ERR(...) LOG(L_ERR, __VA_ARGS__)
  206. # define WARN(...) LOG(L_WARN, __VA_ARGS__)
  207. # define NOTICE(...) LOG(L_NOTICE, __VA_ARGS__)
  208. # define INFO(...) LOG(L_INFO, __VA_ARGS__)
  209. # ifdef NO_DEBUG
  210. # define DBG(...)
  211. # else
  212. # define DBG(...) LOG(L_DBG, __VA_ARGS__)
  213. # endif
  214. /* obsolete, do not use */
  215. # define DEBUG(...) DBG(__VA_ARGS__)
  216. #else
  217. # define ALERT(fmt, args...) LOG(L_ALERT, fmt, ## args)
  218. # define BUG(fmt, args...) LOG(L_CRIT, fmt, ## args)
  219. # define ERR(fmt, args...) LOG(L_ERR, fmt, ## args)
  220. # define WARN(fmt, args...) LOG(L_WARN, fmt, ## args)
  221. # define NOTICE(fmt, args...) LOG(L_NOTICE, fmt, ## args)
  222. # define INFO(fmt, args...) LOG(L_INFO, fmt, ## args)
  223. # ifdef NO_DEBUG
  224. # define DBG(fmt, args...)
  225. # else
  226. # define DBG(fmt, args...) LOG(L_DBG, fmt, ## args)
  227. # endif
  228. /* obsolete, do not use */
  229. # define DEBUG(fmt, args...) DBG(fmt, ## args)
  230. #endif /* __SUNPRO_C */
  231. #endif /* !dprint_h */