log.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. debug(uint32_t domain, const char *format, ...)
  245. {
  246. va_list ap;
  247. va_start(ap,format);
  248. logv(LOG_DEBUG, domain, NULL, format, ap);
  249. va_end(ap);
  250. _log_fn_function_name = NULL;
  251. }
  252. void
  253. info(uint32_t domain, const char *format, ...)
  254. {
  255. va_list ap;
  256. va_start(ap,format);
  257. logv(LOG_INFO, domain, NULL, format, ap);
  258. va_end(ap);
  259. _log_fn_function_name = NULL;
  260. }
  261. void
  262. notice(uint32_t domain, const char *format, ...)
  263. {
  264. va_list ap;
  265. va_start(ap,format);
  266. logv(LOG_NOTICE, domain, NULL, format, ap);
  267. va_end(ap);
  268. _log_fn_function_name = NULL;
  269. }
  270. void
  271. warn(uint32_t domain, const char *format, ...)
  272. {
  273. va_list ap;
  274. va_start(ap,format);
  275. logv(LOG_WARN, domain, NULL, format, ap);
  276. va_end(ap);
  277. _log_fn_function_name = NULL;
  278. }
  279. void
  280. err(uint32_t domain, const char *format, ...)
  281. {
  282. va_list ap;
  283. va_start(ap,format);
  284. logv(LOG_ERR, domain, NULL, 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;
  294. while (logfiles) {
  295. victim = logfiles;
  296. logfiles = logfiles->next;
  297. close_log(victim);
  298. tor_free(victim->filename);
  299. tor_free(victim);
  300. }
  301. }
  302. /** Remove and free the log entry <b>victim</b> from the linked-list
  303. * logfiles (it must be present in the list when this function is
  304. * called). After this function is called, the caller shouldn't refer
  305. * to <b>victim</b> anymore.
  306. */
  307. static void
  308. delete_log(logfile_t *victim)
  309. {
  310. logfile_t *tmpl;
  311. if (victim == logfiles)
  312. logfiles = victim->next;
  313. else {
  314. for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
  315. tor_assert(tmpl);
  316. tor_assert(tmpl->next == victim);
  317. tmpl->next = victim->next;
  318. }
  319. tor_free(victim->filename);
  320. tor_free(victim);
  321. }
  322. /** Helper: release system resources (but not memory) held by a single
  323. * logfile_t. */
  324. static void
  325. close_log(logfile_t *victim)
  326. {
  327. if (victim->needs_close && victim->file) {
  328. fclose(victim->file);
  329. } else if (victim->is_syslog) {
  330. #ifdef HAVE_SYSLOG_H
  331. if (--syslog_count == 0) {
  332. /* There are no other syslogs; close the logging facility. */
  333. closelog();
  334. }
  335. #endif
  336. }
  337. }
  338. /** Add a log handler to send all messages of severity <b>loglevel</b>
  339. * or higher to <b>stream</b>. */
  340. void
  341. add_stream_log(int loglevelMin, int loglevelMax,
  342. const char *name, FILE *stream)
  343. {
  344. logfile_t *lf;
  345. lf = tor_malloc_zero(sizeof(logfile_t));
  346. lf->filename = tor_strdup(name);
  347. lf->min_loglevel = loglevelMin;
  348. lf->max_loglevel = loglevelMax;
  349. lf->file = stream;
  350. lf->next = logfiles;
  351. logfiles = lf;
  352. }
  353. /** Add a log handler to receive messages during startup (before the real
  354. * logs are initialized).
  355. */
  356. void
  357. add_temp_log(void)
  358. {
  359. add_stream_log(LOG_NOTICE, LOG_ERR, "<temp>", stdout);
  360. logfiles->is_temporary = 1;
  361. }
  362. /**
  363. * Add a log handler to send messages of severity between
  364. * <b>logLevelmin</b> and <b>logLevelMax</b> to the function
  365. * <b>cb</b>.
  366. */
  367. int
  368. add_callback_log(int loglevelMin, int loglevelMax, log_callback cb)
  369. {
  370. logfile_t *lf;
  371. lf = tor_malloc_zero(sizeof(logfile_t));
  372. lf->min_loglevel = loglevelMin;
  373. lf->max_loglevel = loglevelMax;
  374. lf->filename = tor_strdup("<callback>");
  375. lf->callback = cb;
  376. lf->next = logfiles;
  377. logfiles = lf;
  378. return 0;
  379. }
  380. void
  381. change_callback_log_severity(int loglevelMin, int loglevelMax,
  382. log_callback cb)
  383. {
  384. logfile_t *lf;
  385. for (lf = logfiles; lf; lf = lf->next) {
  386. if (lf->callback == cb) {
  387. lf->min_loglevel = loglevelMin;
  388. lf->max_loglevel = loglevelMax;
  389. }
  390. }
  391. }
  392. /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
  393. void
  394. close_temp_logs(void)
  395. {
  396. logfile_t *lf, **p;
  397. for (p = &logfiles; *p; ) {
  398. if ((*p)->is_temporary) {
  399. lf = *p;
  400. /* we use *p here to handle the edge case of the head of the list */
  401. *p = (*p)->next;
  402. close_log(lf);
  403. tor_free(lf->filename);
  404. tor_free(lf);
  405. } else {
  406. p = &((*p)->next);
  407. }
  408. }
  409. }
  410. /** Make all currently temporary logs (set to be closed by close_temp_logs)
  411. * live again, and close all non-temporary logs. */
  412. void
  413. rollback_log_changes(void)
  414. {
  415. logfile_t *lf;
  416. for (lf = logfiles; lf; lf = lf->next)
  417. lf->is_temporary = ! lf->is_temporary;
  418. close_temp_logs();
  419. }
  420. /** Configure all log handles to be closed by close_temp_logs */
  421. void
  422. mark_logs_temp(void)
  423. {
  424. logfile_t *lf;
  425. for (lf = logfiles; lf; lf = lf->next)
  426. lf->is_temporary = 1;
  427. }
  428. /**
  429. * Add a log handler to send messages to <b>filename</b>. If opening
  430. * the logfile fails, -1 is returned and errno is set appropriately
  431. * (by fopen).
  432. */
  433. int
  434. add_file_log(int loglevelMin, int loglevelMax, const char *filename)
  435. {
  436. FILE *f;
  437. f = fopen(filename, "a");
  438. if (!f) return -1;
  439. add_stream_log(loglevelMin, loglevelMax, filename, f);
  440. logfiles->needs_close = 1;
  441. if (log_tor_version(logfiles, 0) < 0) {
  442. delete_log(logfiles);
  443. }
  444. return 0;
  445. }
  446. #ifdef HAVE_SYSLOG_H
  447. /**
  448. * Add a log handler to send messages to they system log facility.
  449. */
  450. int
  451. add_syslog_log(int loglevelMin, int loglevelMax)
  452. {
  453. logfile_t *lf;
  454. if (syslog_count++ == 0)
  455. /* This is the first syslog. */
  456. openlog("Tor", LOG_PID | LOG_NDELAY, LOGFACILITY);
  457. lf = tor_malloc_zero(sizeof(logfile_t));
  458. lf->min_loglevel = loglevelMin;
  459. lf->filename = tor_strdup("<syslog>");
  460. lf->max_loglevel = loglevelMax;
  461. lf->is_syslog = 1;
  462. lf->next = logfiles;
  463. logfiles = lf;
  464. return 0;
  465. }
  466. #endif
  467. /** If <b>level</b> is a valid log severity, return the corresponding
  468. * numeric value. Otherwise, return -1. */
  469. int
  470. parse_log_level(const char *level)
  471. {
  472. if (!strcasecmp(level, "err"))
  473. return LOG_ERR;
  474. if (!strcasecmp(level, "warn"))
  475. return LOG_WARN;
  476. if (!strcasecmp(level, "notice"))
  477. return LOG_NOTICE;
  478. if (!strcasecmp(level, "info"))
  479. return LOG_INFO;
  480. if (!strcasecmp(level, "debug"))
  481. return LOG_DEBUG;
  482. return -1;
  483. }
  484. /** Return the string equivalent of a given log level. */
  485. const char *
  486. log_level_to_string(int level)
  487. {
  488. return sev_to_string(level);
  489. }
  490. /** Return the least severe log level that any current log is interested in. */
  491. int
  492. get_min_log_level(void)
  493. {
  494. logfile_t *lf;
  495. int min = LOG_ERR;
  496. for (lf = logfiles; lf; lf = lf->next) {
  497. if (lf->min_loglevel > min)
  498. min = lf->min_loglevel;
  499. }
  500. return min;
  501. }
  502. /** Switch all logs to output at most verbose level. */
  503. void
  504. switch_logs_debug(void)
  505. {
  506. logfile_t *lf;
  507. for (lf = logfiles; lf; lf=lf->next) {
  508. lf->min_loglevel = LOG_DEBUG;
  509. }
  510. }
  511. #ifdef HAVE_EVENT_SET_LOG_CALLBACK
  512. static const char *suppress_msg = NULL;
  513. static void
  514. libevent_logging_callback(int severity, const char *msg)
  515. {
  516. char buf[1024];
  517. size_t n;
  518. if (suppress_msg && strstr(msg, suppress_msg))
  519. return;
  520. n = strlcpy(buf, msg, sizeof(buf));
  521. if (n && n < sizeof(buf) && buf[n-1] == '\n') {
  522. buf[n-1] = '\0';
  523. }
  524. switch (severity) {
  525. case _EVENT_LOG_DEBUG:
  526. log(LOG_DEBUG, LD_NET, "Message from libevent: %s", buf);
  527. break;
  528. case _EVENT_LOG_MSG:
  529. log(LOG_INFO, LD_NET, "Message from libevent: %s", buf);
  530. break;
  531. case _EVENT_LOG_WARN:
  532. log(LOG_WARN, LD_GENERAL, "Warning from libevent: %s", buf);
  533. break;
  534. case _EVENT_LOG_ERR:
  535. log(LOG_ERR, LD_GENERAL, "Error from libevent: %s", buf);
  536. break;
  537. default:
  538. log(LOG_WARN, LD_GENERAL, "Message [%d] from libevent: %s",
  539. severity, buf);
  540. break;
  541. }
  542. }
  543. void
  544. configure_libevent_logging(void)
  545. {
  546. event_set_log_callback(libevent_logging_callback);
  547. }
  548. void
  549. suppress_libevent_log_msg(const char *msg)
  550. {
  551. suppress_msg = msg;
  552. }
  553. #else
  554. void
  555. configure_libevent_logging(void)
  556. {
  557. }
  558. void
  559. suppress_libevent_log_msg(const char *msg)
  560. {
  561. }
  562. #endif
  563. #if 0
  564. static void
  565. dump_log_info(logfile_t *lf)
  566. {
  567. const char *tp;
  568. if (lf->filename) {
  569. printf("=== log into \"%s\" (%s-%s) (%stemporary)\n", lf->filename,
  570. sev_to_string(lf->min_loglevel),
  571. sev_to_string(lf->max_loglevel),
  572. lf->is_temporary?"":"not ");
  573. } else if (lf->is_syslog) {
  574. printf("=== syslog (%s-%s) (%stemporary)\n",
  575. sev_to_string(lf->min_loglevel),
  576. sev_to_string(lf->max_loglevel),
  577. lf->is_temporary?"":"not ");
  578. } else {
  579. printf("=== log (%s-%s) (%stemporary)\n",
  580. sev_to_string(lf->min_loglevel),
  581. sev_to_string(lf->max_loglevel),
  582. lf->is_temporary?"":"not ");
  583. }
  584. }
  585. void
  586. describe_logs(void)
  587. {
  588. logfile_t *lf;
  589. printf("==== BEGIN LOGS ====\n");
  590. for (lf = logfiles; lf; lf = lf->next)
  591. dump_log_info(lf);
  592. printf("==== END LOGS ====\n");
  593. }
  594. #endif