log.c 14 KB

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