log.c 13 KB

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