log.c 17 KB

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