1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
4 const char log_c_id
[] = "$Id$";
9 * \brief Functions to send messages to log files or the console.
23 #error "Tor requires libevent to build."
26 #define TRUNCATED_STR "[...truncated]"
27 #define TRUNCATED_STR_LEN 14
29 /** Information for a single logfile; only used in log.c */
30 typedef struct logfile_t
{
31 struct logfile_t
*next
; /**< Next logfile_t in the linked list. */
32 char *filename
; /**< Filename to open. */
33 FILE *file
; /**< Stream to receive log messages. */
34 int needs_close
; /**< Boolean: true if the stream gets closed on shutdown. */
35 int loglevel
; /**< Lowest severity level to send to this stream. */
36 int max_loglevel
; /**< Highest severity level to send to this stream. */
37 int is_temporary
; /**< Boolean: close after initializing logging subsystem.*/
38 int is_syslog
; /**< Boolean: send messages to syslog. */
39 log_callback callback
; /**< If not NULL, send messages to this function. */
42 /** Helper: map a log severity to descriptive string. */
43 static INLINE
const char *sev_to_string(int severity
) {
45 case LOG_DEBUG
: return "debug";
46 case LOG_INFO
: return "info";
47 case LOG_NOTICE
: return "notice";
48 case LOG_WARN
: return "warn";
49 case LOG_ERR
: return "err";
50 default: assert(0); return "UNKNOWN";
54 /** Linked list of logfile_t. */
55 static logfile_t
*logfiles
= NULL
;
57 static int syslog_count
= 0;
60 static void delete_log(logfile_t
*victim
);
61 static void close_log(logfile_t
*victim
);
62 static int reset_log(logfile_t
*lf
);
64 /** Helper: Write the standard prefix for log lines to a
65 * <b>buf_len</b> character buffer in <b>buf</b>.
68 _log_prefix(char *buf
, size_t buf_len
, int severity
)
76 tor_gettimeofday(&now
);
77 t
= (time_t)now
.tv_sec
;
79 n
= strftime(buf
, buf_len
, "%b %d %H:%M:%S", tor_localtime_r(&t
, &tm
));
80 r
= tor_snprintf(buf
+n
, buf_len
-n
,
82 (long)now
.tv_usec
/ 1000, sev_to_string(severity
));
89 /** If lf refers to an actual file that we have just opened, and the file
90 * contains no data, log an "opening new logfile" message at the top.
92 * Return -1 if the log is broken and needs to be deleted, else return 0.
94 static int log_tor_version(logfile_t
*lf
, int reset
)
100 if (!lf
->needs_close
)
101 /* If it doesn't get closed, it isn't really a file. */
103 if (lf
->is_temporary
)
104 /* If it's temporary, it isn't really a file. */
107 is_new
= (ftello(lf
->file
) == 0);
109 is_new
= (ftell(lf
->file
) == 0);
111 if (reset
&& !is_new
)
112 /* We are resetting, but we aren't at the start of the file; no
113 * need to log again. */
115 n
= _log_prefix(buf
, sizeof(buf
), LOG_NOTICE
);
116 tor_snprintf(buf
+n
, sizeof(buf
)-n
,
117 "Tor %s opening %slog file.\n", VERSION
, is_new
?"new ":"");
118 if (fputs(buf
, lf
->file
) == EOF
||
119 fflush(lf
->file
) == EOF
) /* error */
120 return -1; /* failed */
124 /** Helper: Format a log message into a fixed-sized buffer. (This is
125 * factored out of <b>logv</b> so that we never format a message more
126 * than once.) Return a pointer to the first character of the message
127 * portion of the formatted string.
129 static INLINE
char *format_msg(char *buf
, size_t buf_len
,
130 int severity
, const char *funcname
,
131 const char *format
, va_list ap
)
137 tor_assert(buf_len
>= 2); /* prevent integer underflow */
138 buf_len
-= 2; /* subtract 2 characters so we have room for \n\0 */
140 n
= _log_prefix(buf
, buf_len
, severity
);
141 end_of_prefix
= buf
+n
;
144 r
= tor_snprintf(buf
+n
, buf_len
-n
, "%s(): ", funcname
);
151 r
= tor_vsnprintf(buf
+n
,buf_len
-n
,format
,ap
);
153 /* The message was too long; overwrite the end of the buffer with
154 * "[...truncated]" */
155 if (buf_len
>= TRUNCATED_STR_LEN
) {
156 int offset
= buf_len
-TRUNCATED_STR_LEN
;
157 /* We have an extra 2 characters after buf_len to hold the \n\0,
158 * so it's safe to add 1 to the size here. */
159 strlcpy(buf
+offset
, TRUNCATED_STR
, buf_len
-offset
+1);
161 /* Set 'n' to the end of the buffer, where we'll be writing \n\0.
162 * Since we already subtracted 2 from buf_len, this is safe.*/
169 return end_of_prefix
;
172 /** Helper: sends a message to the appropriate logfiles, at loglevel
173 * <b>severity</b>. If provided, <b>funcname</b> is prepended to the
174 * message. The actual message is derived as from tor_snprintf(format,ap).
177 logv(int severity
, const char *funcname
, const char *format
, va_list ap
)
182 char *end_of_prefix
=NULL
;
187 if (severity
> lf
->loglevel
|| severity
< lf
->max_loglevel
) {
191 if (! (lf
->file
|| lf
->is_syslog
|| lf
->callback
)) {
198 format_msg(buf
, sizeof(buf
), severity
, funcname
, format
, ap
);
203 syslog(severity
, "%s", end_of_prefix
);
207 } else if (lf
->callback
) {
208 lf
->callback(severity
, end_of_prefix
);
212 if (fputs(buf
, lf
->file
) == EOF
||
213 fflush(lf
->file
) == EOF
) { /* error */
214 /* don't log the error! Blow away this log entry and continue. */
215 logfile_t
*victim
= lf
;
224 /** Output a message to the log. */
225 void _log(int severity
, const char *format
, ...)
229 logv(severity
, NULL
, format
, ap
);
233 /** Output a message to the log, prefixed with a function name <b>fn</b>. */
235 void _log_fn(int severity
, const char *fn
, const char *format
, ...)
239 logv(severity
, fn
, format
, ap
);
243 const char *_log_fn_function_name
=NULL
;
244 void _log_fn(int severity
, const char *format
, ...)
248 logv(severity
, _log_fn_function_name
, format
, ap
);
250 _log_fn_function_name
= NULL
;
254 /** Close all open log files. */
260 logfiles
= logfiles
->next
;
262 tor_free(victim
->filename
);
267 /** Close and re-open all log files; used to rotate logs on SIGHUP. */
270 logfile_t
*lf
= logfiles
;
273 /* error. don't log it. delete the log entry and continue. */
274 logfile_t
*victim
= lf
;
283 /** Remove and free the log entry <b>victim</b> from the linked-list
284 * logfiles (it must be present in the list when this function is
285 * called). After this function is called, the caller shouldn't refer
286 * to <b>victim</b> anymore.
288 static void delete_log(logfile_t
*victim
) {
290 if (victim
== logfiles
)
291 logfiles
= victim
->next
;
293 for (tmpl
= logfiles
; tmpl
&& tmpl
->next
!= victim
; tmpl
=tmpl
->next
) ;
295 tor_assert(tmpl
->next
== victim
);
296 tmpl
->next
= victim
->next
;
298 tor_free(victim
->filename
);
302 /** Helper: release system resources (but not memory) held by a single
304 static void close_log(logfile_t
*victim
)
306 if (victim
->needs_close
&& victim
->file
) {
307 fclose(victim
->file
);
308 } else if (victim
->is_syslog
) {
310 if (--syslog_count
== 0)
311 /* There are no other syslogs; close the logging facility. */
317 /** Helper: reset a single logfile_t. For a file log, this involves
318 * closing and reopening the log, and maybe writing the version. For
319 * other log types, do nothing. */
320 static int reset_log(logfile_t
*lf
)
322 if (lf
->needs_close
) {
323 if (fclose(lf
->file
)==EOF
||
324 !(lf
->file
= fopen(lf
->filename
, "a"))) {
327 if (log_tor_version(lf
, 1) < 0)
334 /** Add a log handler to send all messages of severity <b>loglevel</b>
335 * or higher to <b>stream</b>. */
336 void add_stream_log(int loglevelMin
, int loglevelMax
, const char *name
, FILE *stream
)
339 lf
= tor_malloc_zero(sizeof(logfile_t
));
340 lf
->filename
= tor_strdup(name
);
341 lf
->loglevel
= loglevelMin
;
342 lf
->max_loglevel
= loglevelMax
;
348 /** Add a log handler to receive messages during startup (before the real
349 * logs are initialized).
351 void add_temp_log(void)
353 add_stream_log(LOG_NOTICE
, LOG_ERR
, "<temp>", stdout
);
354 logfiles
->is_temporary
= 1;
358 * Add a log handler to send messages of severity between
359 * <b>logLevelmin</b> and <b>logLevelMax</b> to the function
362 int add_callback_log(int loglevelMin
, int loglevelMax
, log_callback cb
)
365 lf
= tor_malloc_zero(sizeof(logfile_t
));
366 lf
->loglevel
= loglevelMin
;
367 lf
->max_loglevel
= loglevelMax
;
368 lf
->filename
= tor_strdup("<callback>");
375 /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
376 void close_temp_logs(void)
379 for (p
= &logfiles
; *p
; ) {
380 if ((*p
)->is_temporary
) {
382 /* we use *p here to handle the edge case of the head of the list */
385 tor_free(lf
->filename
);
393 /** Configure all log handles to be closed by close_temp_logs */
394 void mark_logs_temp(void)
397 for (lf
= logfiles
; lf
; lf
= lf
->next
)
398 lf
->is_temporary
= 1;
402 * Add a log handler to send messages to <b>filename</b>. If opening
403 * the logfile fails, -1 is returned and errno is set appropriately
406 int add_file_log(int loglevelMin
, int loglevelMax
, const char *filename
)
409 f
= fopen(filename
, "a");
411 add_stream_log(loglevelMin
, loglevelMax
, filename
, f
);
412 logfiles
->needs_close
= 1;
413 if (log_tor_version(logfiles
, 0) < 0) {
414 delete_log(logfiles
);
421 * Add a log handler to send messages to they system log facility.
423 int add_syslog_log(int loglevelMin
, int loglevelMax
)
426 if (syslog_count
++ == 0)
427 /* This is the first syslog. */
428 openlog("Tor", LOG_NDELAY
, LOG_DAEMON
);
430 lf
= tor_malloc_zero(sizeof(logfile_t
));
431 lf
->loglevel
= loglevelMin
;
432 lf
->filename
= tor_strdup("<syslog>");
433 lf
->max_loglevel
= loglevelMax
;
441 /** If <b>level</b> is a valid log severity, return the corresponding
442 * numeric value. Otherwise, return -1. */
443 int parse_log_level(const char *level
) {
444 if (!strcasecmp(level
, "err"))
446 if (!strcasecmp(level
, "warn"))
448 if (!strcasecmp(level
, "notice"))
450 if (!strcasecmp(level
, "info"))
452 if (!strcasecmp(level
, "debug"))
457 /** Return the string equivalent of a given log level. */
458 const char *log_level_to_string(int level
)
460 return sev_to_string(level
);
463 /** Return the least severe log level that any current log is interested in. */
464 int get_min_log_level(void)
468 for (lf
= logfiles
; lf
; lf
= lf
->next
) {
469 if (lf
->loglevel
> min
)
475 /** Switch all logs to output at most verbose level. */
476 void switch_logs_debug(void)
479 for (lf
= logfiles
; lf
; lf
=lf
->next
) {
480 lf
->loglevel
= LOG_DEBUG
;
484 #ifdef HAVE_EVENT_SET_LOG_CALLBACK
485 static const char *suppress_msg
= NULL
;
486 void libevent_logging_callback(int severity
, const char *msg
)
488 if (suppress_msg
&& strstr(msg
, suppress_msg
))
491 case _EVENT_LOG_DEBUG
:
492 log(LOG_DEBUG
, "Message from libevent: %s", msg
);
495 log(LOG_INFO
, "Message from libevent: %s", msg
);
497 case _EVENT_LOG_WARN
:
498 log(LOG_WARN
, "Warning from libevent: %s", msg
);
501 log(LOG_ERR
, "Error from libevent: %s", msg
);
504 log(LOG_WARN
, "Message [%d] from libevent: %s", severity
, msg
);
508 void configure_libevent_logging(void)
510 event_set_log_callback(libevent_logging_callback
);
512 void suppress_libevent_log_msg(const char *msg
)
517 void configure_libevent_logging(void) {}