1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
8 * \brief Functions to send messages to log files or the console.
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. */
35 /** Helper: map a log severity to descriptive string. */
36 static INLINE
const char *sev_to_string(int 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
;
50 static int syslog_count
= 0;
53 static void delete_log(logfile_t
*victim
);
54 static void close_log(logfile_t
*victim
);
55 static int reset_log(logfile_t
*lf
);
58 _log_prefix(char *buf
, size_t buf_len
, int severity
)
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
,
71 (long)now
.tv_usec
/ 1000, sev_to_string(severity
));
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
)
90 /* If it doesn't get closed, it isn't really a file. */
93 /* If it's temporary, it isn't really a file. */
96 is_new
= (ftello(lf
->file
) == 0);
98 is_new
= (ftell(lf
->file
) == 0);
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. */
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 */
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
)
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
;
131 r
= tor_snprintf(buf
+n
, buf_len
-n
, "%s(): ", funcname
);
138 r
= tor_vsnprintf(buf
+n
,buf_len
-n
,format
,ap
);
141 strlcpy(buf
+buf_len
-TRUNCATED_STR_LEN
-1, TRUNCATED_STR
,
142 buf_len
-(buf_len
-TRUNCATED_STR_LEN
-1));
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).
156 logv(int severity
, const char *funcname
, const char *format
, va_list ap
)
161 char *end_of_prefix
=NULL
;
166 if (severity
> lf
->loglevel
|| severity
< lf
->max_loglevel
) {
170 if (! (lf
->file
|| lf
->is_syslog
|| lf
->callback
)) {
177 format_msg(buf
, sizeof(buf
), severity
, funcname
, format
, ap
);
182 syslog(severity
, "%s", end_of_prefix
);
186 } else if (lf
->callback
) {
187 lf
->callback(severity
, end_of_prefix
);
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
;
203 /** Output a message to the log. */
204 void _log(int severity
, const char *format
, ...)
208 logv(severity
, NULL
, format
, ap
);
212 /** Output a message to the log, prefixed with a function name <b>fn</b>. */
214 void _log_fn(int severity
, const char *fn
, const char *format
, ...)
218 logv(severity
, fn
, format
, ap
);
222 const char *_log_fn_function_name
=NULL
;
223 void _log_fn(int severity
, const char *format
, ...)
227 logv(severity
, _log_fn_function_name
, format
, ap
);
229 _log_fn_function_name
= NULL
;
233 /** Close all open log files. */
239 logfiles
= logfiles
->next
;
241 tor_free(victim
->filename
);
246 /** Close and re-open all log files; used to rotate logs on SIGHUP. */
249 logfile_t
*lf
= logfiles
;
252 /* error. don't log it. delete the log entry and continue. */
253 logfile_t
*victim
= lf
;
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
) {
269 if(victim
== logfiles
)
270 logfiles
= victim
->next
;
272 for(tmpl
= logfiles
; tmpl
&& tmpl
->next
!= victim
; tmpl
=tmpl
->next
) ;
274 tor_assert(tmpl
->next
== victim
);
275 tmpl
->next
= victim
->next
;
277 tor_free(victim
->filename
);
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
) {
288 if (--syslog_count
== 0)
289 /* There are no other syslogs; close the logging facility. */
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"))) {
303 if (log_tor_version(lf
, 1) < 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
)
315 lf
= tor_malloc_zero(sizeof(logfile_t
));
316 lf
->filename
= tor_strdup(name
);
317 lf
->loglevel
= loglevelMin
;
318 lf
->max_loglevel
= loglevelMax
;
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
)
336 lf
= tor_malloc_zero(sizeof(logfile_t
));
337 lf
->loglevel
= loglevelMin
;
338 lf
->max_loglevel
= loglevelMax
;
339 lf
->filename
= tor_strdup("<callback>");
346 /** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
347 void close_temp_logs(void)
350 for (p
= &logfiles
; *p
; ) {
351 if ((*p
)->is_temporary
) {
353 /* we use *p here to handle the edge case of the head of the list */
356 tor_free(lf
->filename
);
364 /** Configure all log handles to be closed by close_temp_logs */
365 void mark_logs_temp(void)
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
377 int add_file_log(int loglevelMin
, int loglevelMax
, const char *filename
)
380 f
= fopen(filename
, "a");
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
;
394 * Add a log handler to send messages to they system log facility.
396 int add_syslog_log(int loglevelMin
, int loglevelMax
)
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
;
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"))
419 if (!strcasecmp(level
, "warn"))
421 if (!strcasecmp(level
, "notice"))
423 if (!strcasecmp(level
, "info"))
425 if (!strcasecmp(level
, "debug"))
430 const char *log_level_to_string(int level
)
432 return sev_to_string(level
);
435 int get_min_log_level(void)
439 for (lf
= logfiles
; lf
; lf
= lf
->next
) {
440 if (lf
->loglevel
> min
)
446 /** Switch all logs to output at most verbose level. */
447 void switch_logs_debug(void)
450 for (lf
= logfiles
; lf
; lf
=lf
->next
) {
451 lf
->loglevel
= LOG_DEBUG
;