log.c 16 KB

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