dprint.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * ser is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * @file
  22. * @brief SIP-router core :: debug printing
  23. * @ingroup core
  24. * Module: @ref core
  25. */
  26. #ifndef dprint_h
  27. #define dprint_h
  28. #include <assert.h>
  29. #include <syslog.h>
  30. #include <stdio.h> /* stderr, fprintf() */
  31. #include "compiler_opt.h"
  32. #include "cfg_core.h"
  33. /** if defined the function name will also be logged. */
  34. #ifdef NO_LOG_FUNC_NAME
  35. # undef LOG_FUNC_NAME
  36. #else
  37. /* by default log the function name */
  38. # define LOG_FUNC_NAME
  39. #endif /* NO_LOG_FUNC_NAME */
  40. /* C >= 99 has __func__, older gcc versions have __FUNCTION__ */
  41. #if __STDC_VERSION__ < 199901L
  42. # if __GNUC__ >= 2
  43. # define _FUNC_NAME_ __FUNCTION__
  44. # else
  45. # define _FUNC_NAME_ ""
  46. # undef LOG_FUNC_NAME
  47. # endif
  48. #else
  49. # define _FUNC_NAME_ __func__
  50. #endif
  51. #ifdef NO_DEBUG
  52. # ifdef MOD_NAME
  53. # define LOC_INFO MOD_NAME ": "
  54. # else
  55. # define LOC_INFO "<core>: "
  56. # endif
  57. #else
  58. # define XCT2STR(i) #i
  59. # define CT2STR(l) XCT2STR(l)
  60. #
  61. # ifdef MOD_NAME
  62. # define LOC_INFO MOD_NAME " [" __FILE__ ":" CT2STR(__LINE__) "]: "
  63. # else
  64. # define LOC_INFO "<core> [" __FILE__ ":" CT2STR(__LINE__) "]: "
  65. # endif
  66. #
  67. # ifdef NO_LOG
  68. # undef NO_LOG
  69. # endif
  70. #endif /* NO_DEBUG */
  71. /*
  72. * Log levels
  73. */
  74. #define L_MIN -5
  75. #define L_ALERT -5
  76. #define L_BUG -4
  77. #define L_CRIT2 -3 /* like L_CRIT, but adds prefix */
  78. #define L_CRIT -2 /* no prefix added */
  79. #define L_ERR -1
  80. #define L_WARN 0
  81. #define L_NOTICE 1
  82. #define L_INFO 2
  83. #define L_DBG 3
  84. #define L_MAX 3
  85. /** @brief This is the facility value used to indicate that the caller of the macro
  86. * did not override the facility. Value 0 (the defaul) is LOG_KERN on Linux
  87. */
  88. #define DEFAULT_FACILITY 0
  89. #define LOG_LEVEL2NAME(level) (log_level_info[(level) - (L_ALERT)].name)
  90. #define LOG2SYSLOG_LEVEL(level) \
  91. (log_level_info[(level) - (L_ALERT)].syslog_level)
  92. /** @brief my_pid(), process_no are from pt.h but we cannot \#include it here
  93. because of circular dependencies */
  94. extern int process_no;
  95. extern int my_pid(void);
  96. /** @brief non-zero if logging to stderr instead to the syslog */
  97. extern int log_stderr;
  98. extern int log_color;
  99. /** @brief maps log levels to their string name and corresponding syslog level */
  100. struct log_level_info {
  101. char *name;
  102. int syslog_level;
  103. };
  104. /** @brief per process debug level handling */
  105. int get_debug_level(void);
  106. void set_local_debug_level(int level);
  107. void reset_local_debug_level(void);
  108. #define is_printable(level) (get_debug_level()>=(level))
  109. extern struct log_level_info log_level_info[];
  110. extern char *log_name;
  111. #ifndef NO_SIG_DEBUG
  112. /** @brief protection against "simultaneous" printing from signal handlers */
  113. extern volatile int dprint_crit;
  114. #endif
  115. int str2facility(char *s);
  116. int log_facility_fixup(void *handle, str *gname, str *name, void **val);
  117. void dprint_color(int level);
  118. void dprint_color_reset(void);
  119. void dprint_color_update(int level, char f, char b);
  120. void dprint_init_colors(void);
  121. void dprint_term_color(char f, char b, str *obuf);
  122. /** @brief
  123. * General logging macros
  124. *
  125. * LOG_(level, prefix, fmt, ...) prints "printf"-formatted log message to
  126. * stderr (if `log_stderr' is non-zero) or to syslog. Note that `fmt' must
  127. * be constant. `prefix' is added to the beginning of the message.
  128. *
  129. * LOG(level, fmt, ...) is same as LOG_() with LOC_INFO prefix.
  130. */
  131. #ifdef NO_LOG
  132. # ifdef __SUNPRO_C
  133. # define LOG_(facility, level, prefix, fmt, ...)
  134. # define LOG(level, fmt, ...)
  135. # define LOG_FC(facility, level, fmt, ...)
  136. # else
  137. # define LOG_(facility, level, prefix, fmt, args...)
  138. # define LOG(level, fmt, args...)
  139. # define LOG_FC(facility, level, fmt, args...)
  140. # endif
  141. #else
  142. # ifdef NO_SIG_DEBUG
  143. # define DPRINT_NON_CRIT (1)
  144. # define DPRINT_CRIT_ENTER
  145. # define DPRINT_CRIT_EXIT
  146. # else
  147. # define DPRINT_NON_CRIT (dprint_crit==0)
  148. # define DPRINT_CRIT_ENTER (dprint_crit++)
  149. # define DPRINT_CRIT_EXIT (dprint_crit--)
  150. # endif
  151. # ifdef __SUNPRO_C
  152. # define LOG_(facility, level, prefix, fmt, ...) \
  153. do { \
  154. if (unlikely(get_debuglevel() >= (level) && \
  155. DPRINT_NON_CRIT)) { \
  156. DPRINT_CRIT_ENTER; \
  157. if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
  158. if (unlikely(log_stderr)) { \
  159. if (unlikely(log_color)) dprint_color(level); \
  160. fprintf(stderr, "%2d(%d) %s: %s" fmt, \
  161. process_no, my_pid(), \
  162. LOG_LEVEL2NAME(level), (prefix), \
  163. __VA_ARGS__); \
  164. if (unlikely(log_color)) dprint_color_reset(); \
  165. } else { \
  166. syslog(LOG2SYSLOG_LEVEL(level) | \
  167. (((facility) != DEFAULT_FACILITY) ? \
  168. (facility) : \
  169. cfg_get(core, core_cfg, log_facility)), \
  170. "%s: %s" fmt, LOG_LEVEL2NAME(level),\
  171. (prefix), __VA_ARGS__); \
  172. } \
  173. } else { \
  174. if (log_stderr) { \
  175. if (unlikely(log_color)) dprint_color(level); \
  176. fprintf(stderr, "%2d(%d) %s" fmt, \
  177. process_no, my_pid(), \
  178. (prefix), __VA_ARGS__); \
  179. if (unlikely(log_color)) dprint_color_reset(); \
  180. } else { \
  181. if ((level)<L_ALERT) \
  182. syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
  183. (((facility) != DEFAULT_FACILITY) ? \
  184. (facility) : \
  185. cfg_get(core, core_cfg, log_facility)),\
  186. "%s" fmt, (prefix), __VA_ARGS__); \
  187. else \
  188. syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
  189. (((facility) != DEFAULT_FACILITY) ? \
  190. (facility) : \
  191. cfg_get(core, core_cfg, log_facility)),\
  192. "%s" fmt, (prefix), __VA_ARGS__); \
  193. } \
  194. } \
  195. DPRINT_CRIT_EXIT; \
  196. } \
  197. } while(0)
  198. # ifdef LOG_FUNC_NAME
  199. # define LOG(level, fmt, ...) \
  200. LOG_(DEFAULT_FACILITY, (level), LOC_INFO, "%s(): " fmt,\
  201. _FUNC_NAME_, __VA_ARGS__)
  202. # define LOG_FC(facility, level, fmt, ...) \
  203. LOG_((facility), (level), LOC_INFO, "%s(): " fmt,\
  204. _FUNC_NAME_, __VA_ARGS__)
  205. # else /* LOG_FUNC_NAME */
  206. # define LOG(level, fmt, ...) \
  207. LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt, __VA_ARGS__)
  208. # define LOG_FC(facility, level, fmt, ...) \
  209. LOG_((facility), (level), LOC_INFO, fmt, __VA_ARGS__)
  210. # endif /* LOG_FUNC_NAME */
  211. # else /* ! __SUNPRO_C */
  212. # define LOG_(facility, level, prefix, fmt, args...) \
  213. do { \
  214. if (get_debug_level() >= (level) && \
  215. DPRINT_NON_CRIT) { \
  216. DPRINT_CRIT_ENTER; \
  217. if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
  218. if (unlikely(log_stderr)) { \
  219. if (unlikely(log_color)) dprint_color(level); \
  220. fprintf(stderr, "%2d(%d) %s: %s" fmt, \
  221. process_no, my_pid(), \
  222. LOG_LEVEL2NAME(level), \
  223. (prefix) , ## args);\
  224. if (unlikely(log_color)) dprint_color_reset(); \
  225. } else { \
  226. syslog(LOG2SYSLOG_LEVEL(level) |\
  227. (((facility) != DEFAULT_FACILITY) ? \
  228. (facility) : \
  229. cfg_get(core, core_cfg, log_facility)), \
  230. "%s: %s" fmt, LOG_LEVEL2NAME(level),\
  231. (prefix) , ## args); \
  232. } \
  233. } else { \
  234. if (log_stderr) { \
  235. if (unlikely(log_color)) dprint_color(level); \
  236. fprintf(stderr, "%2d(%d) %s" fmt, \
  237. process_no, my_pid(), \
  238. (prefix) , ## args); \
  239. if (unlikely(log_color)) dprint_color_reset(); \
  240. } else { \
  241. if ((level)<L_ALERT) \
  242. syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
  243. (((facility) != DEFAULT_FACILITY) ? \
  244. (facility) : \
  245. cfg_get(core, core_cfg, log_facility)),\
  246. "%s" fmt, (prefix) , ## args); \
  247. else \
  248. syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
  249. (((facility) != DEFAULT_FACILITY) ? \
  250. (facility) : \
  251. cfg_get(core, core_cfg, log_facility)),\
  252. "%s" fmt, (prefix) , ## args); \
  253. } \
  254. } \
  255. DPRINT_CRIT_EXIT; \
  256. } \
  257. } while(0)
  258. # ifdef LOG_FUNC_NAME
  259. # define LOG(level, fmt, args...) \
  260. LOG_(DEFAULT_FACILITY, (level), LOC_INFO, "%s(): " fmt ,\
  261. _FUNC_NAME_, ## args)
  262. # define LOG_FC(facility, level, fmt, args...) \
  263. LOG_((facility), (level), LOC_INFO, "%s(): " fmt , _FUNC_NAME_, ## args)
  264. # else /* LOG_FUNC_NAME */
  265. # define LOG(level, fmt, args...) \
  266. LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt , ## args)
  267. # define LOG_FC(facility, level, fmt, args...) \
  268. LOG_((facility), (level), LOC_INFO, fmt , ## args)
  269. # endif /* LOG_FUNC_NAME */
  270. # endif /* __SUNPRO_C */
  271. #endif /* NO_LOG */
  272. /** @name SimpleLog
  273. * Simplier, prefered logging macros for constant log level
  274. */
  275. /*@ { */
  276. #ifdef __SUNPRO_C
  277. # define ALERT(...) LOG(L_ALERT, __VA_ARGS__)
  278. # define BUG(...) LOG(L_BUG, __VA_ARGS__)
  279. # define ERR(...) LOG(L_ERR, __VA_ARGS__)
  280. # define WARN(...) LOG(L_WARN, __VA_ARGS__)
  281. # define NOTICE(...) LOG(L_NOTICE, __VA_ARGS__)
  282. # define INFO(...) LOG(L_INFO, __VA_ARGS__)
  283. # define CRIT(...) LOG(L_CRIT2, __VA_ARGS__)
  284. # ifdef NO_DEBUG
  285. # define DBG(...)
  286. # else
  287. # define DBG(...) LOG(L_DBG, __VA_ARGS__)
  288. # endif
  289. /*@ } */
  290. /* obsolete, do not use */
  291. # define DEBUG(...) DBG(__VA_ARGS__)
  292. #else /* ! __SUNPRO_C */
  293. # define ALERT(fmt, args...) LOG(L_ALERT, fmt , ## args)
  294. # define BUG(fmt, args...) LOG(L_BUG, fmt , ## args)
  295. # define ERR(fmt, args...) LOG(L_ERR, fmt , ## args)
  296. # define WARN(fmt, args...) LOG(L_WARN, fmt , ## args)
  297. # define NOTICE(fmt, args...) LOG(L_NOTICE, fmt , ## args)
  298. # define INFO(fmt, args...) LOG(L_INFO, fmt , ## args)
  299. # define CRIT(fmt, args...) LOG(L_CRIT2, fmt , ## args)
  300. # ifdef NO_DEBUG
  301. # define DBG(fmt, args...)
  302. # else
  303. # define DBG(fmt, args...) LOG(L_DBG, fmt , ## args)
  304. # endif
  305. /* obsolete, do not use */
  306. # define DEBUG(fmt, args...) DBG(fmt , ## args)
  307. #endif /* __SUNPRO_C */
  308. /* kamailio/openser compatibility */
  309. #define LM_GEN1 LOG
  310. #define LM_GEN2 LOG_FC
  311. #define LM_ALERT ALERT
  312. #define LM_CRIT CRIT
  313. #define LM_ERR ERR
  314. #define LM_WARN WARN
  315. #define LM_NOTICE NOTICE
  316. #define LM_INFO INFO
  317. #define LM_DBG DEBUG
  318. #endif /* !dprint_h */