1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
6 const char log_c_id
[] = "$Id$";
10 * \brief Functions to send messages to log files or the console.
19 #ifdef HAVE_SYS_TIME_H
31 #error "Tor requires libevent to build."
34 #define TRUNCATED_STR "[...truncated]"
35 #define TRUNCATED_STR_LEN 14
37 /** Information for a single logfile; only used in log.c */
38 typedef struct logfile_t
{
39 struct logfile_t
*next
; /**< Next logfile_t in the linked list. */
40 char *filename
; /**< Filename to open. */
41 FILE *file
; /**< Stream to receive log messages. */
42 int needs_close
; /**< Boolean: true if the stream gets closed on shutdown. */
43 int min_loglevel
; /**< Lowest severity level to send to this stream. */
44 int max_loglevel
; /**< Highest severity level to send to this stream. */
45 int is_temporary
; /**< Boolean: close after initializing logging subsystem.*/
46 int is_syslog
; /**< Boolean: send messages to syslog. */
47 log_callback callback
; /**< If not NULL, send messages to this function. */
50 /** Helper: map a log severity to descriptive string. */
51 static INLINE
const char *
52 sev_to_string(int severity
)
55 case LOG_DEBUG
: return "debug";
56 case LOG_INFO
: return "info";
57 case LOG_NOTICE
: return "notice";
58 case LOG_WARN
: return "warn";
59 case LOG_ERR
: return "err";
60 default: assert(0); return "UNKNOWN";
64 /** Helper: decide whether to include the function name in the log message. */
66 should_log_function_name(uint32_t domain
, int severity
)
71 /* All debugging messages occur in interesting places. */
76 /* We care about places where bugs occur. */
77 return (domain
== LD_BUG
);
83 /** Linked list of logfile_t. */
84 static logfile_t
*logfiles
= NULL
;
86 static int syslog_count
= 0;
89 static void delete_log(logfile_t
*victim
);
90 static void close_log(logfile_t
*victim
);
92 /** Helper: Write the standard prefix for log lines to a
93 * <b>buf_len</b> character buffer in <b>buf</b>.
96 _log_prefix(char *buf
, size_t buf_len
, int severity
)
104 tor_gettimeofday(&now
);
105 t
= (time_t)now
.tv_sec
;
107 n
= strftime(buf
, buf_len
, "%b %d %H:%M:%S", tor_localtime_r(&t
, &tm
));
108 r
= tor_snprintf(buf
+n
, buf_len
-n
,
110 (long)now
.tv_usec
/ 1000, sev_to_string(severity
));
117 /** If lf refers to an actual file that we have just opened, and the file
118 * contains no data, log an "opening new logfile" message at the top.
120 * Return -1 if the log is broken and needs to be deleted, else return 0.
123 log_tor_version(logfile_t
*lf
, int reset
)
129 if (!lf
->needs_close
)
130 /* If it doesn't get closed, it isn't really a file. */
132 if (lf
->is_temporary
)
133 /* If it's temporary, it isn't really a file. */
136 is_new
= (ftello(lf
->file
) == 0);
138 is_new
= (ftell(lf
->file
) == 0);
140 if (reset
&& !is_new
)
141 /* We are resetting, but we aren't at the start of the file; no
142 * need to log again. */
144 n
= _log_prefix(buf
, sizeof(buf
), LOG_NOTICE
);
145 tor_snprintf(buf
+n
, sizeof(buf
)-n
,
146 "Tor %s opening %slog file.\n", VERSION
, is_new
?"new ":"");
147 if (fputs(buf
, lf
->file
) == EOF
||
148 fflush(lf
->file
) == EOF
) /* error */
149 return -1; /* failed */
153 /** Helper: Format a log message into a fixed-sized buffer. (This is
154 * factored out of <b>logv</b> so that we never format a message more
155 * than once.) Return a pointer to the first character of the message
156 * portion of the formatted string.
159 format_msg(char *buf
, size_t buf_len
,
160 uint32_t domain
, int severity
, const char *funcname
,
161 const char *format
, va_list ap
)
167 tor_assert(buf_len
>= 2); /* prevent integer underflow */
168 buf_len
-= 2; /* subtract 2 characters so we have room for \n\0 */
170 n
= _log_prefix(buf
, buf_len
, severity
);
171 end_of_prefix
= buf
+n
;
173 if (funcname
&& should_log_function_name(domain
, severity
)) {
174 r
= tor_snprintf(buf
+n
, buf_len
-n
, "%s(): ", funcname
);
181 r
= tor_vsnprintf(buf
+n
,buf_len
-n
,format
,ap
);
183 /* The message was too long; overwrite the end of the buffer with
184 * "[...truncated]" */
185 if (buf_len
>= TRUNCATED_STR_LEN
) {
186 int offset
= buf_len
-TRUNCATED_STR_LEN
;
187 /* We have an extra 2 characters after buf_len to hold the \n\0,
188 * so it's safe to add 1 to the size here. */
189 strlcpy(buf
+offset
, TRUNCATED_STR
, buf_len
-offset
+1);
191 /* Set 'n' to the end of the buffer, where we'll be writing \n\0.
192 * Since we already subtracted 2 from buf_len, this is safe.*/
199 return end_of_prefix
;
202 /** Helper: sends a message to the appropriate logfiles, at loglevel
203 * <b>severity</b>. If provided, <b>funcname</b> is prepended to the
204 * message. The actual message is derived as from tor_snprintf(format,ap).
207 logv(int severity
, uint32_t domain
, const char *funcname
, const char *format
,
213 char *end_of_prefix
=NULL
;
218 if (severity
> lf
->min_loglevel
|| severity
< lf
->max_loglevel
) {
222 if (! (lf
->file
|| lf
->is_syslog
|| lf
->callback
)) {
229 format_msg(buf
, sizeof(buf
), domain
, severity
, funcname
, format
, ap
);
234 /* XXXX Some syslog implementations have scary limits on the length of
235 * what you can pass them. Can/should we detect this? */
236 syslog(severity
, "%s", end_of_prefix
);
240 } else if (lf
->callback
) {
241 lf
->callback(severity
, domain
, end_of_prefix
);
245 if (fputs(buf
, lf
->file
) == EOF
||
246 fflush(lf
->file
) == EOF
) { /* error */
247 /* don't log the error! Blow away this log entry and continue. */
248 logfile_t
*victim
= lf
;
257 /** Output a message to the log. */
259 _log(int severity
, uint32_t domain
, const char *format
, ...)
263 logv(severity
, domain
, NULL
, format
, ap
);
267 /** Output a message to the log, prefixed with a function name <b>fn</b>. */
270 _log_fn(int severity
, uint32_t domain
, const char *fn
, const char *format
, ...)
274 logv(severity
, domain
, fn
, format
, ap
);
278 const char *_log_fn_function_name
=NULL
;
280 _log_fn(int severity
, uint32_t domain
, const char *format
, ...)
284 logv(severity
, domain
, _log_fn_function_name
, format
, ap
);
286 _log_fn_function_name
= NULL
;
289 _log_debug(uint32_t domain
, const char *format
, ...)
293 logv(LOG_DEBUG
, domain
, _log_fn_function_name
, format
, ap
);
295 _log_fn_function_name
= NULL
;
298 _log_info(uint32_t domain
, const char *format
, ...)
302 logv(LOG_INFO
, domain
, _log_fn_function_name
, format
, ap
);
304 _log_fn_function_name
= NULL
;
307 _log_notice(uint32_t domain
, const char *format
, ...)
311 logv(LOG_NOTICE
, domain
, _log_fn_function_name
, format
, ap
);
313 _log_fn_function_name
= NULL
;
316 _log_warn(uint32_t domain
, const char *format
, ...)
320 logv(LOG_WARN
, domain
, _log_fn_function_name
, format
, ap
);
322 _log_fn_function_name
= NULL
;
325 _log_err(uint32_t domain
, const char *format
, ...)
329 logv(LOG_ERR
, domain
, _log_fn_function_name
, format
, ap
);
331 _log_fn_function_name
= NULL
;
335 /** Close all open log files. */
339 logfile_t
*victim
, *next
;
346 tor_free(victim
->filename
);
351 /** Remove and free the log entry <b>victim</b> from the linked-list
352 * logfiles (it is probably present, but it might not be due to thread
353 * racing issues). After this function is called, the caller shouldn't
354 * refer to <b>victim</b> anymore.
356 * Long-term, we need to do something about races in the log subsystem
357 * in general. See bug 222 for more details.
360 delete_log(logfile_t
*victim
)
363 if (victim
== logfiles
)
364 logfiles
= victim
->next
;
366 for (tmpl
= logfiles
; tmpl
&& tmpl
->next
!= victim
; tmpl
=tmpl
->next
) ;
368 // tor_assert(tmpl->next == victim);
371 tmpl
->next
= victim
->next
;
373 tor_free(victim
->filename
);
377 /** Helper: release system resources (but not memory) held by a single
380 close_log(logfile_t
*victim
)
382 if (victim
->needs_close
&& victim
->file
) {
383 fclose(victim
->file
);
384 } else if (victim
->is_syslog
) {
386 if (--syslog_count
== 0) {
387 /* There are no other syslogs; close the logging facility. */
394 /** Add a log handler to send all messages of severity <b>loglevel</b>
395 * or higher to <b>stream</b>. */
397 add_stream_log(int loglevelMin
, int loglevelMax
,
398 const char *name
, FILE *stream
)
401 lf
= tor_malloc_zero(sizeof(logfile_t
));
402 lf
->filename
= tor_strdup(name
);
403 lf
->min_loglevel
= loglevelMin
;
404 lf
->max_loglevel
= loglevelMax
;
410 /** Add a log handler to receive messages during startup (before the real
411 * logs are initialized).
416 add_stream_log(LOG_NOTICE
, LOG_ERR
, "<temp>", stdout
);
417 logfiles
->is_temporary
= 1;
421 * Add a log handler to send messages of severity between
422 * <b>logLevelmin</b> and <b>logLevelMax</b> to the function
426 add_callback_log(int loglevelMin
, int loglevelMax
, log_callback cb
)
429 lf
= tor_malloc_zero(sizeof(logfile_t
));
430 lf
->min_loglevel
= loglevelMin
;
431 lf
->max_loglevel
= loglevelMax
;
432 lf
->filename
= tor_strdup("<callback>");
439 /** Adjust the configured severity of any logs whose callback function is
442 change_callback_log_severity(int loglevelMin
, int loglevelMax
,
446 for (lf
= logfiles
; lf
; lf
= lf
->next
) {
447 if (lf
->callback
== cb
) {
448 lf
->min_loglevel
= loglevelMin
;
449 lf
->max_loglevel
= loglevelMax
;
454 /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
456 close_temp_logs(void)
459 for (p
= &logfiles
; *p
; ) {
460 if ((*p
)->is_temporary
) {
462 /* we use *p here to handle the edge case of the head of the list */
465 tor_free(lf
->filename
);
473 /** Make all currently temporary logs (set to be closed by close_temp_logs)
474 * live again, and close all non-temporary logs. */
476 rollback_log_changes(void)
479 for (lf
= logfiles
; lf
; lf
= lf
->next
)
480 lf
->is_temporary
= ! lf
->is_temporary
;
484 /** Configure all log handles to be closed by close_temp_logs */
489 for (lf
= logfiles
; lf
; lf
= lf
->next
)
490 lf
->is_temporary
= 1;
494 * Add a log handler to send messages to <b>filename</b>. If opening
495 * the logfile fails, -1 is returned and errno is set appropriately
499 add_file_log(int loglevelMin
, int loglevelMax
, const char *filename
)
502 f
= fopen(filename
, "a");
504 add_stream_log(loglevelMin
, loglevelMax
, filename
, f
);
505 logfiles
->needs_close
= 1;
506 if (log_tor_version(logfiles
, 0) < 0) {
507 delete_log(logfiles
);
514 * Add a log handler to send messages to they system log facility.
517 add_syslog_log(int loglevelMin
, int loglevelMax
)
520 if (syslog_count
++ == 0)
521 /* This is the first syslog. */
522 openlog("Tor", LOG_PID
| LOG_NDELAY
, LOGFACILITY
);
524 lf
= tor_malloc_zero(sizeof(logfile_t
));
525 lf
->min_loglevel
= loglevelMin
;
526 lf
->filename
= tor_strdup("<syslog>");
527 lf
->max_loglevel
= loglevelMax
;
535 /** If <b>level</b> is a valid log severity, return the corresponding
536 * numeric value. Otherwise, return -1. */
538 parse_log_level(const char *level
)
540 if (!strcasecmp(level
, "err"))
542 if (!strcasecmp(level
, "warn"))
544 if (!strcasecmp(level
, "notice"))
546 if (!strcasecmp(level
, "info"))
548 if (!strcasecmp(level
, "debug"))
553 /** Return the string equivalent of a given log level. */
555 log_level_to_string(int level
)
557 return sev_to_string(level
);
560 /** Return the least severe log level that any current log is interested in. */
562 get_min_log_level(void)
566 for (lf
= logfiles
; lf
; lf
= lf
->next
) {
567 if (lf
->min_loglevel
> min
)
568 min
= lf
->min_loglevel
;
573 /** Switch all logs to output at most verbose level. */
575 switch_logs_debug(void)
578 for (lf
= logfiles
; lf
; lf
=lf
->next
) {
579 lf
->min_loglevel
= LOG_DEBUG
;
583 #ifdef HAVE_EVENT_SET_LOG_CALLBACK
584 /** A string which, if it appears in a libevent log, should be ignored. */
585 static const char *suppress_msg
= NULL
;
586 /** Callback function passed to event_set_log() so we can intercept
587 * log messages from libevent. */
589 libevent_logging_callback(int severity
, const char *msg
)
593 if (suppress_msg
&& strstr(msg
, suppress_msg
))
595 n
= strlcpy(buf
, msg
, sizeof(buf
));
596 if (n
&& n
< sizeof(buf
) && buf
[n
-1] == '\n') {
600 case _EVENT_LOG_DEBUG
:
601 log(LOG_DEBUG
, LD_NET
, "Message from libevent: %s", buf
);
604 log(LOG_INFO
, LD_NET
, "Message from libevent: %s", buf
);
606 case _EVENT_LOG_WARN
:
607 log(LOG_WARN
, LD_GENERAL
, "Warning from libevent: %s", buf
);
610 log(LOG_ERR
, LD_GENERAL
, "Error from libevent: %s", buf
);
613 log(LOG_WARN
, LD_GENERAL
, "Message [%d] from libevent: %s",
618 /** Set hook to intercept log messages from libevent. */
620 configure_libevent_logging(void)
622 event_set_log_callback(libevent_logging_callback
);
624 /** Ignore any libevent log message that contains <b>msg</b>. */
626 suppress_libevent_log_msg(const char *msg
)
632 configure_libevent_logging(void)
636 suppress_libevent_log_msg(const char *msg
)
643 dump_log_info(logfile_t
*lf
)
648 printf("=== log into \"%s\" (%s-%s) (%stemporary)\n", lf
->filename
,
649 sev_to_string(lf
->min_loglevel
),
650 sev_to_string(lf
->max_loglevel
),
651 lf
->is_temporary
?"":"not ");
652 } else if (lf
->is_syslog
) {
653 printf("=== syslog (%s-%s) (%stemporary)\n",
654 sev_to_string(lf
->min_loglevel
),
655 sev_to_string(lf
->max_loglevel
),
656 lf
->is_temporary
?"":"not ");
658 printf("=== log (%s-%s) (%stemporary)\n",
659 sev_to_string(lf
->min_loglevel
),
660 sev_to_string(lf
->max_loglevel
),
661 lf
->is_temporary
?"":"not ");
669 printf("==== BEGIN LOGS ====\n");
670 for (lf
= logfiles
; lf
; lf
= lf
->next
)
672 printf("==== END LOGS ====\n");