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
);
71 /** Helper: Write the standard prefix for log lines to a
72 * <b>buf_len</b> character buffer in <b>buf</b>.
75 _log_prefix(char *buf
, size_t buf_len
, int severity
)
83 tor_gettimeofday(&now
);
84 t
= (time_t)now
.tv_sec
;
86 n
= strftime(buf
, buf_len
, "%b %d %H:%M:%S", tor_localtime_r(&t
, &tm
));
87 r
= tor_snprintf(buf
+n
, buf_len
-n
,
89 (long)now
.tv_usec
/ 1000, sev_to_string(severity
));
96 /** If lf refers to an actual file that we have just opened, and the file
97 * contains no data, log an "opening new logfile" message at the top.
99 * Return -1 if the log is broken and needs to be deleted, else return 0.
102 log_tor_version(logfile_t
*lf
, int reset
)
108 if (!lf
->needs_close
)
109 /* If it doesn't get closed, it isn't really a file. */
111 if (lf
->is_temporary
)
112 /* If it's temporary, it isn't really a file. */
115 is_new
= (ftello(lf
->file
) == 0);
117 is_new
= (ftell(lf
->file
) == 0);
119 if (reset
&& !is_new
)
120 /* We are resetting, but we aren't at the start of the file; no
121 * need to log again. */
123 n
= _log_prefix(buf
, sizeof(buf
), LOG_NOTICE
);
124 tor_snprintf(buf
+n
, sizeof(buf
)-n
,
125 "Tor %s opening %slog file.\n", VERSION
, is_new
?"new ":"");
126 if (fputs(buf
, lf
->file
) == EOF
||
127 fflush(lf
->file
) == EOF
) /* error */
128 return -1; /* failed */
132 /** Helper: Format a log message into a fixed-sized buffer. (This is
133 * factored out of <b>logv</b> so that we never format a message more
134 * than once.) Return a pointer to the first character of the message
135 * portion of the formatted string.
138 format_msg(char *buf
, size_t buf_len
,
139 int severity
, const char *funcname
,
140 const char *format
, va_list ap
)
146 tor_assert(buf_len
>= 2); /* prevent integer underflow */
147 buf_len
-= 2; /* subtract 2 characters so we have room for \n\0 */
149 n
= _log_prefix(buf
, buf_len
, severity
);
150 end_of_prefix
= buf
+n
;
153 r
= tor_snprintf(buf
+n
, buf_len
-n
, "%s(): ", funcname
);
160 r
= tor_vsnprintf(buf
+n
,buf_len
-n
,format
,ap
);
162 /* The message was too long; overwrite the end of the buffer with
163 * "[...truncated]" */
164 if (buf_len
>= TRUNCATED_STR_LEN
) {
165 int offset
= buf_len
-TRUNCATED_STR_LEN
;
166 /* We have an extra 2 characters after buf_len to hold the \n\0,
167 * so it's safe to add 1 to the size here. */
168 strlcpy(buf
+offset
, TRUNCATED_STR
, buf_len
-offset
+1);
170 /* Set 'n' to the end of the buffer, where we'll be writing \n\0.
171 * Since we already subtracted 2 from buf_len, this is safe.*/
178 return end_of_prefix
;
181 /** Helper: sends a message to the appropriate logfiles, at loglevel
182 * <b>severity</b>. If provided, <b>funcname</b> is prepended to the
183 * message. The actual message is derived as from tor_snprintf(format,ap).
186 logv(int severity
, uint32_t domain
, const char *funcname
, const char *format
,
192 char *end_of_prefix
=NULL
;
197 if (severity
> lf
->min_loglevel
|| severity
< lf
->max_loglevel
) {
201 if (! (lf
->file
|| lf
->is_syslog
|| lf
->callback
)) {
208 format_msg(buf
, sizeof(buf
), severity
, funcname
, format
, ap
);
213 syslog(severity
, "%s", end_of_prefix
);
217 } else if (lf
->callback
) {
218 lf
->callback(severity
, domain
, end_of_prefix
);
222 if (fputs(buf
, lf
->file
) == EOF
||
223 fflush(lf
->file
) == EOF
) { /* error */
224 /* don't log the error! Blow away this log entry and continue. */
225 logfile_t
*victim
= lf
;
234 /** Output a message to the log. */
236 _log(int severity
, uint32_t domain
, const char *format
, ...)
240 logv(severity
, domain
, NULL
, format
, ap
);
244 /** Output a message to the log, prefixed with a function name <b>fn</b>. */
247 _log_fn(int severity
, uint32_t domain
, const char *fn
, const char *format
, ...)
251 logv(severity
, domain
, fn
, format
, ap
);
255 const char *_log_fn_function_name
=NULL
;
257 _log_fn(int severity
, uint32_t domain
, const char *format
, ...)
261 logv(severity
, domain
, _log_fn_function_name
, format
, ap
);
263 _log_fn_function_name
= NULL
;
266 debug(uint32_t domain
, const char *format
, ...)
270 logv(LOG_DEBUG
, domain
, NULL
, format
, ap
);
272 _log_fn_function_name
= NULL
;
275 info(uint32_t domain
, const char *format
, ...)
279 logv(LOG_INFO
, domain
, NULL
, format
, ap
);
281 _log_fn_function_name
= NULL
;
284 notice(uint32_t domain
, const char *format
, ...)
288 logv(LOG_NOTICE
, domain
, NULL
, format
, ap
);
290 _log_fn_function_name
= NULL
;
293 warn(uint32_t domain
, const char *format
, ...)
297 logv(LOG_WARN
, domain
, NULL
, format
, ap
);
299 _log_fn_function_name
= NULL
;
302 err(uint32_t domain
, const char *format
, ...)
306 logv(LOG_ERR
, domain
, NULL
, format
, ap
);
308 _log_fn_function_name
= NULL
;
312 /** Close all open log files. */
319 logfiles
= logfiles
->next
;
321 tor_free(victim
->filename
);
326 /** Remove and free the log entry <b>victim</b> from the linked-list
327 * logfiles (it must be present in the list when this function is
328 * called). After this function is called, the caller shouldn't refer
329 * to <b>victim</b> anymore.
332 delete_log(logfile_t
*victim
)
335 if (victim
== logfiles
)
336 logfiles
= victim
->next
;
338 for (tmpl
= logfiles
; tmpl
&& tmpl
->next
!= victim
; tmpl
=tmpl
->next
) ;
340 tor_assert(tmpl
->next
== victim
);
341 tmpl
->next
= victim
->next
;
343 tor_free(victim
->filename
);
347 /** Helper: release system resources (but not memory) held by a single
350 close_log(logfile_t
*victim
)
352 if (victim
->needs_close
&& victim
->file
) {
353 fclose(victim
->file
);
354 } else if (victim
->is_syslog
) {
356 if (--syslog_count
== 0) {
357 /* There are no other syslogs; close the logging facility. */
364 /** Add a log handler to send all messages of severity <b>loglevel</b>
365 * or higher to <b>stream</b>. */
367 add_stream_log(int loglevelMin
, int loglevelMax
,
368 const char *name
, FILE *stream
)
371 lf
= tor_malloc_zero(sizeof(logfile_t
));
372 lf
->filename
= tor_strdup(name
);
373 lf
->min_loglevel
= loglevelMin
;
374 lf
->max_loglevel
= loglevelMax
;
380 /** Add a log handler to receive messages during startup (before the real
381 * logs are initialized).
386 add_stream_log(LOG_NOTICE
, LOG_ERR
, "<temp>", stdout
);
387 logfiles
->is_temporary
= 1;
391 * Add a log handler to send messages of severity between
392 * <b>logLevelmin</b> and <b>logLevelMax</b> to the function
396 add_callback_log(int loglevelMin
, int loglevelMax
, log_callback cb
)
399 lf
= tor_malloc_zero(sizeof(logfile_t
));
400 lf
->min_loglevel
= loglevelMin
;
401 lf
->max_loglevel
= loglevelMax
;
402 lf
->filename
= tor_strdup("<callback>");
410 change_callback_log_severity(int loglevelMin
, int loglevelMax
,
414 for (lf
= logfiles
; lf
; lf
= lf
->next
) {
415 if (lf
->callback
== cb
) {
416 lf
->min_loglevel
= loglevelMin
;
417 lf
->max_loglevel
= loglevelMax
;
422 /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
424 close_temp_logs(void)
427 for (p
= &logfiles
; *p
; ) {
428 if ((*p
)->is_temporary
) {
430 /* we use *p here to handle the edge case of the head of the list */
433 tor_free(lf
->filename
);
441 /** Make all currently temporary logs (set to be closed by close_temp_logs)
442 * live again, and close all non-temporary logs. */
444 rollback_log_changes(void)
447 for (lf
= logfiles
; lf
; lf
= lf
->next
)
448 lf
->is_temporary
= ! lf
->is_temporary
;
452 /** Configure all log handles to be closed by close_temp_logs */
457 for (lf
= logfiles
; lf
; lf
= lf
->next
)
458 lf
->is_temporary
= 1;
462 * Add a log handler to send messages to <b>filename</b>. If opening
463 * the logfile fails, -1 is returned and errno is set appropriately
467 add_file_log(int loglevelMin
, int loglevelMax
, const char *filename
)
470 f
= fopen(filename
, "a");
472 add_stream_log(loglevelMin
, loglevelMax
, filename
, f
);
473 logfiles
->needs_close
= 1;
474 if (log_tor_version(logfiles
, 0) < 0) {
475 delete_log(logfiles
);
482 * Add a log handler to send messages to they system log facility.
485 add_syslog_log(int loglevelMin
, int loglevelMax
)
488 if (syslog_count
++ == 0)
489 /* This is the first syslog. */
490 openlog("Tor", LOG_PID
| LOG_NDELAY
, LOGFACILITY
);
492 lf
= tor_malloc_zero(sizeof(logfile_t
));
493 lf
->min_loglevel
= loglevelMin
;
494 lf
->filename
= tor_strdup("<syslog>");
495 lf
->max_loglevel
= loglevelMax
;
503 /** If <b>level</b> is a valid log severity, return the corresponding
504 * numeric value. Otherwise, return -1. */
506 parse_log_level(const char *level
)
508 if (!strcasecmp(level
, "err"))
510 if (!strcasecmp(level
, "warn"))
512 if (!strcasecmp(level
, "notice"))
514 if (!strcasecmp(level
, "info"))
516 if (!strcasecmp(level
, "debug"))
521 /** Return the string equivalent of a given log level. */
523 log_level_to_string(int level
)
525 return sev_to_string(level
);
528 /** Return the least severe log level that any current log is interested in. */
530 get_min_log_level(void)
534 for (lf
= logfiles
; lf
; lf
= lf
->next
) {
535 if (lf
->min_loglevel
> min
)
536 min
= lf
->min_loglevel
;
541 /** Switch all logs to output at most verbose level. */
543 switch_logs_debug(void)
546 for (lf
= logfiles
; lf
; lf
=lf
->next
) {
547 lf
->min_loglevel
= LOG_DEBUG
;
551 #ifdef HAVE_EVENT_SET_LOG_CALLBACK
552 static const char *suppress_msg
= NULL
;
554 libevent_logging_callback(int severity
, const char *msg
)
558 if (suppress_msg
&& strstr(msg
, suppress_msg
))
560 n
= strlcpy(buf
, msg
, sizeof(buf
));
561 if (n
&& n
< sizeof(buf
) && buf
[n
-1] == '\n') {
565 case _EVENT_LOG_DEBUG
:
566 log(LOG_DEBUG
, LD_NET
, "Message from libevent: %s", buf
);
569 log(LOG_INFO
, LD_NET
, "Message from libevent: %s", buf
);
571 case _EVENT_LOG_WARN
:
572 log(LOG_WARN
, LD_GENERAL
, "Warning from libevent: %s", buf
);
575 log(LOG_ERR
, LD_GENERAL
, "Error from libevent: %s", buf
);
578 log(LOG_WARN
, LD_GENERAL
, "Message [%d] from libevent: %s",
584 configure_libevent_logging(void)
586 event_set_log_callback(libevent_logging_callback
);
589 suppress_libevent_log_msg(const char *msg
)
595 configure_libevent_logging(void)
599 suppress_libevent_log_msg(const char *msg
)
606 dump_log_info(logfile_t
*lf
)
611 printf("=== log into \"%s\" (%s-%s) (%stemporary)\n", lf
->filename
,
612 sev_to_string(lf
->min_loglevel
),
613 sev_to_string(lf
->max_loglevel
),
614 lf
->is_temporary
?"":"not ");
615 } else if (lf
->is_syslog
) {
616 printf("=== syslog (%s-%s) (%stemporary)\n",
617 sev_to_string(lf
->min_loglevel
),
618 sev_to_string(lf
->max_loglevel
),
619 lf
->is_temporary
?"":"not ");
621 printf("=== log (%s-%s) (%stemporary)\n",
622 sev_to_string(lf
->min_loglevel
),
623 sev_to_string(lf
->max_loglevel
),
624 lf
->is_temporary
?"":"not ");
632 printf("==== BEGIN LOGS ====\n");
633 for (lf
= logfiles
; lf
; lf
= lf
->next
)
635 printf("==== END LOGS ====\n");