log.c 10 KB

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