dprint.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <stdarg.h>
  39. #include <stdio.h>
  40. #include <strings.h>
  41. #ifndef NO_SIG_DEBUG
  42. /* signal protection: !=0 when LOG/DBG/... are printing */
  43. volatile int dprint_crit = 0;
  44. #endif
  45. static char* str_fac[]={"LOG_AUTH","LOG_CRON","LOG_DAEMON",
  46. "LOG_KERN","LOG_LOCAL0","LOG_LOCAL1",
  47. "LOG_LOCAL2","LOG_LOCAL3","LOG_LOCAL4","LOG_LOCAL5",
  48. "LOG_LOCAL6","LOG_LOCAL7","LOG_LPR","LOG_MAIL",
  49. "LOG_NEWS","LOG_USER","LOG_UUCP",
  50. #ifndef __OS_solaris
  51. "LOG_AUTHPRIV","LOG_FTP","LOG_SYSLOG",
  52. #endif
  53. 0};
  54. static int int_fac[]={LOG_AUTH , LOG_CRON , LOG_DAEMON ,
  55. LOG_KERN , LOG_LOCAL0 , LOG_LOCAL1 ,
  56. LOG_LOCAL2 , LOG_LOCAL3 , LOG_LOCAL4 , LOG_LOCAL5 ,
  57. LOG_LOCAL6 , LOG_LOCAL7 , LOG_LPR , LOG_MAIL ,
  58. LOG_NEWS , LOG_USER , LOG_UUCP,
  59. #ifndef __OS_solaris
  60. LOG_AUTHPRIV,LOG_FTP,LOG_SYSLOG,
  61. #endif
  62. 0};
  63. struct log_level_info log_level_info[] = {
  64. {"ALERT", LOG_ALERT}, /* L_ALERT */
  65. {"BUG", LOG_CRIT}, /* L_BUG */
  66. {"CRITICAL", LOG_CRIT}, /* L_CRIT2 */
  67. {"", LOG_CRIT}, /* L_CRIT */
  68. {"ERROR", LOG_ERR}, /* L_ERR */
  69. {"WARNING", LOG_WARNING}, /* L_WARN */
  70. {"NOTICE", LOG_NOTICE}, /* L_NOTICE */
  71. {"INFO", LOG_INFO}, /* L_INFO */
  72. {"DEBUG", LOG_DEBUG} /* L_DBG */
  73. };
  74. int str2facility(char *s)
  75. {
  76. int i;
  77. for( i=0; str_fac[i] ; i++) {
  78. if (!strcasecmp(s,str_fac[i]))
  79. return int_fac[i];
  80. }
  81. return -1;
  82. }
  83. /* fixup function for log_facility cfg parameter */
  84. int log_facility_fixup(void *handle, str *gname, str *name, void **val)
  85. {
  86. int i;
  87. if ((i = str2facility((char *)*val)) == -1) {
  88. LOG(L_ERR, "log_facility_fixup: invalid log facility: %s\n",
  89. (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. /**
  103. * @brief return the log level - the local one if it set,
  104. * otherwise the global value
  105. */
  106. int get_debug_level(void) {
  107. return (_local_debug_level != UNSET_LOCAL_DEBUG_LEVEL) ?
  108. _local_debug_level : cfg_get(core, core_cfg, debug);
  109. }
  110. /**
  111. * @brief set the local debug log level
  112. */
  113. void set_local_debug_level(int level)
  114. {
  115. _local_debug_level = level;
  116. }
  117. /**
  118. * @brief reset the local debug log level
  119. */
  120. void reset_local_debug_level(void)
  121. {
  122. _local_debug_level = UNSET_LOCAL_DEBUG_LEVEL;
  123. }
  124. typedef struct log_level_color {
  125. char f;
  126. char b;
  127. } log_level_color_t;
  128. log_level_color_t _log_level_colors[L_MAX - L_MIN + 1];
  129. void dprint_init_colors(void)
  130. {
  131. int i;
  132. i = 0;
  133. memset(_log_level_colors, 0,
  134. (L_MAX - L_MIN + 1)*sizeof(log_level_color_t));
  135. /* L_ALERT */
  136. _log_level_colors[i].f = 'R'; /* default */
  137. _log_level_colors[i].b = 'x'; /* default */
  138. i++;
  139. /* L_BUG */
  140. _log_level_colors[i].f = 'P'; /* default */
  141. _log_level_colors[i].b = 'x'; /* default */
  142. i++;
  143. /* L_CRIT2 */
  144. _log_level_colors[i].f = 'y'; /* default */
  145. _log_level_colors[i].b = 'x'; /* default */
  146. i++;
  147. /* L_CRIT */
  148. _log_level_colors[i].f = 'b'; /* default */
  149. _log_level_colors[i].b = 'x'; /* default */
  150. i++;
  151. /* L_ERR */
  152. _log_level_colors[i].f = 'r'; /* default */
  153. _log_level_colors[i].b = 'x'; /* default */
  154. i++;
  155. /* L_WARN */
  156. _log_level_colors[i].f = 'p'; /* default */
  157. _log_level_colors[i].b = 'x'; /* default */
  158. i++;
  159. /* L_NOTICE */
  160. _log_level_colors[i].f = 'g'; /* default */
  161. _log_level_colors[i].b = 'x'; /* default */
  162. i++;
  163. /* L_INFO */
  164. _log_level_colors[i].f = 'c'; /* default */
  165. _log_level_colors[i].b = 'x'; /* default */
  166. i++;
  167. /* L_DBG */
  168. _log_level_colors[i].f = 'x'; /* default */
  169. _log_level_colors[i].b = 'x'; /* default */
  170. i++;
  171. }
  172. #define TERM_COLOR_SIZE 16
  173. #define dprint_termc_add(p, end, s) \
  174. do{ \
  175. if ((p)+(sizeof(s)-1)<=(end)){ \
  176. memcpy((p), s, sizeof(s)-1); \
  177. (p)+=sizeof(s)-1; \
  178. }else{ \
  179. /* overflow */ \
  180. LM_ERR("dprint_termc_add overflow\n"); \
  181. goto error; \
  182. } \
  183. } while(0)
  184. void dprint_term_color(char f, char b, str *obuf)
  185. {
  186. static char term_color[TERM_COLOR_SIZE];
  187. char* p;
  188. char* end;
  189. p = term_color;
  190. end = p + TERM_COLOR_SIZE;
  191. /* excape sequence */
  192. dprint_termc_add(p, end, "\033[");
  193. if(f!='_')
  194. {
  195. if (islower((int)f))
  196. {
  197. /* normal font */
  198. dprint_termc_add(p, end, "0;");
  199. } else {
  200. /* bold font */
  201. dprint_termc_add(p, end, "1;");
  202. f += 32;
  203. }
  204. }
  205. /* foreground */
  206. switch(f)
  207. {
  208. case 'x':
  209. dprint_termc_add(p, end, "39;");
  210. break;
  211. case 's':
  212. dprint_termc_add(p, end, "30;");
  213. break;
  214. case 'r':
  215. dprint_termc_add(p, end, "31;");
  216. break;
  217. case 'g':
  218. dprint_termc_add(p, end, "32;");
  219. break;
  220. case 'y':
  221. dprint_termc_add(p, end, "33;");
  222. break;
  223. case 'b':
  224. dprint_termc_add(p, end, "34;");
  225. break;
  226. case 'p':
  227. dprint_termc_add(p, end, "35;");
  228. break;
  229. case 'c':
  230. dprint_termc_add(p, end, "36;");
  231. break;
  232. case 'w':
  233. dprint_termc_add(p, end, "37;");
  234. break;
  235. default:
  236. dprint_termc_add(p, end, "39;");
  237. }
  238. /* background */
  239. switch(b)
  240. {
  241. case 'x':
  242. dprint_termc_add(p, end, "49");
  243. break;
  244. case 's':
  245. dprint_termc_add(p, end, "40");
  246. break;
  247. case 'r':
  248. dprint_termc_add(p, end, "41");
  249. break;
  250. case 'g':
  251. dprint_termc_add(p, end, "42");
  252. break;
  253. case 'y':
  254. dprint_termc_add(p, end, "43");
  255. break;
  256. case 'b':
  257. dprint_termc_add(p, end, "44");
  258. break;
  259. case 'p':
  260. dprint_termc_add(p, end, "45");
  261. break;
  262. case 'c':
  263. dprint_termc_add(p, end, "46");
  264. break;
  265. case 'w':
  266. dprint_termc_add(p, end, "47");
  267. break;
  268. default:
  269. dprint_termc_add(p, end, "49");
  270. }
  271. /* end */
  272. dprint_termc_add(p, end, "m");
  273. obuf->s = term_color;
  274. obuf->len = p - term_color;
  275. return;
  276. error:
  277. obuf->s = term_color;
  278. term_color[0] = '\0';
  279. obuf->len = 0;
  280. }
  281. void dprint_color(int level)
  282. {
  283. str obuf;
  284. if(level<L_MIN || level>L_MAX)
  285. return;
  286. dprint_term_color(_log_level_colors[level - L_MIN].f,
  287. _log_level_colors[level - L_MIN].b,
  288. &obuf);
  289. fprintf(stderr, "%.*s", obuf.len, obuf.s);
  290. }
  291. void dprint_color_reset(void)
  292. {
  293. str obuf;
  294. dprint_term_color('x', 'x', &obuf);
  295. fprintf(stderr, "%.*s", obuf.len, obuf.s);
  296. }
  297. void dprint_color_update(int level, char f, char b)
  298. {
  299. if(level<L_MIN || level>L_MAX)
  300. return;
  301. if(f && f!='0') _log_level_colors[level - L_MIN].f = f;
  302. if(b && b!='0') _log_level_colors[level - L_MIN].b = b;
  303. }