dprint.h 12 KB

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