log.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2008, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /* $Id$ */
  7. const char log_c_id[] = "$Id$";
  8. /**
  9. * \file log.c
  10. * \brief Functions to send messages to log files or the console.
  11. **/
  12. #include "orconfig.h"
  13. #include <stdarg.h>
  14. #include <assert.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #ifdef HAVE_SYS_TIME_H
  19. #include <sys/time.h>
  20. #endif
  21. #ifdef HAVE_TIME_H
  22. #include <time.h>
  23. #endif
  24. #include "./util.h"
  25. #include "./log.h"
  26. #include <event.h>
  27. #define TRUNCATED_STR "[...truncated]"
  28. #define TRUNCATED_STR_LEN 14
  29. /** Information for a single logfile; only used in log.c */
  30. typedef struct logfile_t {
  31. struct logfile_t *next; /**< Next logfile_t in the linked list. */
  32. char *filename; /**< Filename to open. */
  33. FILE *file; /**< Stream to receive log messages. */
  34. int seems_dead; /**< Boolean: true if the stream seems to be kaput. */
  35. int needs_close; /**< Boolean: true if the stream gets closed on shutdown. */
  36. int min_loglevel; /**< Lowest severity level to send to this stream. */
  37. int max_loglevel; /**< Highest severity level to send to this stream. */
  38. int is_temporary; /**< Boolean: close after initializing logging subsystem.*/
  39. int is_syslog; /**< Boolean: send messages to syslog. */
  40. log_callback callback; /**< If not NULL, send messages to this function. */
  41. } logfile_t;
  42. /** Helper: map a log severity to descriptive string. */
  43. static INLINE const char *
  44. sev_to_string(int severity)
  45. {
  46. switch (severity) {
  47. case LOG_DEBUG: return "debug";
  48. case LOG_INFO: return "info";
  49. case LOG_NOTICE: return "notice";
  50. case LOG_WARN: return "warn";
  51. case LOG_ERR: return "err";
  52. default: assert(0); return "UNKNOWN";
  53. }
  54. }
  55. /** Helper: decide whether to include the function name in the log message. */
  56. static INLINE int
  57. should_log_function_name(uint32_t domain, int severity)
  58. {
  59. switch (severity) {
  60. case LOG_DEBUG:
  61. case LOG_INFO:
  62. /* All debugging messages occur in interesting places. */
  63. return 1;
  64. case LOG_NOTICE:
  65. case LOG_WARN:
  66. case LOG_ERR:
  67. /* We care about places where bugs occur. */
  68. return (domain == LD_BUG);
  69. default:
  70. assert(0); return 0;
  71. }
  72. }
  73. #define USE_LOG_MUTEX
  74. #ifdef USE_LOG_MUTEX
  75. /** A mutex to guard changes to logfiles and logging. */
  76. static tor_mutex_t *log_mutex = NULL;
  77. #endif
  78. /** Linked list of logfile_t. */
  79. static logfile_t *logfiles = NULL;
  80. #ifdef HAVE_SYSLOG_H
  81. static int syslog_count = 0;
  82. #endif
  83. #ifdef USE_LOG_MUTEX
  84. #define LOCK_LOGS() STMT_BEGIN \
  85. tor_mutex_acquire(log_mutex); \
  86. STMT_END
  87. #define UNLOCK_LOGS() STMT_BEGIN tor_mutex_release(log_mutex); STMT_END
  88. #else
  89. #define LOCK_LOGS() STMT_NIL
  90. #define UNLOCK_LOGS() STMT_NIL
  91. #endif
  92. /* What's the lowest log level anybody cares about? */
  93. int _log_global_min_severity = LOG_NOTICE;
  94. static void delete_log(logfile_t *victim);
  95. static void close_log(logfile_t *victim);
  96. /** Name of the application: used to generate the message we write at the
  97. * start of each new log. */
  98. static char *appname = NULL;
  99. /** Set the "application name" for the logs to <b>name</b>: we'll use this
  100. * name in the message we write when starting up, and at the start of each new
  101. * log.
  102. *
  103. * Tor uses this string to write the version number to the log file. */
  104. void
  105. log_set_application_name(const char *name)
  106. {
  107. tor_free(appname);
  108. appname = name ? tor_strdup(name) : NULL;
  109. }
  110. /** Helper: Write the standard prefix for log lines to a
  111. * <b>buf_len</b> character buffer in <b>buf</b>.
  112. */
  113. static INLINE size_t
  114. _log_prefix(char *buf, size_t buf_len, int severity)
  115. {
  116. time_t t;
  117. struct timeval now;
  118. struct tm tm;
  119. size_t n;
  120. int r;
  121. tor_gettimeofday(&now);
  122. t = (time_t)now.tv_sec;
  123. n = strftime(buf, buf_len, "%b %d %H:%M:%S", tor_localtime_r(&t, &tm));
  124. r = tor_snprintf(buf+n, buf_len-n, ".%.3ld [%s] ",
  125. (long)now.tv_usec / 1000, sev_to_string(severity));
  126. if (r<0)
  127. return buf_len-1;
  128. else
  129. return n+r;
  130. }
  131. /** If lf refers to an actual file that we have just opened, and the file
  132. * contains no data, log an "opening new logfile" message at the top.
  133. *
  134. * Return -1 if the log is broken and needs to be deleted, else return 0.
  135. */
  136. static int
  137. log_tor_version(logfile_t *lf, int reset)
  138. {
  139. char buf[256];
  140. size_t n;
  141. int is_new;
  142. if (!lf->needs_close)
  143. /* If it doesn't get closed, it isn't really a file. */
  144. return 0;
  145. if (lf->is_temporary)
  146. /* If it's temporary, it isn't really a file. */
  147. return 0;
  148. #ifdef HAVE_FTELLO
  149. is_new = (ftello(lf->file) == 0);
  150. #else
  151. is_new = (ftell(lf->file) == 0);
  152. #endif
  153. if (reset && !is_new)
  154. /* We are resetting, but we aren't at the start of the file; no
  155. * need to log again. */
  156. return 0;
  157. n = _log_prefix(buf, sizeof(buf), LOG_NOTICE);
  158. if (appname) {
  159. tor_snprintf(buf+n, sizeof(buf)-n,
  160. "%s opening %slog file.\n", appname, is_new?"new ":"");
  161. } else {
  162. tor_snprintf(buf+n, sizeof(buf)-n,
  163. "Tor %s opening %slog file.\n", VERSION, is_new?"new ":"");
  164. }
  165. if (fputs(buf, lf->file) == EOF ||
  166. fflush(lf->file) == EOF) /* error */
  167. return -1; /* failed */
  168. return 0;
  169. }
  170. /** Helper: Format a log message into a fixed-sized buffer. (This is
  171. * factored out of <b>logv</b> so that we never format a message more
  172. * than once.) Return a pointer to the first character of the message
  173. * portion of the formatted string.
  174. */
  175. static INLINE char *
  176. format_msg(char *buf, size_t buf_len,
  177. uint32_t domain, int severity, const char *funcname,
  178. const char *format, va_list ap)
  179. {
  180. size_t n;
  181. int r;
  182. char *end_of_prefix;
  183. tor_assert(buf_len >= 2); /* prevent integer underflow */
  184. buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
  185. n = _log_prefix(buf, buf_len, severity);
  186. end_of_prefix = buf+n;
  187. if (funcname && should_log_function_name(domain, severity)) {
  188. r = tor_snprintf(buf+n, buf_len-n, "%s(): ", funcname);
  189. if (r<0)
  190. n = strlen(buf);
  191. else
  192. n += r;
  193. }
  194. if (domain == LD_BUG && buf_len-n > 6) {
  195. memcpy(buf+n, "Bug: ", 6);
  196. n += 5;
  197. }
  198. r = tor_vsnprintf(buf+n,buf_len-n,format,ap);
  199. if (r < 0) {
  200. /* The message was too long; overwrite the end of the buffer with
  201. * "[...truncated]" */
  202. if (buf_len >= TRUNCATED_STR_LEN) {
  203. int offset = buf_len-TRUNCATED_STR_LEN;
  204. /* We have an extra 2 characters after buf_len to hold the \n\0,
  205. * so it's safe to add 1 to the size here. */
  206. strlcpy(buf+offset, TRUNCATED_STR, buf_len-offset+1);
  207. }
  208. /* Set 'n' to the end of the buffer, where we'll be writing \n\0.
  209. * Since we already subtracted 2 from buf_len, this is safe.*/
  210. n = buf_len;
  211. } else {
  212. n += r;
  213. }
  214. buf[n]='\n';
  215. buf[n+1]='\0';
  216. return end_of_prefix;
  217. }
  218. /** Helper: sends a message to the appropriate logfiles, at loglevel
  219. * <b>severity</b>. If provided, <b>funcname</b> is prepended to the
  220. * message. The actual message is derived as from tor_snprintf(format,ap).
  221. */
  222. static void
  223. logv(int severity, uint32_t domain, const char *funcname, const char *format,
  224. va_list ap)
  225. {
  226. char buf[10024];
  227. int formatted = 0;
  228. logfile_t *lf;
  229. char *end_of_prefix=NULL;
  230. assert(format);
  231. LOCK_LOGS();
  232. lf = logfiles;
  233. while (lf) {
  234. if (severity > lf->min_loglevel || severity < lf->max_loglevel) {
  235. lf = lf->next;
  236. continue;
  237. }
  238. if (! (lf->file || lf->is_syslog || lf->callback)) {
  239. lf = lf->next;
  240. continue;
  241. }
  242. if (lf->seems_dead) {
  243. lf = lf->next;
  244. continue;
  245. }
  246. if (!formatted) {
  247. end_of_prefix =
  248. format_msg(buf, sizeof(buf), domain, severity, funcname, format, ap);
  249. formatted = 1;
  250. }
  251. if (lf->is_syslog) {
  252. #ifdef HAVE_SYSLOG_H
  253. /* XXXX Some syslog implementations have scary limits on the length of
  254. * what you can pass them. Can/should we detect this? */
  255. syslog(severity, "%s", end_of_prefix);
  256. #endif
  257. lf = lf->next;
  258. continue;
  259. } else if (lf->callback) {
  260. lf->callback(severity, domain, end_of_prefix);
  261. lf = lf->next;
  262. continue;
  263. }
  264. if (fputs(buf, lf->file) == EOF ||
  265. fflush(lf->file) == EOF) { /* error */
  266. /* don't log the error! mark this log entry to be blown away, and
  267. * continue. */
  268. lf->seems_dead = 1;
  269. }
  270. lf = lf->next;
  271. }
  272. UNLOCK_LOGS();
  273. }
  274. /** Output a message to the log. */
  275. void
  276. _log(int severity, uint32_t domain, const char *format, ...)
  277. {
  278. va_list ap;
  279. if (severity > _log_global_min_severity)
  280. return;
  281. va_start(ap,format);
  282. logv(severity, domain, NULL, format, ap);
  283. va_end(ap);
  284. }
  285. /** Output a message to the log, prefixed with a function name <b>fn</b>. */
  286. #ifdef __GNUC__
  287. void
  288. _log_fn(int severity, uint32_t domain, const char *fn, const char *format, ...)
  289. {
  290. va_list ap;
  291. if (severity > _log_global_min_severity)
  292. return;
  293. va_start(ap,format);
  294. logv(severity, domain, fn, format, ap);
  295. va_end(ap);
  296. }
  297. #else
  298. const char *_log_fn_function_name=NULL;
  299. void
  300. _log_fn(int severity, uint32_t domain, const char *format, ...)
  301. {
  302. va_list ap;
  303. if (severity > _log_global_min_severity)
  304. return;
  305. va_start(ap,format);
  306. logv(severity, domain, _log_fn_function_name, format, ap);
  307. va_end(ap);
  308. _log_fn_function_name = NULL;
  309. }
  310. void
  311. _log_debug(uint32_t domain, const char *format, ...)
  312. {
  313. va_list ap;
  314. /* For GCC we do this check in the macro. */
  315. if (PREDICT_LIKELY(LOG_DEBUG > _log_global_min_severity))
  316. return;
  317. va_start(ap,format);
  318. logv(LOG_DEBUG, domain, _log_fn_function_name, format, ap);
  319. va_end(ap);
  320. _log_fn_function_name = NULL;
  321. }
  322. void
  323. _log_info(uint32_t domain, const char *format, ...)
  324. {
  325. va_list ap;
  326. if (LOG_INFO > _log_global_min_severity)
  327. return;
  328. va_start(ap,format);
  329. logv(LOG_INFO, domain, _log_fn_function_name, format, ap);
  330. va_end(ap);
  331. _log_fn_function_name = NULL;
  332. }
  333. void
  334. _log_notice(uint32_t domain, const char *format, ...)
  335. {
  336. va_list ap;
  337. if (LOG_NOTICE > _log_global_min_severity)
  338. return;
  339. va_start(ap,format);
  340. logv(LOG_NOTICE, domain, _log_fn_function_name, format, ap);
  341. va_end(ap);
  342. _log_fn_function_name = NULL;
  343. }
  344. void
  345. _log_warn(uint32_t domain, const char *format, ...)
  346. {
  347. va_list ap;
  348. if (LOG_WARN > _log_global_min_severity)
  349. return;
  350. va_start(ap,format);
  351. logv(LOG_WARN, domain, _log_fn_function_name, format, ap);
  352. va_end(ap);
  353. _log_fn_function_name = NULL;
  354. }
  355. void
  356. _log_err(uint32_t domain, const char *format, ...)
  357. {
  358. va_list ap;
  359. if (LOG_ERR > _log_global_min_severity)
  360. return;
  361. va_start(ap,format);
  362. logv(LOG_ERR, domain, _log_fn_function_name, format, ap);
  363. va_end(ap);
  364. _log_fn_function_name = NULL;
  365. }
  366. #endif
  367. /** Close all open log files, and free other static memory. */
  368. void
  369. logs_free_all(void)
  370. {
  371. logfile_t *victim, *next;
  372. LOCK_LOGS();
  373. next = logfiles;
  374. logfiles = NULL;
  375. UNLOCK_LOGS();
  376. while (next) {
  377. victim = next;
  378. next = next->next;
  379. close_log(victim);
  380. tor_free(victim->filename);
  381. tor_free(victim);
  382. }
  383. tor_free(appname);
  384. }
  385. /** Remove and free the log entry <b>victim</b> from the linked-list
  386. * logfiles (it is probably present, but it might not be due to thread
  387. * racing issues). After this function is called, the caller shouldn't
  388. * refer to <b>victim</b> anymore.
  389. *
  390. * Long-term, we need to do something about races in the log subsystem
  391. * in general. See bug 222 for more details.
  392. */
  393. static void
  394. delete_log(logfile_t *victim)
  395. {
  396. logfile_t *tmpl;
  397. if (victim == logfiles)
  398. logfiles = victim->next;
  399. else {
  400. for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
  401. // tor_assert(tmpl);
  402. // tor_assert(tmpl->next == victim);
  403. if (!tmpl)
  404. return;
  405. tmpl->next = victim->next;
  406. }
  407. tor_free(victim->filename);
  408. tor_free(victim);
  409. }
  410. /** Helper: release system resources (but not memory) held by a single
  411. * logfile_t. */
  412. static void
  413. close_log(logfile_t *victim)
  414. {
  415. if (victim->needs_close && victim->file) {
  416. fclose(victim->file);
  417. } else if (victim->is_syslog) {
  418. #ifdef HAVE_SYSLOG_H
  419. if (--syslog_count == 0) {
  420. /* There are no other syslogs; close the logging facility. */
  421. closelog();
  422. }
  423. #endif
  424. }
  425. }
  426. /** Add a log handler named <b>name</b> to send all messages of severity
  427. * between <b>loglevelMin</b> and <b>loglevelMax</b> (inclusive) to
  428. * <b>stream</b>. */
  429. static void
  430. add_stream_log_impl(int loglevelMin, int loglevelMax,
  431. const char *name, FILE *stream)
  432. {
  433. logfile_t *lf;
  434. lf = tor_malloc_zero(sizeof(logfile_t));
  435. lf->filename = tor_strdup(name);
  436. lf->min_loglevel = loglevelMin;
  437. lf->max_loglevel = loglevelMax;
  438. lf->file = stream;
  439. lf->next = logfiles;
  440. logfiles = lf;
  441. _log_global_min_severity = get_min_log_level();
  442. }
  443. /** Add a log handler to send all messages of severity <b>loglevel</b>
  444. * or higher to <b>stream</b>. */
  445. void
  446. add_stream_log(int loglevelMin, int loglevelMax,
  447. const char *name, FILE *stream)
  448. {
  449. LOCK_LOGS();
  450. add_stream_log_impl(loglevelMin, loglevelMax, name, stream);
  451. UNLOCK_LOGS();
  452. }
  453. /** Initialize the global logging facility */
  454. void
  455. init_logging(void)
  456. {
  457. if (!log_mutex)
  458. log_mutex = tor_mutex_new();
  459. }
  460. /** Add a log handler to receive messages during startup (before the real
  461. * logs are initialized).
  462. */
  463. void
  464. add_temp_log(void)
  465. {
  466. LOCK_LOGS();
  467. add_stream_log_impl(LOG_NOTICE, LOG_ERR, "<temp>", stdout);
  468. logfiles->is_temporary = 1;
  469. UNLOCK_LOGS();
  470. }
  471. /**
  472. * Add a log handler to send messages of severity between
  473. * <b>logLevelmin</b> and <b>logLevelMax</b> to the function
  474. * <b>cb</b>.
  475. */
  476. int
  477. add_callback_log(int loglevelMin, int loglevelMax, log_callback cb)
  478. {
  479. logfile_t *lf;
  480. lf = tor_malloc_zero(sizeof(logfile_t));
  481. lf->min_loglevel = loglevelMin;
  482. lf->max_loglevel = loglevelMax;
  483. lf->filename = tor_strdup("<callback>");
  484. lf->callback = cb;
  485. lf->next = logfiles;
  486. LOCK_LOGS();
  487. logfiles = lf;
  488. _log_global_min_severity = get_min_log_level();
  489. UNLOCK_LOGS();
  490. return 0;
  491. }
  492. /** Adjust the configured severity of any logs whose callback function is
  493. * <b>cb</b>. */
  494. void
  495. change_callback_log_severity(int loglevelMin, int loglevelMax,
  496. log_callback cb)
  497. {
  498. logfile_t *lf;
  499. LOCK_LOGS();
  500. for (lf = logfiles; lf; lf = lf->next) {
  501. if (lf->callback == cb) {
  502. lf->min_loglevel = loglevelMin;
  503. lf->max_loglevel = loglevelMax;
  504. }
  505. }
  506. _log_global_min_severity = get_min_log_level();
  507. UNLOCK_LOGS();
  508. }
  509. /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
  510. void
  511. close_temp_logs(void)
  512. {
  513. logfile_t *lf, **p;
  514. LOCK_LOGS();
  515. for (p = &logfiles; *p; ) {
  516. if ((*p)->is_temporary) {
  517. lf = *p;
  518. /* we use *p here to handle the edge case of the head of the list */
  519. *p = (*p)->next;
  520. close_log(lf);
  521. tor_free(lf->filename);
  522. tor_free(lf);
  523. } else {
  524. p = &((*p)->next);
  525. }
  526. }
  527. _log_global_min_severity = get_min_log_level();
  528. UNLOCK_LOGS();
  529. }
  530. /** Make all currently temporary logs (set to be closed by close_temp_logs)
  531. * live again, and close all non-temporary logs. */
  532. void
  533. rollback_log_changes(void)
  534. {
  535. logfile_t *lf;
  536. LOCK_LOGS();
  537. for (lf = logfiles; lf; lf = lf->next)
  538. lf->is_temporary = ! lf->is_temporary;
  539. UNLOCK_LOGS();
  540. close_temp_logs();
  541. }
  542. /** Configure all log handles to be closed by close_temp_logs */
  543. void
  544. mark_logs_temp(void)
  545. {
  546. logfile_t *lf;
  547. LOCK_LOGS();
  548. for (lf = logfiles; lf; lf = lf->next)
  549. lf->is_temporary = 1;
  550. UNLOCK_LOGS();
  551. }
  552. /**
  553. * Add a log handler to send messages to <b>filename</b>. If opening
  554. * the logfile fails, -1 is returned and errno is set appropriately
  555. * (by fopen).
  556. */
  557. int
  558. add_file_log(int loglevelMin, int loglevelMax, const char *filename)
  559. {
  560. FILE *f;
  561. logfile_t *lf;
  562. f = fopen(filename, "a");
  563. if (!f) return -1;
  564. LOCK_LOGS();
  565. add_stream_log_impl(loglevelMin, loglevelMax, filename, f);
  566. logfiles->needs_close = 1;
  567. lf = logfiles;
  568. _log_global_min_severity = get_min_log_level();
  569. UNLOCK_LOGS();
  570. if (log_tor_version(lf, 0) < 0) {
  571. LOCK_LOGS();
  572. delete_log(lf);
  573. UNLOCK_LOGS();
  574. }
  575. return 0;
  576. }
  577. #ifdef HAVE_SYSLOG_H
  578. /**
  579. * Add a log handler to send messages to they system log facility.
  580. */
  581. int
  582. add_syslog_log(int loglevelMin, int loglevelMax)
  583. {
  584. logfile_t *lf;
  585. if (syslog_count++ == 0)
  586. /* This is the first syslog. */
  587. openlog("Tor", LOG_PID | LOG_NDELAY, LOGFACILITY);
  588. lf = tor_malloc_zero(sizeof(logfile_t));
  589. lf->min_loglevel = loglevelMin;
  590. lf->filename = tor_strdup("<syslog>");
  591. lf->max_loglevel = loglevelMax;
  592. lf->is_syslog = 1;
  593. LOCK_LOGS();
  594. lf->next = logfiles;
  595. logfiles = lf;
  596. _log_global_min_severity = get_min_log_level();
  597. UNLOCK_LOGS();
  598. return 0;
  599. }
  600. #endif
  601. /** If <b>level</b> is a valid log severity, return the corresponding
  602. * numeric value. Otherwise, return -1. */
  603. int
  604. parse_log_level(const char *level)
  605. {
  606. if (!strcasecmp(level, "err"))
  607. return LOG_ERR;
  608. if (!strcasecmp(level, "warn"))
  609. return LOG_WARN;
  610. if (!strcasecmp(level, "notice"))
  611. return LOG_NOTICE;
  612. if (!strcasecmp(level, "info"))
  613. return LOG_INFO;
  614. if (!strcasecmp(level, "debug"))
  615. return LOG_DEBUG;
  616. return -1;
  617. }
  618. /** Return the string equivalent of a given log level. */
  619. const char *
  620. log_level_to_string(int level)
  621. {
  622. return sev_to_string(level);
  623. }
  624. /** Return the least severe log level that any current log is interested in. */
  625. int
  626. get_min_log_level(void)
  627. {
  628. logfile_t *lf;
  629. int min = LOG_ERR;
  630. for (lf = logfiles; lf; lf = lf->next) {
  631. if (lf->min_loglevel > min)
  632. min = lf->min_loglevel;
  633. }
  634. return min;
  635. }
  636. /** Switch all logs to output at most verbose level. */
  637. void
  638. switch_logs_debug(void)
  639. {
  640. logfile_t *lf;
  641. LOCK_LOGS();
  642. for (lf = logfiles; lf; lf=lf->next) {
  643. lf->min_loglevel = LOG_DEBUG;
  644. }
  645. UNLOCK_LOGS();
  646. }
  647. #ifdef HAVE_EVENT_SET_LOG_CALLBACK
  648. /** A string which, if it appears in a libevent log, should be ignored. */
  649. static const char *suppress_msg = NULL;
  650. /** Callback function passed to event_set_log() so we can intercept
  651. * log messages from libevent. */
  652. static void
  653. libevent_logging_callback(int severity, const char *msg)
  654. {
  655. char buf[1024];
  656. size_t n;
  657. if (suppress_msg && strstr(msg, suppress_msg))
  658. return;
  659. n = strlcpy(buf, msg, sizeof(buf));
  660. if (n && n < sizeof(buf) && buf[n-1] == '\n') {
  661. buf[n-1] = '\0';
  662. }
  663. switch (severity) {
  664. case _EVENT_LOG_DEBUG:
  665. log(LOG_DEBUG, LD_NET, "Message from libevent: %s", buf);
  666. break;
  667. case _EVENT_LOG_MSG:
  668. log(LOG_INFO, LD_NET, "Message from libevent: %s", buf);
  669. break;
  670. case _EVENT_LOG_WARN:
  671. log(LOG_WARN, LD_GENERAL, "Warning from libevent: %s", buf);
  672. break;
  673. case _EVENT_LOG_ERR:
  674. log(LOG_ERR, LD_GENERAL, "Error from libevent: %s", buf);
  675. break;
  676. default:
  677. log(LOG_WARN, LD_GENERAL, "Message [%d] from libevent: %s",
  678. severity, buf);
  679. break;
  680. }
  681. }
  682. /** Set hook to intercept log messages from libevent. */
  683. void
  684. configure_libevent_logging(void)
  685. {
  686. event_set_log_callback(libevent_logging_callback);
  687. }
  688. /** Ignore any libevent log message that contains <b>msg</b>. */
  689. void
  690. suppress_libevent_log_msg(const char *msg)
  691. {
  692. suppress_msg = msg;
  693. }
  694. #else
  695. void
  696. configure_libevent_logging(void)
  697. {
  698. }
  699. void
  700. suppress_libevent_log_msg(const char *msg)
  701. {
  702. (void)msg;
  703. }
  704. #endif
  705. #if 0
  706. static void
  707. dump_log_info(logfile_t *lf)
  708. {
  709. const char *tp;
  710. if (lf->filename) {
  711. printf("=== log into \"%s\" (%s-%s) (%stemporary)\n", lf->filename,
  712. sev_to_string(lf->min_loglevel),
  713. sev_to_string(lf->max_loglevel),
  714. lf->is_temporary?"":"not ");
  715. } else if (lf->is_syslog) {
  716. printf("=== syslog (%s-%s) (%stemporary)\n",
  717. sev_to_string(lf->min_loglevel),
  718. sev_to_string(lf->max_loglevel),
  719. lf->is_temporary?"":"not ");
  720. } else {
  721. printf("=== log (%s-%s) (%stemporary)\n",
  722. sev_to_string(lf->min_loglevel),
  723. sev_to_string(lf->max_loglevel),
  724. lf->is_temporary?"":"not ");
  725. }
  726. }
  727. void
  728. describe_logs(void)
  729. {
  730. logfile_t *lf;
  731. printf("==== BEGIN LOGS ====\n");
  732. for (lf = logfiles; lf; lf = lf->next)
  733. dump_log_info(lf);
  734. printf("==== END LOGS ====\n");
  735. }
  736. #endif