log.c 12 KB

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