Flush the logfd after we print "Tor opening log file", so we don't see those messages...
[tor.git] / src / common / log.c
blobc91b13275986f306070b9b2774c331aaabbecde5
1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
5 /**
6 * \file log.c
8 * \brief Functions to send messages to log files or the console.
9 */
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"
19 #define TRUNCATED_STR "[...truncated]"
20 #define TRUNCATED_STR_LEN 14
22 /** Information for a single logfile; only used in log.c */
23 typedef struct logfile_t {
24 struct logfile_t *next; /**< Next logfile_t in the linked list. */
25 char *filename; /**< Filename to open. */
26 FILE *file; /**< Stream to receive log messages. */
27 int needs_close; /**< Boolean: true if the stream gets closed on shutdown. */
28 int loglevel; /**< Lowest severity level to send to this stream. */
29 int max_loglevel; /**< Highest severity level to send to this stream. */
30 int is_temporary; /**< Boolean: close after initializing logging subsystem.*/
31 int is_syslog; /**< Boolean: send messages to syslog. */
32 log_callback callback; /**< If not NULL, send messages to this function. */
33 } logfile_t;
35 /** Helper: map a log severity to descriptive string. */
36 static INLINE const char *sev_to_string(int severity) {
37 switch(severity) {
38 case LOG_DEBUG: return "debug";
39 case LOG_INFO: return "info";
40 case LOG_NOTICE: return "notice";
41 case LOG_WARN: return "warn";
42 case LOG_ERR: return "err";
43 default: assert(0); return "UNKNOWN";
47 /** Linked list of logfile_t. */
48 static logfile_t *logfiles = NULL;
49 #ifdef HAVE_SYSLOG_H
50 static int syslog_count = 0;
51 #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);
57 static INLINE size_t
58 _log_prefix(char *buf, size_t buf_len, int severity)
60 time_t t;
61 struct timeval now;
62 size_t n;
63 int r;
65 tor_gettimeofday(&now);
66 t = (time_t)now.tv_sec;
68 n = strftime(buf, buf_len, "%b %d %H:%M:%S", localtime(&t));
69 r = tor_snprintf(buf+n, buf_len-n,
70 ".%.3ld [%s] ",
71 (long)now.tv_usec / 1000, sev_to_string(severity));
72 if (r<0)
73 return buf_len-1;
74 else
75 return n+r;
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.
81 * Return -1 if the log is broken and needs to be deleted, else return 0.
83 static int log_tor_version(logfile_t *lf, int reset)
85 char buf[256];
86 size_t n;
87 int is_new;
89 if (!lf->needs_close)
90 /* If it doesn't get closed, it isn't really a file. */
91 return 0;
92 if (lf->is_temporary)
93 /* If it's temporary, it isn't really a file. */
94 return 0;
95 #ifdef HAVE_FTELLO
96 is_new = (ftello(lf->file) == 0);
97 #else
98 is_new = (ftell(lf->file) == 0);
99 #endif
100 if (reset && !is_new)
101 /* We are resetting, but we aren't at the start of the file; no
102 * need to log again. */
103 return 0;
104 n = _log_prefix(buf, sizeof(buf), LOG_NOTICE);
105 tor_snprintf(buf+n, sizeof(buf)-n,
106 "Tor %s opening %slog file.\n", VERSION, is_new?"new ":"");
107 if (fputs(buf, lf->file) == EOF ||
108 fflush(lf->file) == EOF) /* error */
109 return -1; /* failed */
110 return 0;
113 /** Helper: Format a log message into a fixed-sized buffer. (This is
114 * factored out of <b>logv</b> so that we never format a message more
115 * than once.) Return a pointer to the first character of the message
116 * portion of the formatted string.
118 static INLINE char *format_msg(char *buf, size_t buf_len,
119 int severity, const char *funcname,
120 const char *format, va_list ap)
122 size_t n;
123 int r;
124 char *end_of_prefix;
125 buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
127 n = _log_prefix(buf, buf_len, severity);
128 end_of_prefix = buf+n;
130 if (funcname) {
131 r = tor_snprintf(buf+n, buf_len-n, "%s(): ", funcname);
132 if (r<0)
133 n = strlen(buf);
134 else
135 n += r;
138 r = tor_vsnprintf(buf+n,buf_len-n,format,ap);
139 if(r < 0) {
140 n = buf_len-2;
141 strlcpy(buf+buf_len-TRUNCATED_STR_LEN-1, TRUNCATED_STR,
142 buf_len-(buf_len-TRUNCATED_STR_LEN-1));
143 } else {
144 n += r;
146 buf[n]='\n';
147 buf[n+1]='\0';
148 return end_of_prefix;
151 /** Helper: sends a message to the appropriate logfiles, at loglevel
152 * <b>severity</b>. If provided, <b>funcname</b> is prepended to the
153 * message. The actual message is derived as from tor_snprintf(format,ap).
155 static void
156 logv(int severity, const char *funcname, const char *format, va_list ap)
158 char buf[10024];
159 int formatted = 0;
160 logfile_t *lf;
161 char *end_of_prefix=NULL;
163 assert(format);
164 lf = logfiles;
165 while(lf) {
166 if (severity > lf->loglevel || severity < lf->max_loglevel) {
167 lf = lf->next;
168 continue;
170 if (! (lf->file || lf->is_syslog || lf->callback)) {
171 lf = lf->next;
172 continue;
175 if (!formatted) {
176 end_of_prefix =
177 format_msg(buf, sizeof(buf), severity, funcname, format, ap);
178 formatted = 1;
180 if (lf->is_syslog) {
181 #ifdef HAVE_SYSLOG_H
182 syslog(severity, "%s", end_of_prefix);
183 #endif
184 lf = lf->next;
185 continue;
186 } else if (lf->callback) {
187 lf->callback(severity, end_of_prefix);
188 lf = lf->next;
189 continue;
191 if(fputs(buf, lf->file) == EOF ||
192 fflush(lf->file) == EOF) { /* error */
193 /* don't log the error! Blow away this log entry and continue. */
194 logfile_t *victim = lf;
195 lf = victim->next;
196 delete_log(victim);
197 } else {
198 lf = lf->next;
203 /** Output a message to the log. */
204 void _log(int severity, const char *format, ...)
206 va_list ap;
207 va_start(ap,format);
208 logv(severity, NULL, format, ap);
209 va_end(ap);
212 /** Output a message to the log, prefixed with a function name <b>fn</b>. */
213 #ifdef __GNUC__
214 void _log_fn(int severity, const char *fn, const char *format, ...)
216 va_list ap;
217 va_start(ap,format);
218 logv(severity, fn, format, ap);
219 va_end(ap);
221 #else
222 const char *_log_fn_function_name=NULL;
223 void _log_fn(int severity, const char *format, ...)
225 va_list ap;
226 va_start(ap,format);
227 logv(severity, _log_fn_function_name, format, ap);
228 va_end(ap);
229 _log_fn_function_name = NULL;
231 #endif
233 /** Close all open log files. */
234 void close_logs()
236 logfile_t *victim;
237 while(logfiles) {
238 victim = logfiles;
239 logfiles = logfiles->next;
240 close_log(victim);
241 tor_free(victim->filename);
242 tor_free(victim);
246 /** Close and re-open all log files; used to rotate logs on SIGHUP. */
247 void reset_logs()
249 logfile_t *lf = logfiles;
250 while(lf) {
251 if (reset_log(lf)) {
252 /* error. don't log it. delete the log entry and continue. */
253 logfile_t *victim = lf;
254 lf = victim->next;
255 delete_log(victim);
256 continue;
258 lf = lf->next;
262 /** Remove and free the log entry <b>victim</b> from the linked-list
263 * logfiles (it must be present in the list when this function is
264 * called). After this function is called, the caller shouldn't refer
265 * to <b>victim</b> anymore.
267 static void delete_log(logfile_t *victim) {
268 logfile_t *tmpl;
269 if(victim == logfiles)
270 logfiles = victim->next;
271 else {
272 for(tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
273 tor_assert(tmpl);
274 tor_assert(tmpl->next == victim);
275 tmpl->next = victim->next;
277 tor_free(victim->filename);
278 tor_free(victim);
281 /** DOCDOC */
282 static void close_log(logfile_t *victim)
284 if (victim->needs_close && victim->file) {
285 fclose(victim->file);
286 } else if (victim->is_syslog) {
287 #ifdef HAVE_SYSLOG_H
288 if (--syslog_count == 0)
289 /* There are no other syslogs; close the logging facility. */
290 closelog();
291 #endif
295 /** DOCDOC */
296 static int reset_log(logfile_t *lf)
298 if (lf->needs_close) {
299 if (fclose(lf->file)==EOF ||
300 !(lf->file = fopen(lf->filename, "a"))) {
301 return -1;
302 } else {
303 if (log_tor_version(lf, 1) < 0)
304 return -1;
307 return 0;
310 /** Add a log handler to send all messages of severity <b>loglevel</b>
311 * or higher to <b>stream</b>. */
312 void add_stream_log(int loglevelMin, int loglevelMax, const char *name, FILE *stream)
314 logfile_t *lf;
315 lf = tor_malloc_zero(sizeof(logfile_t));
316 lf->filename = tor_strdup(name);
317 lf->loglevel = loglevelMin;
318 lf->max_loglevel = loglevelMax;
319 lf->file = stream;
320 lf->next = logfiles;
321 logfiles = lf;
324 /** Add a log handler to receive messages during startup (before the real
325 * logs are initialized).
327 void add_temp_log(void)
329 add_stream_log(LOG_INFO, LOG_ERR, "<temp>", stdout);
330 logfiles->is_temporary = 1;
333 int add_callback_log(int loglevelMin, int loglevelMax, log_callback cb)
335 logfile_t *lf;
336 lf = tor_malloc_zero(sizeof(logfile_t));
337 lf->loglevel = loglevelMin;
338 lf->max_loglevel = loglevelMax;
339 lf->filename = tor_strdup("<callback>");
340 lf->callback = cb;
341 lf->next = logfiles;
342 logfiles = lf;
343 return 0;
346 /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
347 void close_temp_logs(void)
349 logfile_t *lf, **p;
350 for (p = &logfiles; *p; ) {
351 if ((*p)->is_temporary) {
352 lf = *p;
353 /* we use *p here to handle the edge case of the head of the list */
354 *p = (*p)->next;
355 close_log(lf);
356 tor_free(lf->filename);
357 tor_free(lf);
358 } else {
359 p = &((*p)->next);
364 /** Configure all log handles to be closed by close_temp_logs */
365 void mark_logs_temp(void)
367 logfile_t *lf;
368 for (lf = logfiles; lf; lf = lf->next)
369 lf->is_temporary = 1;
373 * Add a log handler to send messages to <b>filename</b>. If opening
374 * the logfile fails, -1 is returned and errno is set appropriately
375 * (by fopen).
377 int add_file_log(int loglevelMin, int loglevelMax, const char *filename)
379 FILE *f;
380 f = fopen(filename, "a");
381 if (!f) return -1;
382 add_stream_log(loglevelMin, loglevelMax, filename, f);
383 logfiles->needs_close = 1;
384 if (log_tor_version(logfiles, 0) < 0) {
385 logfile_t *victim = logfiles;
386 logfiles = victim->next;
387 delete_log(victim);
389 return 0;
392 #ifdef HAVE_SYSLOG_H
394 * Add a log handler to send messages to they system log facility.
396 int add_syslog_log(int loglevelMin, int loglevelMax)
398 logfile_t *lf;
399 if (syslog_count++ == 0)
400 /* This is the first syslog. */
401 openlog("Tor", LOG_NDELAY, LOG_DAEMON);
403 lf = tor_malloc_zero(sizeof(logfile_t));
404 lf->loglevel = loglevelMin;
405 lf->filename = tor_strdup("<syslog>");
406 lf->max_loglevel = loglevelMax;
407 lf->is_syslog = 1;
408 lf->next = logfiles;
409 logfiles = lf;
410 return 0;
412 #endif
414 /** If <b>level</b> is a valid log severity, return the corresponding
415 * numeric value. Otherwise, return -1. */
416 int parse_log_level(const char *level) {
417 if (!strcasecmp(level, "err"))
418 return LOG_ERR;
419 if (!strcasecmp(level, "warn"))
420 return LOG_WARN;
421 if (!strcasecmp(level, "notice"))
422 return LOG_NOTICE;
423 if (!strcasecmp(level, "info"))
424 return LOG_INFO;
425 if (!strcasecmp(level, "debug"))
426 return LOG_DEBUG;
427 return -1;
430 const char *log_level_to_string(int level)
432 return sev_to_string(level);
435 int get_min_log_level(void)
437 logfile_t *lf;
438 int min = LOG_ERR;
439 for (lf = logfiles; lf; lf = lf->next) {
440 if (lf->loglevel > min)
441 min = lf->loglevel;
443 return min;
446 /** Switch all logs to output at most verbose level. */
447 void switch_logs_debug(void)
449 logfile_t *lf;
450 for (lf = logfiles; lf; lf=lf->next) {
451 lf->loglevel = LOG_DEBUG;
456 Local Variables:
457 mode:c
458 indent-tabs-mode:nil
459 c-basic-offset:2
460 End: