dprint.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. # define LOG_MNAME MOD_NAME
  55. # else
  56. # define LOC_INFO "<core>: "
  57. # define LOG_MNAME "core"
  58. # endif
  59. #else
  60. # define XCT2STR(i) #i
  61. # define CT2STR(l) XCT2STR(l)
  62. #
  63. # ifdef MOD_NAME
  64. # define LOC_INFO MOD_NAME " [" __FILE__ ":" CT2STR(__LINE__) "]: "
  65. # define LOG_MNAME MOD_NAME
  66. # else
  67. # define LOC_INFO "<core> [" __FILE__ ":" CT2STR(__LINE__) "]: "
  68. # define LOG_MNAME "core"
  69. # endif
  70. #
  71. # ifdef NO_LOG
  72. # undef NO_LOG
  73. # endif
  74. #endif /* NO_DEBUG */
  75. #define LOG_MNAME_LEN (sizeof(LOG_MNAME)-1)
  76. /*
  77. * Log levels
  78. */
  79. #define L_MIN -5
  80. #define L_ALERT -5
  81. #define L_BUG -4
  82. #define L_CRIT2 -3 /* like L_CRIT, but adds prefix */
  83. #define L_CRIT -2 /* no prefix added */
  84. #define L_ERR -1
  85. #define L_WARN 0
  86. #define L_NOTICE 1
  87. #define L_INFO 2
  88. #define L_DBG 3
  89. #define L_MAX 3
  90. /** @brief This is the facility value used to indicate that the caller of the macro
  91. * did not override the facility. Value 0 (the defaul) is LOG_KERN on Linux
  92. */
  93. #define DEFAULT_FACILITY 0
  94. #define LOG_LEVEL2NAME(level) (log_level_info[(level) - (L_ALERT)].name)
  95. #define LOG2SYSLOG_LEVEL(level) \
  96. (log_level_info[(level) - (L_ALERT)].syslog_level)
  97. /** @brief my_pid(), process_no are from pt.h but we cannot \#include it here
  98. because of circular dependencies */
  99. extern int process_no;
  100. extern int my_pid(void);
  101. /** @brief non-zero if logging to stderr instead to the syslog */
  102. extern int log_stderr;
  103. extern int log_color;
  104. /** @brief maps log levels to their string name and corresponding syslog level */
  105. struct log_level_info {
  106. char *name;
  107. int syslog_level;
  108. };
  109. /** @brief per process debug level handling */
  110. int get_debug_level(char *mname, int mnlen);
  111. void set_local_debug_level(int level);
  112. void reset_local_debug_level(void);
  113. typedef int (*get_module_debug_level_f)(char *mname, int mnlen, int *mlevel);
  114. void set_module_debug_level_cb(get_module_debug_level_f f);
  115. #define is_printable(level) (get_debug_level(LOG_MNAME, LOG_MNAME_LEN)>=(level))
  116. extern struct log_level_info log_level_info[];
  117. extern char *log_name;
  118. #ifndef NO_SIG_DEBUG
  119. /** @brief protection against "simultaneous" printing from signal handlers */
  120. extern volatile int dprint_crit;
  121. #endif
  122. int str2facility(char *s);
  123. int log_facility_fixup(void *handle, str *gname, str *name, void **val);
  124. void dprint_color(int level);
  125. void dprint_color_reset(void);
  126. void dprint_color_update(int level, char f, char b);
  127. void dprint_init_colors(void);
  128. void dprint_term_color(char f, char b, str *obuf);
  129. /** @brief
  130. * General logging macros
  131. *
  132. * LOG_(level, prefix, fmt, ...) prints "printf"-formatted log message to
  133. * stderr (if `log_stderr' is non-zero) or to syslog. Note that `fmt' must
  134. * be constant. `prefix' is added to the beginning of the message.
  135. *
  136. * LOG(level, fmt, ...) is same as LOG_() with LOC_INFO prefix.
  137. */
  138. #ifdef NO_LOG
  139. # ifdef __SUNPRO_C
  140. # define LOG_(facility, level, prefix, fmt, ...)
  141. # define LOG(level, fmt, ...)
  142. # define LOG_FC(facility, level, fmt, ...)
  143. # else
  144. # define LOG_(facility, level, prefix, fmt, args...)
  145. # define LOG(level, fmt, args...)
  146. # define LOG_FC(facility, level, fmt, args...)
  147. # endif
  148. #else
  149. # ifdef NO_SIG_DEBUG
  150. # define DPRINT_NON_CRIT (1)
  151. # define DPRINT_CRIT_ENTER
  152. # define DPRINT_CRIT_EXIT
  153. # else
  154. # define DPRINT_NON_CRIT (dprint_crit==0)
  155. # define DPRINT_CRIT_ENTER (dprint_crit++)
  156. # define DPRINT_CRIT_EXIT (dprint_crit--)
  157. # endif
  158. # ifdef __SUNPRO_C
  159. # define LOG_(facility, level, prefix, fmt, ...) \
  160. do { \
  161. if (unlikely(get_debug_level(LOG_MNAME, LOG_MNAME_LEN) >= (level) && \
  162. DPRINT_NON_CRIT)) { \
  163. DPRINT_CRIT_ENTER; \
  164. if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
  165. if (unlikely(log_stderr)) { \
  166. if (unlikely(log_color)) dprint_color(level); \
  167. fprintf(stderr, "%2d(%d) %s: %s" fmt, \
  168. process_no, my_pid(), \
  169. LOG_LEVEL2NAME(level), (prefix), \
  170. __VA_ARGS__); \
  171. if (unlikely(log_color)) dprint_color_reset(); \
  172. } else { \
  173. syslog(LOG2SYSLOG_LEVEL(level) | \
  174. (((facility) != DEFAULT_FACILITY) ? \
  175. (facility) : \
  176. cfg_get(core, core_cfg, log_facility)), \
  177. "%s: %s" fmt, LOG_LEVEL2NAME(level),\
  178. (prefix), __VA_ARGS__); \
  179. } \
  180. } else { \
  181. if (log_stderr) { \
  182. if (unlikely(log_color)) dprint_color(level); \
  183. fprintf(stderr, "%2d(%d) %s" fmt, \
  184. process_no, my_pid(), \
  185. (prefix), __VA_ARGS__); \
  186. if (unlikely(log_color)) dprint_color_reset(); \
  187. } else { \
  188. if ((level)<L_ALERT) \
  189. syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
  190. (((facility) != DEFAULT_FACILITY) ? \
  191. (facility) : \
  192. cfg_get(core, core_cfg, log_facility)),\
  193. "%s" fmt, (prefix), __VA_ARGS__); \
  194. else \
  195. syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
  196. (((facility) != DEFAULT_FACILITY) ? \
  197. (facility) : \
  198. cfg_get(core, core_cfg, log_facility)),\
  199. "%s" fmt, (prefix), __VA_ARGS__); \
  200. } \
  201. } \
  202. DPRINT_CRIT_EXIT; \
  203. } \
  204. } while(0)
  205. # ifdef LOG_FUNC_NAME
  206. # define LOG(level, fmt, ...) \
  207. LOG_(DEFAULT_FACILITY, (level), LOC_INFO, "%s(): " fmt,\
  208. _FUNC_NAME_, __VA_ARGS__)
  209. # define LOG_FC(facility, level, fmt, ...) \
  210. LOG_((facility), (level), LOC_INFO, "%s(): " fmt,\
  211. _FUNC_NAME_, __VA_ARGS__)
  212. # else /* LOG_FUNC_NAME */
  213. # define LOG(level, fmt, ...) \
  214. LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt, __VA_ARGS__)
  215. # define LOG_FC(facility, level, fmt, ...) \
  216. LOG_((facility), (level), LOC_INFO, fmt, __VA_ARGS__)
  217. # endif /* LOG_FUNC_NAME */
  218. # else /* ! __SUNPRO_C */
  219. # define LOG_(facility, level, prefix, fmt, args...) \
  220. do { \
  221. if (get_debug_level(LOG_MNAME, LOG_MNAME_LEN) >= (level) && \
  222. DPRINT_NON_CRIT) { \
  223. DPRINT_CRIT_ENTER; \
  224. if (likely(((level) >= L_ALERT) && ((level) <= L_DBG))){ \
  225. if (unlikely(log_stderr)) { \
  226. if (unlikely(log_color)) dprint_color(level); \
  227. fprintf(stderr, "%2d(%d) %s: %s" fmt, \
  228. process_no, my_pid(), \
  229. LOG_LEVEL2NAME(level), \
  230. (prefix) , ## args);\
  231. if (unlikely(log_color)) dprint_color_reset(); \
  232. } else { \
  233. syslog(LOG2SYSLOG_LEVEL(level) |\
  234. (((facility) != DEFAULT_FACILITY) ? \
  235. (facility) : \
  236. cfg_get(core, core_cfg, log_facility)), \
  237. "%s: %s" fmt, LOG_LEVEL2NAME(level),\
  238. (prefix) , ## args); \
  239. } \
  240. } else { \
  241. if (log_stderr) { \
  242. if (unlikely(log_color)) dprint_color(level); \
  243. fprintf(stderr, "%2d(%d) %s" fmt, \
  244. process_no, my_pid(), \
  245. (prefix) , ## args); \
  246. if (unlikely(log_color)) dprint_color_reset(); \
  247. } else { \
  248. if ((level)<L_ALERT) \
  249. syslog(LOG2SYSLOG_LEVEL(L_ALERT) | \
  250. (((facility) != DEFAULT_FACILITY) ? \
  251. (facility) : \
  252. cfg_get(core, core_cfg, log_facility)),\
  253. "%s" fmt, (prefix) , ## args); \
  254. else \
  255. syslog(LOG2SYSLOG_LEVEL(L_DBG) | \
  256. (((facility) != DEFAULT_FACILITY) ? \
  257. (facility) : \
  258. cfg_get(core, core_cfg, log_facility)),\
  259. "%s" fmt, (prefix) , ## args); \
  260. } \
  261. } \
  262. DPRINT_CRIT_EXIT; \
  263. } \
  264. } while(0)
  265. # ifdef LOG_FUNC_NAME
  266. # define LOG(level, fmt, args...) \
  267. LOG_(DEFAULT_FACILITY, (level), LOC_INFO, "%s(): " fmt ,\
  268. _FUNC_NAME_, ## args)
  269. # define LOG_FC(facility, level, fmt, args...) \
  270. LOG_((facility), (level), LOC_INFO, "%s(): " fmt , _FUNC_NAME_, ## args)
  271. # else /* LOG_FUNC_NAME */
  272. # define LOG(level, fmt, args...) \
  273. LOG_(DEFAULT_FACILITY, (level), LOC_INFO, fmt , ## args)
  274. # define LOG_FC(facility, level, fmt, args...) \
  275. LOG_((facility), (level), LOC_INFO, fmt , ## args)
  276. # endif /* LOG_FUNC_NAME */
  277. # endif /* __SUNPRO_C */
  278. #endif /* NO_LOG */
  279. /** @name SimpleLog
  280. * Simplier, prefered logging macros for constant log level
  281. */
  282. /*@ { */
  283. #ifdef __SUNPRO_C
  284. # define ALERT(...) LOG(L_ALERT, __VA_ARGS__)
  285. # define BUG(...) LOG(L_BUG, __VA_ARGS__)
  286. # define ERR(...) LOG(L_ERR, __VA_ARGS__)
  287. # define WARN(...) LOG(L_WARN, __VA_ARGS__)
  288. # define NOTICE(...) LOG(L_NOTICE, __VA_ARGS__)
  289. # define INFO(...) LOG(L_INFO, __VA_ARGS__)
  290. # define CRIT(...) LOG(L_CRIT2, __VA_ARGS__)
  291. # ifdef NO_DEBUG
  292. # define DBG(...)
  293. # else
  294. # define DBG(...) LOG(L_DBG, __VA_ARGS__)
  295. # endif
  296. /*@ } */
  297. /* obsolete, do not use */
  298. # define DEBUG(...) DBG(__VA_ARGS__)
  299. #else /* ! __SUNPRO_C */
  300. # define ALERT(fmt, args...) LOG(L_ALERT, fmt , ## args)
  301. # define BUG(fmt, args...) LOG(L_BUG, fmt , ## args)
  302. # define ERR(fmt, args...) LOG(L_ERR, fmt , ## args)
  303. # define WARN(fmt, args...) LOG(L_WARN, fmt , ## args)
  304. # define NOTICE(fmt, args...) LOG(L_NOTICE, fmt , ## args)
  305. # define INFO(fmt, args...) LOG(L_INFO, fmt , ## args)
  306. # define CRIT(fmt, args...) LOG(L_CRIT2, fmt , ## args)
  307. # ifdef NO_DEBUG
  308. # define DBG(fmt, args...)
  309. # else
  310. # define DBG(fmt, args...) LOG(L_DBG, fmt , ## args)
  311. # endif
  312. /* obsolete, do not use */
  313. # define DEBUG(fmt, args...) DBG(fmt , ## args)
  314. #endif /* __SUNPRO_C */
  315. /* kamailio/openser compatibility */
  316. #define LM_GEN1 LOG
  317. #define LM_GEN2 LOG_FC
  318. #define LM_ALERT ALERT
  319. #define LM_CRIT CRIT
  320. #define LM_ERR ERR
  321. #define LM_WARN WARN
  322. #define LM_NOTICE NOTICE
  323. #define LM_INFO INFO
  324. #define LM_DBG DEBUG
  325. #endif /* !dprint_h */