1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar.
2 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
3 /* See LICENSE for licensing information */
5 const char log_c_id
[] = "$Id$";
9 * \brief Functions to send messages to log files or the console.
17 #ifdef HAVE_SYS_TIME_H
29 #error "Tor requires libevent to build."
32 #define TRUNCATED_STR "[...truncated]"
33 #define TRUNCATED_STR_LEN 14
35 /** Information for a single logfile; only used in log.c */
36 typedef struct logfile_t
{
37 struct logfile_t
*next
; /**< Next logfile_t in the linked list. */
38 char *filename
; /**< Filename to open. */
39 FILE *file
; /**< Stream to receive log messages. */
40 int needs_close
; /**< Boolean: true if the stream gets closed on shutdown. */
41 int min_loglevel
; /**< Lowest severity level to send to this stream. */
42 int max_loglevel
; /**< Highest severity level to send to this stream. */
43 int is_temporary
; /**< Boolean: close after initializing logging subsystem.*/
44 int is_syslog
; /**< Boolean: send messages to syslog. */
45 log_callback callback
; /**< If not NULL, send messages to this function. */
48 /** Helper: map a log severity to descriptive string. */
49 static INLINE
const char *
50 sev_to_string(int severity
)
53 case LOG_DEBUG
: return "debug";
54 case LOG_INFO
: return "info";
55 case LOG_NOTICE
: return "notice";
56 case LOG_WARN
: return "warn";
57 case LOG_ERR
: return "err";
58 default: assert(0); return "UNKNOWN";
62 /** Linked list of logfile_t. */
63 static logfile_t
*logfiles
= NULL
;
65 static int syslog_count
= 0;
68 static void delete_log(logfile_t
*victim
);
69 static void close_log(logfile_t
*victim
);
70 static int reset_log(logfile_t
*lf
);
72 /** Helper: Write the standard prefix for log lines to a
73 * <b>buf_len</b> character buffer in <b>buf</b>.
76 _log_prefix(char *buf
, size_t buf_len
, int severity
)
84 tor_gettimeofday(&now
);
85 t
= (time_t)now
.tv_sec
;
87 n
= strftime(buf
, buf_len
, "%b %d %H:%M:%S", tor_localtime_r(&t
, &tm
));
88 r
= tor_snprintf(buf
+n
, buf_len
-n
,
90 (long)now
.tv_usec
/ 1000, sev_to_string(severity
));
97 /** If lf refers to an actual file that we have just opened, and the file
98 * contains no data, log an "opening new logfile" message at the top.
100 * Return -1 if the log is broken and needs to be deleted, else return 0.
103 log_tor_version(logfile_t
*lf
, int reset
)
109 if (!lf
->needs_close
)
110 /* If it doesn't get closed, it isn't really a file. */
112 if (lf
->is_temporary
)
113 /* If it's temporary, it isn't really a file. */
116 is_new
= (ftello(lf
->file
) == 0);
118 is_new
= (ftell(lf
->file
) == 0);
120 if (reset
&& !is_new
)
121 /* We are resetting, but we aren't at the start of the file; no
122 * need to log again. */
124 n
= _log_prefix(buf
, sizeof(buf
), LOG_NOTICE
);
125 tor_snprintf(buf
+n
, sizeof(buf
)-n
,
126 "Tor %s opening %slog file.\n", VERSION
, is_new
?"new ":"");
127 if (fputs(buf
, lf
->file
) == EOF
||
128 fflush(lf
->file
) == EOF
) /* error */
129 return -1; /* failed */
133 /** Helper: Format a log message into a fixed-sized buffer. (This is
134 * factored out of <b>logv</b> so that we never format a message more
135 * than once.) Return a pointer to the first character of the message
136 * portion of the formatted string.
139 format_msg(char *buf
, size_t buf_len
,
140 int severity
, const char *funcname
,
141 const char *format
, va_list ap
)
147 tor_assert(buf_len
>= 2); /* prevent integer underflow */
148 buf_len
-= 2; /* subtract 2 characters so we have room for \n\0 */
150 n
= _log_prefix(buf
, buf_len
, severity
);
151 end_of_prefix
= buf
+n
;
154 r
= tor_snprintf(buf
+n
, buf_len
-n
, "%s(): ", funcname
);
161 r
= tor_vsnprintf(buf
+n
,buf_len
-n
,format
,ap
);
163 /* The message was too long; overwrite the end of the buffer with
164 * "[...truncated]" */
165 if (buf_len
>= TRUNCATED_STR_LEN
) {
166 int offset
= buf_len
-TRUNCATED_STR_LEN
;
167 /* We have an extra 2 characters after buf_len to hold the \n\0,
168 * so it's safe to add 1 to the size here. */
169 strlcpy(buf
+offset
, TRUNCATED_STR
, buf_len
-offset
+1);
171 /* Set 'n' to the end of the buffer, where we'll be writing \n\0.
172 * Since we already subtracted 2 from buf_len, this is safe.*/
179 return end_of_prefix
;
182 /** Helper: sends a message to the appropriate logfiles, at loglevel
183 * <b>severity</b>. If provided, <b>funcname</b> is prepended to the
184 * message. The actual message is derived as from tor_snprintf(format,ap).
187 logv(int severity
, uint32_t domain
, const char *funcname
, const char *format
,
193 char *end_of_prefix
=NULL
;
198 if (severity
> lf
->min_loglevel
|| severity
< lf
->max_loglevel
) {
202 if (! (lf
->file
|| lf
->is_syslog
|| lf
->callback
)) {
209 format_msg(buf
, sizeof(buf
), severity
, funcname
, format
, ap
);
214 syslog(severity
, "%s", end_of_prefix
);
218 } else if (lf
->callback
) {
219 lf
->callback(severity
, domain
, end_of_prefix
);
223 if (fputs(buf
, lf
->file
) == EOF
||
224 fflush(lf
->file
) == EOF
) { /* error */
225 /* don't log the error! Blow away this log entry and continue. */
226 logfile_t
*victim
= lf
;
235 /** Output a message to the log. */
237 _log(int severity
, uint32_t domain
, const char *format
, ...)
241 logv(severity
, domain
, NULL
, format
, ap
);
245 /** Output a message to the log, prefixed with a function name <b>fn</b>. */
248 _log_fn(int severity
, uint32_t domain
, const char *fn
, const char *format
, ...)
252 logv(severity
, domain
, fn
, format
, ap
);
256 const char *_log_fn_function_name
=NULL
;
258 _log_fn(int severity
, uint32_t domain
, const char *format
, ...)
262 logv(severity
, domain
, _log_fn_function_name
, format
, ap
);
264 _log_fn_function_name
= NULL
;
267 debug(uint32_t domain
, const char *format
, ...)
271 logv(LOG_DEBUG
, domain
, NULL
, format
, ap
);
273 _log_fn_function_name
= NULL
;
276 info(uint32_t domain
, const char *format
, ...)
280 logv(LOG_INFO
, domain
, NULL
, format
, ap
);
282 _log_fn_function_name
= NULL
;
285 notice(uint32_t domain
, const char *format
, ...)
289 logv(LOG_NOTICE
, domain
, NULL
, format
, ap
);
291 _log_fn_function_name
= NULL
;
294 warn(uint32_t domain
, const char *format
, ...)
298 logv(LOG_WARN
, domain
, NULL
, format
, ap
);
300 _log_fn_function_name
= NULL
;
303 err(uint32_t domain
, const char *format
, ...)
307 logv(LOG_ERR
, domain
, NULL
, format
, ap
);
309 _log_fn_function_name
= NULL
;
313 /** Close all open log files. */
320 logfiles
= logfiles
->next
;
322 tor_free(victim
->filename
);
327 /** Close and re-open all log files; used to rotate logs on SIGHUP. */
331 logfile_t
*lf
= logfiles
;
334 /* error. don't log it. delete the log entry and continue. */
335 logfile_t
*victim
= lf
;
344 /** Remove and free the log entry <b>victim</b> from the linked-list
345 * logfiles (it must be present in the list when this function is
346 * called). After this function is called, the caller shouldn't refer
347 * to <b>victim</b> anymore.
350 delete_log(logfile_t
*victim
)
353 if (victim
== logfiles
)
354 logfiles
= victim
->next
;
356 for (tmpl
= logfiles
; tmpl
&& tmpl
->next
!= victim
; tmpl
=tmpl
->next
) ;
358 tor_assert(tmpl
->next
== victim
);
359 tmpl
->next
= victim
->next
;
361 tor_free(victim
->filename
);
365 /** Helper: release system resources (but not memory) held by a single
368 close_log(logfile_t
*victim
)
370 if (victim
->needs_close
&& victim
->file
) {
371 fclose(victim
->file
);
372 } else if (victim
->is_syslog
) {
374 if (--syslog_count
== 0) {
375 /* There are no other syslogs; close the logging facility. */
382 /** Helper: reset a single logfile_t. For a file log, this involves
383 * closing and reopening the log, and maybe writing the version. For
384 * other log types, do nothing. */
386 reset_log(logfile_t
*lf
)
388 if (lf
->needs_close
) {
389 if (fclose(lf
->file
)==EOF
||
390 !(lf
->file
= fopen(lf
->filename
, "a"))) {
393 if (log_tor_version(lf
, 1) < 0)
400 /** Add a log handler to send all messages of severity <b>loglevel</b>
401 * or higher to <b>stream</b>. */
403 add_stream_log(int loglevelMin
, int loglevelMax
, const char *name
, FILE *stream
)
406 lf
= tor_malloc_zero(sizeof(logfile_t
));
407 lf
->filename
= tor_strdup(name
);
408 lf
->min_loglevel
= loglevelMin
;
409 lf
->max_loglevel
= loglevelMax
;
415 /** Add a log handler to receive messages during startup (before the real
416 * logs are initialized).
421 add_stream_log(LOG_NOTICE
, LOG_ERR
, "<temp>", stdout
);
422 logfiles
->is_temporary
= 1;
426 * Add a log handler to send messages of severity between
427 * <b>logLevelmin</b> and <b>logLevelMax</b> to the function
431 add_callback_log(int loglevelMin
, int loglevelMax
, log_callback cb
)
434 lf
= tor_malloc_zero(sizeof(logfile_t
));
435 lf
->min_loglevel
= loglevelMin
;
436 lf
->max_loglevel
= loglevelMax
;
437 lf
->filename
= tor_strdup("<callback>");
445 change_callback_log_severity(int loglevelMin
, int loglevelMax
,
449 for (lf
= logfiles
; lf
; lf
= lf
->next
) {
450 if (lf
->callback
== cb
) {
451 lf
->min_loglevel
= loglevelMin
;
452 lf
->max_loglevel
= loglevelMax
;
457 /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
459 close_temp_logs(void)
462 for (p
= &logfiles
; *p
; ) {
463 if ((*p
)->is_temporary
) {
465 /* we use *p here to handle the edge case of the head of the list */
468 tor_free(lf
->filename
);
476 /** Configure all log handles to be closed by close_temp_logs */
481 for (lf
= logfiles
; lf
; lf
= lf
->next
)
482 lf
->is_temporary
= 1;
486 * Add a log handler to send messages to <b>filename</b>. If opening
487 * the logfile fails, -1 is returned and errno is set appropriately
491 add_file_log(int loglevelMin
, int loglevelMax
, const char *filename
)
494 f
= fopen(filename
, "a");
496 add_stream_log(loglevelMin
, loglevelMax
, filename
, f
);
497 logfiles
->needs_close
= 1;
498 if (log_tor_version(logfiles
, 0) < 0) {
499 delete_log(logfiles
);
506 * Add a log handler to send messages to they system log facility.
509 add_syslog_log(int loglevelMin
, int loglevelMax
)
512 if (syslog_count
++ == 0)
513 /* This is the first syslog. */
514 openlog("Tor", LOG_PID
| LOG_NDELAY
, LOG_DAEMON
);
516 lf
= tor_malloc_zero(sizeof(logfile_t
));
517 lf
->min_loglevel
= loglevelMin
;
518 lf
->filename
= tor_strdup("<syslog>");
519 lf
->max_loglevel
= loglevelMax
;
527 /** If <b>level</b> is a valid log severity, return the corresponding
528 * numeric value. Otherwise, return -1. */
530 parse_log_level(const char *level
)
532 if (!strcasecmp(level
, "err"))
534 if (!strcasecmp(level
, "warn"))
536 if (!strcasecmp(level
, "notice"))
538 if (!strcasecmp(level
, "info"))
540 if (!strcasecmp(level
, "debug"))
545 /** Return the string equivalent of a given log level. */
547 log_level_to_string(int level
)
549 return sev_to_string(level
);
552 /** Return the least severe log level that any current log is interested in. */
554 get_min_log_level(void)
558 for (lf
= logfiles
; lf
; lf
= lf
->next
) {
559 if (lf
->min_loglevel
> min
)
560 min
= lf
->min_loglevel
;
565 /** Switch all logs to output at most verbose level. */
567 switch_logs_debug(void)
570 for (lf
= logfiles
; lf
; lf
=lf
->next
) {
571 lf
->min_loglevel
= LOG_DEBUG
;
575 #ifdef HAVE_EVENT_SET_LOG_CALLBACK
576 static const char *suppress_msg
= NULL
;
578 libevent_logging_callback(int severity
, const char *msg
)
582 if (suppress_msg
&& strstr(msg
, suppress_msg
))
584 n
= strlcpy(buf
, msg
, sizeof(buf
));
585 if (n
&& n
< sizeof(buf
) && buf
[n
-1] == '\n') {
589 case _EVENT_LOG_DEBUG
:
590 log(LOG_DEBUG
, LD_NET
, "Message from libevent: %s", buf
);
593 log(LOG_INFO
, LD_NET
, "Message from libevent: %s", buf
);
595 case _EVENT_LOG_WARN
:
596 log(LOG_WARN
, LD_GENERAL
, "Warning from libevent: %s", buf
);
599 log(LOG_ERR
, LD_GENERAL
, "Error from libevent: %s", buf
);
602 log(LOG_WARN
, LD_GENERAL
, "Message [%d] from libevent: %s",
608 configure_libevent_logging(void)
610 event_set_log_callback(libevent_logging_callback
);
613 suppress_libevent_log_msg(const char *msg
)
619 configure_libevent_logging(void)
623 suppress_libevent_log_msg(const char *msg
)