log.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. static void log_tor_version(logfile_t *lf, int reset)
  70. {
  71. char buf[256];
  72. size_t n;
  73. int is_new;
  74. if (!lf->needs_close)
  75. /* If it doesn't get closed, it isn't really a file. */
  76. return;
  77. if (lf->is_temporary)
  78. /* If it's temporary, it isn't really a file. */
  79. return;
  80. #ifdef HAVE_FTELLO
  81. is_new = (ftello(lf->file) == 0);
  82. #else
  83. is_new = (ftell(lf->file) == 0);
  84. #endif
  85. if (reset && !is_new)
  86. /* We are resetting, but we aren't at the start of the file; no
  87. * need to log again. */
  88. return;
  89. n = _log_prefix(buf, sizeof(buf), LOG_NOTICE);
  90. tor_snprintf(buf+n, sizeof(buf)-n,
  91. "Tor %s opening %slog file.\n", VERSION, is_new?"new ":"");
  92. fputs(buf, lf->file);
  93. }
  94. /** Helper: Format a log message into a fixed-sized buffer. (This is
  95. * factored out of <b>logv</b> so that we never format a message more
  96. * than once.) Return a pointer to the first character of the message
  97. * portion of the formatted string.
  98. */
  99. static INLINE char *format_msg(char *buf, size_t buf_len,
  100. int severity, const char *funcname,
  101. const char *format, va_list ap)
  102. {
  103. size_t n;
  104. int r;
  105. char *end_of_prefix;
  106. buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
  107. n = _log_prefix(buf, buf_len, severity);
  108. end_of_prefix = buf+n;
  109. if (funcname) {
  110. r = tor_snprintf(buf+n, buf_len-n, "%s(): ", funcname);
  111. if (r<0)
  112. n = strlen(buf);
  113. else
  114. n += r;
  115. }
  116. r = tor_vsnprintf(buf+n,buf_len-n,format,ap);
  117. if(r < 0) {
  118. n = buf_len-2;
  119. strlcpy(buf+buf_len-TRUNCATED_STR_LEN-1, TRUNCATED_STR,
  120. buf_len-(buf_len-TRUNCATED_STR_LEN-1));
  121. } else {
  122. n += r;
  123. }
  124. buf[n]='\n';
  125. buf[n+1]='\0';
  126. return end_of_prefix;
  127. }
  128. /** Helper: sends a message to the appropriate logfiles, at loglevel
  129. * <b>severity</b>. If provided, <b>funcname</b> is prepended to the
  130. * message. The actual message is derived as from tor_snprintf(format,ap).
  131. */
  132. static void
  133. logv(int severity, const char *funcname, const char *format, va_list ap)
  134. {
  135. char buf[10024];
  136. int formatted = 0;
  137. logfile_t *lf;
  138. char *end_of_prefix=NULL;
  139. assert(format);
  140. lf = logfiles;
  141. while(lf) {
  142. if (severity > lf->loglevel || severity < lf->max_loglevel) {
  143. lf = lf->next;
  144. continue;
  145. }
  146. if (! (lf->file || lf->is_syslog || lf->callback)) {
  147. lf = lf->next;
  148. continue;
  149. }
  150. if (!formatted) {
  151. end_of_prefix =
  152. format_msg(buf, sizeof(buf), severity, funcname, format, ap);
  153. formatted = 1;
  154. }
  155. if (lf->is_syslog) {
  156. #ifdef HAVE_SYSLOG_H
  157. syslog(severity, "%s", end_of_prefix);
  158. #endif
  159. lf = lf->next;
  160. continue;
  161. } else if (lf->callback) {
  162. lf->callback(severity, end_of_prefix);
  163. lf = lf->next;
  164. continue;
  165. }
  166. if(fputs(buf, lf->file) == EOF ||
  167. fflush(lf->file) == EOF) { /* error */
  168. /* don't log the error! Blow away this log entry and continue. */
  169. logfile_t *victim = lf;
  170. lf = victim->next;
  171. delete_log(victim);
  172. } else {
  173. lf = lf->next;
  174. }
  175. }
  176. }
  177. /** Output a message to the log. */
  178. void _log(int severity, const char *format, ...)
  179. {
  180. va_list ap;
  181. va_start(ap,format);
  182. logv(severity, NULL, format, ap);
  183. va_end(ap);
  184. }
  185. /** Output a message to the log, prefixed with a function name <b>fn</b>. */
  186. void _log_fn(int severity, const char *fn, const char *format, ...)
  187. {
  188. va_list ap;
  189. va_start(ap,format);
  190. logv(severity, fn, format, ap);
  191. va_end(ap);
  192. }
  193. /** Close all open log files. */
  194. void close_logs()
  195. {
  196. logfile_t *victim;
  197. while(logfiles) {
  198. victim = logfiles;
  199. logfiles = logfiles->next;
  200. close_log(victim);
  201. tor_free(victim->filename);
  202. tor_free(victim);
  203. }
  204. }
  205. /** Close and re-open all log files; used to rotate logs on SIGHUP. */
  206. void reset_logs()
  207. {
  208. logfile_t *lf = logfiles;
  209. while(lf) {
  210. if (reset_log(lf)) {
  211. /* error. don't log it. delete the log entry and continue. */
  212. logfile_t *victim = lf;
  213. lf = victim->next;
  214. delete_log(victim);
  215. continue;
  216. }
  217. lf = lf->next;
  218. }
  219. }
  220. /** Remove and free the log entry <b>victim</b> from the linked-list
  221. * logfiles (it must be present in the list when this function is
  222. * called). After this function is called, the caller shouldn't refer
  223. * to <b>victim</b> anymore.
  224. */
  225. static void delete_log(logfile_t *victim) {
  226. logfile_t *tmpl;
  227. if(victim == logfiles)
  228. logfiles = victim->next;
  229. else {
  230. for(tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
  231. tor_assert(tmpl);
  232. tor_assert(tmpl->next == victim);
  233. tmpl->next = victim->next;
  234. }
  235. tor_free(victim->filename);
  236. tor_free(victim);
  237. }
  238. static void close_log(logfile_t *victim)
  239. {
  240. if (victim->needs_close && victim->file) {
  241. fclose(victim->file);
  242. } else if (victim->is_syslog) {
  243. #ifdef HAVE_SYSLOG_H
  244. if (--syslog_count == 0)
  245. /* There are no other syslogs; close the logging facility. */
  246. closelog();
  247. #endif
  248. }
  249. }
  250. static int reset_log(logfile_t *lf)
  251. {
  252. if (lf->needs_close) {
  253. if(fclose(lf->file)==EOF ||
  254. !(lf->file = fopen(lf->filename, "a"))) {
  255. return -1;
  256. } else {
  257. log_tor_version(lf, 1);
  258. }
  259. }
  260. return 0;
  261. }
  262. /** Add a log handler to send all messages of severity <b>loglevel</b>
  263. * or higher to <b>stream</b>. */
  264. void add_stream_log(int loglevelMin, int loglevelMax, const char *name, FILE *stream)
  265. {
  266. logfile_t *lf;
  267. lf = tor_malloc_zero(sizeof(logfile_t));
  268. lf->filename = tor_strdup(name);
  269. lf->loglevel = loglevelMin;
  270. lf->max_loglevel = loglevelMax;
  271. lf->file = stream;
  272. lf->next = logfiles;
  273. logfiles = lf;
  274. }
  275. /** Add a log handler to receive messages during startup (before the real
  276. * logs are initialized).
  277. */
  278. void add_temp_log(void)
  279. {
  280. add_stream_log(LOG_INFO, LOG_ERR, "<temp>", stdout);
  281. logfiles->is_temporary = 1;
  282. }
  283. int add_callback_log(int loglevelMin, int loglevelMax, log_callback cb)
  284. {
  285. logfile_t *lf;
  286. lf = tor_malloc_zero(sizeof(logfile_t));
  287. lf->loglevel = loglevelMin;
  288. lf->max_loglevel = loglevelMax;
  289. lf->filename = tor_strdup("<callback>");
  290. lf->callback = cb;
  291. lf->next = logfiles;
  292. logfiles = lf;
  293. return 0;
  294. }
  295. /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
  296. void close_temp_logs(void)
  297. {
  298. logfile_t *lf, **p;
  299. for (p = &logfiles; *p; ) {
  300. if ((*p)->is_temporary) {
  301. lf = *p;
  302. *p = (*p)->next;
  303. close_log(lf);
  304. tor_free(lf->filename);
  305. tor_free(lf);
  306. } else {
  307. p = &((*p)->next);
  308. }
  309. }
  310. }
  311. /** Configure all log handles to be closed by close_temp_logs */
  312. void mark_logs_temp(void)
  313. {
  314. logfile_t *lf;
  315. for (lf = logfiles; lf; lf = lf->next)
  316. lf->is_temporary = 1;
  317. }
  318. /**
  319. * Add a log handler to send messages to <b>filename</b>. If opening
  320. * the logfile fails, -1 is returned and errno is set appropriately
  321. * (by fopen).
  322. */
  323. int add_file_log(int loglevelMin, int loglevelMax, const char *filename)
  324. {
  325. FILE *f;
  326. f = fopen(filename, "a");
  327. if (!f) return -1;
  328. add_stream_log(loglevelMin, loglevelMax, filename, f);
  329. logfiles->needs_close = 1;
  330. log_tor_version(logfiles, 0);
  331. return 0;
  332. }
  333. #ifdef HAVE_SYSLOG_H
  334. /**
  335. * Add a log handler to send messages to they system log facility.
  336. */
  337. int add_syslog_log(int loglevelMin, int loglevelMax)
  338. {
  339. logfile_t *lf;
  340. if (syslog_count++ == 0)
  341. /* This is the first syslog. */
  342. openlog("Tor", LOG_NDELAY, LOG_DAEMON);
  343. lf = tor_malloc_zero(sizeof(logfile_t));
  344. lf->loglevel = loglevelMin;
  345. lf->filename = tor_strdup("<syslog>");
  346. lf->max_loglevel = loglevelMax;
  347. lf->is_syslog = 1;
  348. lf->next = logfiles;
  349. logfiles = lf;
  350. return 0;
  351. }
  352. #endif
  353. /** If <b>level</b> is a valid log severity, return the corresponding
  354. * numeric value. Otherwise, return -1. */
  355. int parse_log_level(const char *level) {
  356. if (!strcasecmp(level, "err"))
  357. return LOG_ERR;
  358. if (!strcasecmp(level, "warn"))
  359. return LOG_WARN;
  360. if (!strcasecmp(level, "notice"))
  361. return LOG_NOTICE;
  362. if (!strcasecmp(level, "info"))
  363. return LOG_INFO;
  364. if (!strcasecmp(level, "debug"))
  365. return LOG_DEBUG;
  366. return -1;
  367. }
  368. const char *log_level_to_string(int level)
  369. {
  370. return sev_to_string(level);
  371. }
  372. int get_min_log_level(void)
  373. {
  374. logfile_t *lf;
  375. int min = LOG_ERR;
  376. for (lf = logfiles; lf; lf = lf->next) {
  377. if (lf->loglevel > min)
  378. min = lf->loglevel;
  379. }
  380. return min;
  381. }
  382. /*
  383. Local Variables:
  384. mode:c
  385. indent-tabs-mode:nil
  386. c-basic-offset:2
  387. End:
  388. */