log.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar.
  2. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
  3. /* See LICENSE for licensing information */
  4. /* $Id$ */
  5. const char log_c_id[] = "$Id$";
  6. /**
  7. * \file log.c
  8. * \brief Functions to send messages to log files or the console.
  9. **/
  10. #include "orconfig.h"
  11. #include <stdarg.h>
  12. #include <assert.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #ifdef HAVE_SYS_TIME_H
  16. #include <sys/time.h>
  17. #endif
  18. #ifdef HAVE_TIME_H
  19. #include <time.h>
  20. #endif
  21. #include "./util.h"
  22. #include "./log.h"
  23. #ifdef HAVE_EVENT_H
  24. #include <event.h>
  25. #else
  26. #error "Tor requires libevent to build."
  27. #endif
  28. #define TRUNCATED_STR "[...truncated]"
  29. #define TRUNCATED_STR_LEN 14
  30. /** Information for a single logfile; only used in log.c */
  31. typedef struct logfile_t {
  32. struct logfile_t *next; /**< Next logfile_t in the linked list. */
  33. char *filename; /**< Filename to open. */
  34. FILE *file; /**< Stream to receive log messages. */
  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. /** Linked list of logfile_t. */
  56. static logfile_t *logfiles = NULL;
  57. #ifdef HAVE_SYSLOG_H
  58. static int syslog_count = 0;
  59. #endif
  60. static void delete_log(logfile_t *victim);
  61. static void close_log(logfile_t *victim);
  62. static int reset_log(logfile_t *lf);
  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. #if 0
  303. /** Close and re-open all log files; used to rotate logs on SIGHUP. */
  304. /* Nobody uses this. Remove it? XXXX */
  305. void
  306. reset_logs(void)
  307. {
  308. logfile_t *lf = logfiles;
  309. while (lf) {
  310. if (reset_log(lf)) {
  311. /* error. don't log it. delete the log entry and continue. */
  312. logfile_t *victim = lf;
  313. lf = victim->next;
  314. delete_log(victim);
  315. continue;
  316. }
  317. lf = lf->next;
  318. }
  319. }
  320. #endif
  321. /** Remove and free the log entry <b>victim</b> from the linked-list
  322. * logfiles (it must be present in the list when this function is
  323. * called). After this function is called, the caller shouldn't refer
  324. * to <b>victim</b> anymore.
  325. */
  326. static void
  327. delete_log(logfile_t *victim)
  328. {
  329. logfile_t *tmpl;
  330. if (victim == logfiles)
  331. logfiles = victim->next;
  332. else {
  333. for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
  334. tor_assert(tmpl);
  335. tor_assert(tmpl->next == victim);
  336. tmpl->next = victim->next;
  337. }
  338. tor_free(victim->filename);
  339. tor_free(victim);
  340. }
  341. /** Helper: release system resources (but not memory) held by a single
  342. * logfile_t. */
  343. static void
  344. close_log(logfile_t *victim)
  345. {
  346. if (victim->needs_close && victim->file) {
  347. fclose(victim->file);
  348. } else if (victim->is_syslog) {
  349. #ifdef HAVE_SYSLOG_H
  350. if (--syslog_count == 0) {
  351. /* There are no other syslogs; close the logging facility. */
  352. closelog();
  353. }
  354. #endif
  355. }
  356. }
  357. /** Helper: reset a single logfile_t. For a file log, this involves
  358. * closing and reopening the log, and maybe writing the version. For
  359. * other log types, do nothing. */
  360. static int
  361. reset_log(logfile_t *lf)
  362. {
  363. if (lf->needs_close) {
  364. if (fclose(lf->file)==EOF ||
  365. !(lf->file = fopen(lf->filename, "a"))) {
  366. return -1;
  367. } else {
  368. if (log_tor_version(lf, 1) < 0)
  369. return -1;
  370. }
  371. }
  372. return 0;
  373. }
  374. /** Add a log handler to send all messages of severity <b>loglevel</b>
  375. * or higher to <b>stream</b>. */
  376. void
  377. add_stream_log(int loglevelMin, int loglevelMax, const char *name, FILE *stream)
  378. {
  379. logfile_t *lf;
  380. lf = tor_malloc_zero(sizeof(logfile_t));
  381. lf->filename = tor_strdup(name);
  382. lf->min_loglevel = loglevelMin;
  383. lf->max_loglevel = loglevelMax;
  384. lf->file = stream;
  385. lf->next = logfiles;
  386. logfiles = lf;
  387. }
  388. /** Add a log handler to receive messages during startup (before the real
  389. * logs are initialized).
  390. */
  391. void
  392. add_temp_log(void)
  393. {
  394. add_stream_log(LOG_NOTICE, LOG_ERR, "<temp>", stdout);
  395. logfiles->is_temporary = 1;
  396. }
  397. /**
  398. * Add a log handler to send messages of severity between
  399. * <b>logLevelmin</b> and <b>logLevelMax</b> to the function
  400. * <b>cb</b>.
  401. */
  402. int
  403. add_callback_log(int loglevelMin, int loglevelMax, log_callback cb)
  404. {
  405. logfile_t *lf;
  406. lf = tor_malloc_zero(sizeof(logfile_t));
  407. lf->min_loglevel = loglevelMin;
  408. lf->max_loglevel = loglevelMax;
  409. lf->filename = tor_strdup("<callback>");
  410. lf->callback = cb;
  411. lf->next = logfiles;
  412. logfiles = lf;
  413. return 0;
  414. }
  415. void
  416. change_callback_log_severity(int loglevelMin, int loglevelMax,
  417. log_callback cb)
  418. {
  419. logfile_t *lf;
  420. for (lf = logfiles; lf; lf = lf->next) {
  421. if (lf->callback == cb) {
  422. lf->min_loglevel = loglevelMin;
  423. lf->max_loglevel = loglevelMax;
  424. }
  425. }
  426. }
  427. /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
  428. void
  429. close_temp_logs(void)
  430. {
  431. logfile_t *lf, **p;
  432. for (p = &logfiles; *p; ) {
  433. if ((*p)->is_temporary) {
  434. lf = *p;
  435. /* we use *p here to handle the edge case of the head of the list */
  436. *p = (*p)->next;
  437. close_log(lf);
  438. tor_free(lf->filename);
  439. tor_free(lf);
  440. } else {
  441. p = &((*p)->next);
  442. }
  443. }
  444. }
  445. /** Configure all log handles to be closed by close_temp_logs */
  446. void
  447. mark_logs_temp(void)
  448. {
  449. logfile_t *lf;
  450. for (lf = logfiles; lf; lf = lf->next)
  451. lf->is_temporary = 1;
  452. }
  453. /**
  454. * Add a log handler to send messages to <b>filename</b>. If opening
  455. * the logfile fails, -1 is returned and errno is set appropriately
  456. * (by fopen).
  457. */
  458. int
  459. add_file_log(int loglevelMin, int loglevelMax, const char *filename)
  460. {
  461. FILE *f;
  462. f = fopen(filename, "a");
  463. if (!f) return -1;
  464. add_stream_log(loglevelMin, loglevelMax, filename, f);
  465. logfiles->needs_close = 1;
  466. if (log_tor_version(logfiles, 0) < 0) {
  467. delete_log(logfiles);
  468. }
  469. return 0;
  470. }
  471. #ifdef HAVE_SYSLOG_H
  472. /**
  473. * Add a log handler to send messages to they system log facility.
  474. */
  475. int
  476. add_syslog_log(int loglevelMin, int loglevelMax)
  477. {
  478. logfile_t *lf;
  479. if (syslog_count++ == 0)
  480. /* This is the first syslog. */
  481. openlog("Tor", LOG_PID | LOG_NDELAY, LOG_DAEMON);
  482. lf = tor_malloc_zero(sizeof(logfile_t));
  483. lf->min_loglevel = loglevelMin;
  484. lf->filename = tor_strdup("<syslog>");
  485. lf->max_loglevel = loglevelMax;
  486. lf->is_syslog = 1;
  487. lf->next = logfiles;
  488. logfiles = lf;
  489. return 0;
  490. }
  491. #endif
  492. /** If <b>level</b> is a valid log severity, return the corresponding
  493. * numeric value. Otherwise, return -1. */
  494. int
  495. parse_log_level(const char *level)
  496. {
  497. if (!strcasecmp(level, "err"))
  498. return LOG_ERR;
  499. if (!strcasecmp(level, "warn"))
  500. return LOG_WARN;
  501. if (!strcasecmp(level, "notice"))
  502. return LOG_NOTICE;
  503. if (!strcasecmp(level, "info"))
  504. return LOG_INFO;
  505. if (!strcasecmp(level, "debug"))
  506. return LOG_DEBUG;
  507. return -1;
  508. }
  509. /** Return the string equivalent of a given log level. */
  510. const char *
  511. log_level_to_string(int level)
  512. {
  513. return sev_to_string(level);
  514. }
  515. /** Return the least severe log level that any current log is interested in. */
  516. int
  517. get_min_log_level(void)
  518. {
  519. logfile_t *lf;
  520. int min = LOG_ERR;
  521. for (lf = logfiles; lf; lf = lf->next) {
  522. if (lf->min_loglevel > min)
  523. min = lf->min_loglevel;
  524. }
  525. return min;
  526. }
  527. /** Switch all logs to output at most verbose level. */
  528. void
  529. switch_logs_debug(void)
  530. {
  531. logfile_t *lf;
  532. for (lf = logfiles; lf; lf=lf->next) {
  533. lf->min_loglevel = LOG_DEBUG;
  534. }
  535. }
  536. #ifdef HAVE_EVENT_SET_LOG_CALLBACK
  537. static const char *suppress_msg = NULL;
  538. static void
  539. libevent_logging_callback(int severity, const char *msg)
  540. {
  541. char buf[1024];
  542. size_t n;
  543. if (suppress_msg && strstr(msg, suppress_msg))
  544. return;
  545. n = strlcpy(buf, msg, sizeof(buf));
  546. if (n && n < sizeof(buf) && buf[n-1] == '\n') {
  547. buf[n-1] = '\0';
  548. }
  549. switch (severity) {
  550. case _EVENT_LOG_DEBUG:
  551. log(LOG_DEBUG, LD_NET, "Message from libevent: %s", buf);
  552. break;
  553. case _EVENT_LOG_MSG:
  554. log(LOG_INFO, LD_NET, "Message from libevent: %s", buf);
  555. break;
  556. case _EVENT_LOG_WARN:
  557. log(LOG_WARN, LD_GENERAL, "Warning from libevent: %s", buf);
  558. break;
  559. case _EVENT_LOG_ERR:
  560. log(LOG_ERR, LD_GENERAL, "Error from libevent: %s", buf);
  561. break;
  562. default:
  563. log(LOG_WARN, LD_GENERAL, "Message [%d] from libevent: %s",
  564. severity, buf);
  565. break;
  566. }
  567. }
  568. void
  569. configure_libevent_logging(void)
  570. {
  571. event_set_log_callback(libevent_logging_callback);
  572. }
  573. void
  574. suppress_libevent_log_msg(const char *msg)
  575. {
  576. suppress_msg = msg;
  577. }
  578. #else
  579. void
  580. configure_libevent_logging(void)
  581. {
  582. }
  583. void
  584. suppress_libevent_log_msg(const char *msg)
  585. {
  586. }
  587. #endif