log.c 11 KB

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