dprint.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * $Id$
  3. *
  4. * debug print
  5. *
  6. *
  7. * Copyright (C) 2001-2003 FhG Fokus
  8. *
  9. * This file is part of ser, a free SIP server.
  10. *
  11. * ser is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * For a license to use the ser software under conditions
  17. * other than those described here, or to purchase support for this
  18. * software, please contact iptel.org by e-mail at the following addresses:
  19. * [email protected]
  20. *
  21. * ser is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. /*!
  31. * \file
  32. * \brief SIP-router core ::
  33. * \ingroup core
  34. * Module: \ref core
  35. */
  36. #include "globals.h"
  37. #include "dprint.h"
  38. #include "pvar.h"
  39. #include <stdarg.h>
  40. #include <stdio.h>
  41. #include <strings.h>
  42. #ifndef NO_SIG_DEBUG
  43. /* signal protection: !=0 when LOG/DBG/... are printing */
  44. volatile int dprint_crit = 0;
  45. #endif
  46. static char* str_fac[]={"LOG_AUTH","LOG_CRON","LOG_DAEMON",
  47. "LOG_KERN","LOG_LOCAL0","LOG_LOCAL1",
  48. "LOG_LOCAL2","LOG_LOCAL3","LOG_LOCAL4","LOG_LOCAL5",
  49. "LOG_LOCAL6","LOG_LOCAL7","LOG_LPR","LOG_MAIL",
  50. "LOG_NEWS","LOG_USER","LOG_UUCP",
  51. #ifndef __OS_solaris
  52. "LOG_AUTHPRIV","LOG_FTP","LOG_SYSLOG",
  53. #endif
  54. 0};
  55. static int int_fac[]={LOG_AUTH , LOG_CRON , LOG_DAEMON ,
  56. LOG_KERN , LOG_LOCAL0 , LOG_LOCAL1 ,
  57. LOG_LOCAL2 , LOG_LOCAL3 , LOG_LOCAL4 , LOG_LOCAL5 ,
  58. LOG_LOCAL6 , LOG_LOCAL7 , LOG_LPR , LOG_MAIL ,
  59. LOG_NEWS , LOG_USER , LOG_UUCP,
  60. #ifndef __OS_solaris
  61. LOG_AUTHPRIV,LOG_FTP,LOG_SYSLOG,
  62. #endif
  63. 0};
  64. struct log_level_info log_level_info[] = {
  65. {"ALERT", LOG_ALERT}, /* L_ALERT */
  66. {"BUG", LOG_CRIT}, /* L_BUG */
  67. {"CRITICAL", LOG_CRIT}, /* L_CRIT2 */
  68. {"", LOG_CRIT}, /* L_CRIT */
  69. {"ERROR", LOG_ERR}, /* L_ERR */
  70. {"WARNING", LOG_WARNING}, /* L_WARN */
  71. {"NOTICE", LOG_NOTICE}, /* L_NOTICE */
  72. {"INFO", LOG_INFO}, /* L_INFO */
  73. {"DEBUG", LOG_DEBUG} /* L_DBG */
  74. };
  75. int str2facility(char *s)
  76. {
  77. int i;
  78. for( i=0; str_fac[i] ; i++) {
  79. if (!strcasecmp(s,str_fac[i]))
  80. return int_fac[i];
  81. }
  82. return -1;
  83. }
  84. /* fixup function for log_facility cfg parameter */
  85. int log_facility_fixup(void *handle, str *gname, str *name, void **val)
  86. {
  87. int i;
  88. if ((i = str2facility((char *)*val)) == -1) {
  89. LM_ERR("invalid log facility: %s\n", (char *)*val);
  90. return -1;
  91. }
  92. *val = (void *)(long)i;
  93. return 0;
  94. }
  95. /**
  96. * per process debug log level (local)
  97. */
  98. /* value for unset local log level */
  99. #define UNSET_LOCAL_DEBUG_LEVEL -255
  100. /* the local debug log level */
  101. static int _local_debug_level = UNSET_LOCAL_DEBUG_LEVEL;
  102. /* callback to get per module debug level */
  103. static get_module_debug_level_f _module_debug_level = NULL;
  104. /**
  105. * @brief set callback function for per module debug level
  106. */
  107. void set_module_debug_level_cb(get_module_debug_level_f f)
  108. {
  109. _module_debug_level = f;
  110. }
  111. /**
  112. * @brief return the log level - the local one if it set,
  113. * otherwise the global value
  114. */
  115. int get_debug_level(char *mname, int mnlen) {
  116. int mlevel = L_DBG;
  117. /*important -- no LOGs inside, because it will loop */
  118. if(unlikely(_module_debug_level!=NULL && mnlen>0)) {
  119. if(_module_debug_level(mname, mnlen, &mlevel)==0) {
  120. return mlevel;
  121. }
  122. }
  123. return (_local_debug_level != UNSET_LOCAL_DEBUG_LEVEL) ?
  124. _local_debug_level : cfg_get(core, core_cfg, debug);
  125. }
  126. /**
  127. * @brief set the local debug log level
  128. */
  129. void set_local_debug_level(int level)
  130. {
  131. _local_debug_level = level;
  132. }
  133. /**
  134. * @brief reset the local debug log level
  135. */
  136. void reset_local_debug_level(void)
  137. {
  138. _local_debug_level = UNSET_LOCAL_DEBUG_LEVEL;
  139. }
  140. typedef struct log_level_color {
  141. char f;
  142. char b;
  143. } log_level_color_t;
  144. log_level_color_t _log_level_colors[L_MAX - L_MIN + 1];
  145. void dprint_init_colors(void)
  146. {
  147. int i;
  148. i = 0;
  149. memset(_log_level_colors, 0,
  150. (L_MAX - L_MIN + 1)*sizeof(log_level_color_t));
  151. /* L_ALERT */
  152. _log_level_colors[i].f = 'R'; /* default */
  153. _log_level_colors[i].b = 'x'; /* default */
  154. i++;
  155. /* L_BUG */
  156. _log_level_colors[i].f = 'P'; /* default */
  157. _log_level_colors[i].b = 'x'; /* default */
  158. i++;
  159. /* L_CRIT2 */
  160. _log_level_colors[i].f = 'y'; /* default */
  161. _log_level_colors[i].b = 'x'; /* default */
  162. i++;
  163. /* L_CRIT */
  164. _log_level_colors[i].f = 'b'; /* default */
  165. _log_level_colors[i].b = 'x'; /* default */
  166. i++;
  167. /* L_ERR */
  168. _log_level_colors[i].f = 'r'; /* default */
  169. _log_level_colors[i].b = 'x'; /* default */
  170. i++;
  171. /* L_WARN */
  172. _log_level_colors[i].f = 'p'; /* default */
  173. _log_level_colors[i].b = 'x'; /* default */
  174. i++;
  175. /* L_NOTICE */
  176. _log_level_colors[i].f = 'g'; /* default */
  177. _log_level_colors[i].b = 'x'; /* default */
  178. i++;
  179. /* L_INFO */
  180. _log_level_colors[i].f = 'c'; /* default */
  181. _log_level_colors[i].b = 'x'; /* default */
  182. i++;
  183. /* L_DBG */
  184. _log_level_colors[i].f = 'x'; /* default */
  185. _log_level_colors[i].b = 'x'; /* default */
  186. i++;
  187. }
  188. #define TERM_COLOR_SIZE 16
  189. #define dprint_termc_add(p, end, s) \
  190. do{ \
  191. if ((p)+(sizeof(s)-1)<=(end)){ \
  192. memcpy((p), s, sizeof(s)-1); \
  193. (p)+=sizeof(s)-1; \
  194. }else{ \
  195. /* overflow */ \
  196. LM_ERR("dprint_termc_add overflow\n"); \
  197. goto error; \
  198. } \
  199. } while(0)
  200. void dprint_term_color(char f, char b, str *obuf)
  201. {
  202. static char term_color[TERM_COLOR_SIZE];
  203. char* p;
  204. char* end;
  205. p = term_color;
  206. end = p + TERM_COLOR_SIZE;
  207. /* excape sequence */
  208. dprint_termc_add(p, end, "\033[");
  209. if(f!='_')
  210. {
  211. if (islower((int)f))
  212. {
  213. /* normal font */
  214. dprint_termc_add(p, end, "0;");
  215. } else {
  216. /* bold font */
  217. dprint_termc_add(p, end, "1;");
  218. f += 32;
  219. }
  220. }
  221. /* foreground */
  222. switch(f)
  223. {
  224. case 'x':
  225. dprint_termc_add(p, end, "39;");
  226. break;
  227. case 's':
  228. dprint_termc_add(p, end, "30;");
  229. break;
  230. case 'r':
  231. dprint_termc_add(p, end, "31;");
  232. break;
  233. case 'g':
  234. dprint_termc_add(p, end, "32;");
  235. break;
  236. case 'y':
  237. dprint_termc_add(p, end, "33;");
  238. break;
  239. case 'b':
  240. dprint_termc_add(p, end, "34;");
  241. break;
  242. case 'p':
  243. dprint_termc_add(p, end, "35;");
  244. break;
  245. case 'c':
  246. dprint_termc_add(p, end, "36;");
  247. break;
  248. case 'w':
  249. dprint_termc_add(p, end, "37;");
  250. break;
  251. default:
  252. dprint_termc_add(p, end, "39;");
  253. }
  254. /* background */
  255. switch(b)
  256. {
  257. case 'x':
  258. dprint_termc_add(p, end, "49");
  259. break;
  260. case 's':
  261. dprint_termc_add(p, end, "40");
  262. break;
  263. case 'r':
  264. dprint_termc_add(p, end, "41");
  265. break;
  266. case 'g':
  267. dprint_termc_add(p, end, "42");
  268. break;
  269. case 'y':
  270. dprint_termc_add(p, end, "43");
  271. break;
  272. case 'b':
  273. dprint_termc_add(p, end, "44");
  274. break;
  275. case 'p':
  276. dprint_termc_add(p, end, "45");
  277. break;
  278. case 'c':
  279. dprint_termc_add(p, end, "46");
  280. break;
  281. case 'w':
  282. dprint_termc_add(p, end, "47");
  283. break;
  284. default:
  285. dprint_termc_add(p, end, "49");
  286. }
  287. /* end */
  288. dprint_termc_add(p, end, "m");
  289. obuf->s = term_color;
  290. obuf->len = p - term_color;
  291. return;
  292. error:
  293. obuf->s = term_color;
  294. term_color[0] = '\0';
  295. obuf->len = 0;
  296. }
  297. void dprint_color(int level)
  298. {
  299. str obuf;
  300. if(level<L_MIN || level>L_MAX)
  301. return;
  302. dprint_term_color(_log_level_colors[level - L_MIN].f,
  303. _log_level_colors[level - L_MIN].b,
  304. &obuf);
  305. fprintf(stderr, "%.*s", obuf.len, obuf.s);
  306. }
  307. void dprint_color_reset(void)
  308. {
  309. str obuf;
  310. dprint_term_color('x', 'x', &obuf);
  311. fprintf(stderr, "%.*s", obuf.len, obuf.s);
  312. }
  313. void dprint_color_update(int level, char f, char b)
  314. {
  315. if(level<L_MIN || level>L_MAX)
  316. return;
  317. if(f && f!='0') _log_level_colors[level - L_MIN].f = f;
  318. if(b && b!='0') _log_level_colors[level - L_MIN].b = b;
  319. }
  320. /* log_prefix functionality */
  321. str *log_prefix_val = NULL;
  322. static pv_elem_t *log_prefix_pvs = NULL;
  323. #define LOG_PREFIX_SIZE 128
  324. static char log_prefix_buf[LOG_PREFIX_SIZE];
  325. static str log_prefix_str;
  326. void log_prefix_init(void)
  327. {
  328. str s;
  329. if(log_prefix_fmt==NULL)
  330. return;
  331. s.s = log_prefix_fmt; s.len = strlen(s.s);
  332. if(pv_parse_format(&s, &log_prefix_pvs)<0)
  333. {
  334. LM_ERR("wrong format[%s]\n", s.s);
  335. return;
  336. }
  337. }
  338. void log_prefix_set(sip_msg_t *msg)
  339. {
  340. if(log_prefix_pvs == NULL)
  341. return;
  342. if(msg==NULL) {
  343. log_prefix_val = NULL;
  344. return;
  345. }
  346. log_prefix_str.s = log_prefix_buf;
  347. log_prefix_str.len = LOG_PREFIX_SIZE;
  348. if(pv_printf(msg, log_prefix_pvs, log_prefix_str.s, &log_prefix_str.len)<0)
  349. return;
  350. if(log_prefix_str.len<=0)
  351. return;
  352. log_prefix_val = &log_prefix_str;
  353. }