log.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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, 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 to send all messages of severity <b>loglevel</b>
  427. * or higher to <b>stream</b>. DOCDOC.*/
  428. static void
  429. add_stream_log_impl(int loglevelMin, int loglevelMax,
  430. const char *name, FILE *stream)
  431. {
  432. logfile_t *lf;
  433. lf = tor_malloc_zero(sizeof(logfile_t));
  434. lf->filename = tor_strdup(name);
  435. lf->min_loglevel = loglevelMin;
  436. lf->max_loglevel = loglevelMax;
  437. lf->file = stream;
  438. lf->next = logfiles;
  439. logfiles = lf;
  440. _log_global_min_severity = get_min_log_level();
  441. }
  442. /** Add a log handler to send all messages of severity <b>loglevel</b>
  443. * or higher to <b>stream</b>. */
  444. void
  445. add_stream_log(int loglevelMin, int loglevelMax,
  446. const char *name, FILE *stream)
  447. {
  448. LOCK_LOGS();
  449. add_stream_log_impl(loglevelMin, loglevelMax, name, stream);
  450. UNLOCK_LOGS();
  451. }
  452. /** Initialize the global logging facility */
  453. void
  454. init_logging(void)
  455. {
  456. if (!log_mutex)
  457. log_mutex = tor_mutex_new();
  458. }
  459. /** Add a log handler to receive messages during startup (before the real
  460. * logs are initialized).
  461. */
  462. void
  463. add_temp_log(void)
  464. {
  465. LOCK_LOGS();
  466. add_stream_log_impl(LOG_NOTICE, LOG_ERR, "<temp>", stdout);
  467. logfiles->is_temporary = 1;
  468. UNLOCK_LOGS();
  469. }
  470. /**
  471. * Add a log handler to send messages of severity between
  472. * <b>logLevelmin</b> and <b>logLevelMax</b> to the function
  473. * <b>cb</b>.
  474. */
  475. int
  476. add_callback_log(int loglevelMin, int loglevelMax, log_callback cb)
  477. {
  478. logfile_t *lf;
  479. lf = tor_malloc_zero(sizeof(logfile_t));
  480. lf->min_loglevel = loglevelMin;
  481. lf->max_loglevel = loglevelMax;
  482. lf->filename = tor_strdup("<callback>");
  483. lf->callback = cb;
  484. lf->next = logfiles;
  485. LOCK_LOGS();
  486. logfiles = lf;
  487. _log_global_min_severity = get_min_log_level();
  488. UNLOCK_LOGS();
  489. return 0;
  490. }
  491. /** Adjust the configured severity of any logs whose callback function is
  492. * <b>cb</b>. */
  493. void
  494. change_callback_log_severity(int loglevelMin, int loglevelMax,
  495. log_callback cb)
  496. {
  497. logfile_t *lf;
  498. LOCK_LOGS();
  499. for (lf = logfiles; lf; lf = lf->next) {
  500. if (lf->callback == cb) {
  501. lf->min_loglevel = loglevelMin;
  502. lf->max_loglevel = loglevelMax;
  503. }
  504. }
  505. _log_global_min_severity = get_min_log_level();
  506. UNLOCK_LOGS();
  507. }
  508. /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
  509. void
  510. close_temp_logs(void)
  511. {
  512. logfile_t *lf, **p;
  513. LOCK_LOGS();
  514. for (p = &logfiles; *p; ) {
  515. if ((*p)->is_temporary) {
  516. lf = *p;
  517. /* we use *p here to handle the edge case of the head of the list */
  518. *p = (*p)->next;
  519. close_log(lf);
  520. tor_free(lf->filename);
  521. tor_free(lf);
  522. } else {
  523. p = &((*p)->next);
  524. }
  525. }
  526. _log_global_min_severity = get_min_log_level();
  527. UNLOCK_LOGS();
  528. }
  529. /** Make all currently temporary logs (set to be closed by close_temp_logs)
  530. * live again, and close all non-temporary logs. */
  531. void
  532. rollback_log_changes(void)
  533. {
  534. logfile_t *lf;
  535. LOCK_LOGS();
  536. for (lf = logfiles; lf; lf = lf->next)
  537. lf->is_temporary = ! lf->is_temporary;
  538. UNLOCK_LOGS();
  539. close_temp_logs();
  540. }
  541. /** Configure all log handles to be closed by close_temp_logs */
  542. void
  543. mark_logs_temp(void)
  544. {
  545. logfile_t *lf;
  546. LOCK_LOGS();
  547. for (lf = logfiles; lf; lf = lf->next)
  548. lf->is_temporary = 1;
  549. UNLOCK_LOGS();
  550. }
  551. /**
  552. * Add a log handler to send messages to <b>filename</b>. If opening
  553. * the logfile fails, -1 is returned and errno is set appropriately
  554. * (by fopen).
  555. */
  556. int
  557. add_file_log(int loglevelMin, int loglevelMax, const char *filename)
  558. {
  559. FILE *f;
  560. logfile_t *lf;
  561. f = fopen(filename, "a");
  562. if (!f) return -1;
  563. LOCK_LOGS();
  564. add_stream_log_impl(loglevelMin, loglevelMax, filename, f);
  565. logfiles->needs_close = 1;
  566. lf = logfiles;
  567. _log_global_min_severity = get_min_log_level();
  568. UNLOCK_LOGS();
  569. if (log_tor_version(lf, 0) < 0) {
  570. LOCK_LOGS();
  571. delete_log(lf);
  572. UNLOCK_LOGS();
  573. }
  574. return 0;
  575. }
  576. #ifdef HAVE_SYSLOG_H
  577. /**
  578. * Add a log handler to send messages to they system log facility.
  579. */
  580. int
  581. add_syslog_log(int loglevelMin, int loglevelMax)
  582. {
  583. logfile_t *lf;
  584. if (syslog_count++ == 0)
  585. /* This is the first syslog. */
  586. openlog("Tor", LOG_PID | LOG_NDELAY, LOGFACILITY);
  587. lf = tor_malloc_zero(sizeof(logfile_t));
  588. lf->min_loglevel = loglevelMin;
  589. lf->filename = tor_strdup("<syslog>");
  590. lf->max_loglevel = loglevelMax;
  591. lf->is_syslog = 1;
  592. LOCK_LOGS();
  593. lf->next = logfiles;
  594. logfiles = lf;
  595. _log_global_min_severity = get_min_log_level();
  596. UNLOCK_LOGS();
  597. return 0;
  598. }
  599. #endif
  600. /** If <b>level</b> is a valid log severity, return the corresponding
  601. * numeric value. Otherwise, return -1. */
  602. int
  603. parse_log_level(const char *level)
  604. {
  605. if (!strcasecmp(level, "err"))
  606. return LOG_ERR;
  607. if (!strcasecmp(level, "warn"))
  608. return LOG_WARN;
  609. if (!strcasecmp(level, "notice"))
  610. return LOG_NOTICE;
  611. if (!strcasecmp(level, "info"))
  612. return LOG_INFO;
  613. if (!strcasecmp(level, "debug"))
  614. return LOG_DEBUG;
  615. return -1;
  616. }
  617. /** Return the string equivalent of a given log level. */
  618. const char *
  619. log_level_to_string(int level)
  620. {
  621. return sev_to_string(level);
  622. }
  623. /** Return the least severe log level that any current log is interested in. */
  624. int
  625. get_min_log_level(void)
  626. {
  627. logfile_t *lf;
  628. int min = LOG_ERR;
  629. for (lf = logfiles; lf; lf = lf->next) {
  630. if (lf->min_loglevel > min)
  631. min = lf->min_loglevel;
  632. }
  633. return min;
  634. }
  635. /** Switch all logs to output at most verbose level. */
  636. void
  637. switch_logs_debug(void)
  638. {
  639. logfile_t *lf;
  640. LOCK_LOGS();
  641. for (lf = logfiles; lf; lf=lf->next) {
  642. lf->min_loglevel = LOG_DEBUG;
  643. }
  644. UNLOCK_LOGS();
  645. }
  646. #ifdef HAVE_EVENT_SET_LOG_CALLBACK
  647. /** A string which, if it appears in a libevent log, should be ignored. */
  648. static const char *suppress_msg = NULL;
  649. /** Callback function passed to event_set_log() so we can intercept
  650. * log messages from libevent. */
  651. static void
  652. libevent_logging_callback(int severity, const char *msg)
  653. {
  654. char buf[1024];
  655. size_t n;
  656. if (suppress_msg && strstr(msg, suppress_msg))
  657. return;
  658. n = strlcpy(buf, msg, sizeof(buf));
  659. if (n && n < sizeof(buf) && buf[n-1] == '\n') {
  660. buf[n-1] = '\0';
  661. }
  662. switch (severity) {
  663. case _EVENT_LOG_DEBUG:
  664. log(LOG_DEBUG, LD_NET, "Message from libevent: %s", buf);
  665. break;
  666. case _EVENT_LOG_MSG:
  667. log(LOG_INFO, LD_NET, "Message from libevent: %s", buf);
  668. break;
  669. case _EVENT_LOG_WARN:
  670. log(LOG_WARN, LD_GENERAL, "Warning from libevent: %s", buf);
  671. break;
  672. case _EVENT_LOG_ERR:
  673. log(LOG_ERR, LD_GENERAL, "Error from libevent: %s", buf);
  674. break;
  675. default:
  676. log(LOG_WARN, LD_GENERAL, "Message [%d] from libevent: %s",
  677. severity, buf);
  678. break;
  679. }
  680. }
  681. /** Set hook to intercept log messages from libevent. */
  682. void
  683. configure_libevent_logging(void)
  684. {
  685. event_set_log_callback(libevent_logging_callback);
  686. }
  687. /** Ignore any libevent log message that contains <b>msg</b>. */
  688. void
  689. suppress_libevent_log_msg(const char *msg)
  690. {
  691. suppress_msg = msg;
  692. }
  693. #else
  694. void
  695. configure_libevent_logging(void)
  696. {
  697. }
  698. void
  699. suppress_libevent_log_msg(const char *msg)
  700. {
  701. (void)msg;
  702. }
  703. #endif
  704. #if 0
  705. static void
  706. dump_log_info(logfile_t *lf)
  707. {
  708. const char *tp;
  709. if (lf->filename) {
  710. printf("=== log into \"%s\" (%s-%s) (%stemporary)\n", lf->filename,
  711. sev_to_string(lf->min_loglevel),
  712. sev_to_string(lf->max_loglevel),
  713. lf->is_temporary?"":"not ");
  714. } else if (lf->is_syslog) {
  715. printf("=== syslog (%s-%s) (%stemporary)\n",
  716. sev_to_string(lf->min_loglevel),
  717. sev_to_string(lf->max_loglevel),
  718. lf->is_temporary?"":"not ");
  719. } else {
  720. printf("=== log (%s-%s) (%stemporary)\n",
  721. sev_to_string(lf->min_loglevel),
  722. sev_to_string(lf->max_loglevel),
  723. lf->is_temporary?"":"not ");
  724. }
  725. }
  726. void
  727. describe_logs(void)
  728. {
  729. logfile_t *lf;
  730. printf("==== BEGIN LOGS ====\n");
  731. for (lf = logfiles; lf; lf = lf->next)
  732. dump_log_info(lf);
  733. printf("==== END LOGS ====\n");
  734. }
  735. #endif