log.c 17 KB

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