2003-10-08 04:04:08 +02:00
|
|
|
/* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
|
2002-11-24 09:45:13 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2004-11-29 23:25:31 +01:00
|
|
|
const char log_c_id[] = "$Id$";
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/**
|
|
|
|
* \file log.c
|
|
|
|
*
|
|
|
|
* \brief Functions to send messages to log files or the console.
|
|
|
|
*/
|
|
|
|
|
2004-11-14 18:21:32 +01:00
|
|
|
#include "orconfig.h"
|
2004-04-03 04:40:30 +02:00
|
|
|
#include <stdarg.h>
|
2004-04-03 06:05:12 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2004-05-19 22:07:08 +02:00
|
|
|
#include <string.h>
|
2004-04-03 06:05:12 +02:00
|
|
|
#include "./util.h"
|
|
|
|
#include "./log.h"
|
|
|
|
|
2004-08-18 13:21:50 +02:00
|
|
|
#define TRUNCATED_STR "[...truncated]"
|
|
|
|
#define TRUNCATED_STR_LEN 14
|
2004-08-15 22:13:07 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Information for a single logfile; only used in log.c */
|
2003-09-16 19:58:36 +02:00
|
|
|
typedef struct logfile_t {
|
2004-05-10 12:27:54 +02:00
|
|
|
struct logfile_t *next; /**< Next logfile_t in the linked list. */
|
2004-05-20 21:47:28 +02:00
|
|
|
char *filename; /**< Filename to open. */
|
2004-05-10 12:27:54 +02:00
|
|
|
FILE *file; /**< Stream to receive log messages. */
|
2004-05-10 05:53:24 +02:00
|
|
|
int needs_close; /**< Boolean: true if the stream gets closed on shutdown. */
|
|
|
|
int loglevel; /**< Lowest severity level to send to this stream. */
|
|
|
|
int max_loglevel; /**< Highest severity level to send to this stream. */
|
2004-05-20 21:47:28 +02:00
|
|
|
int is_temporary; /**< Boolean: close after initializing logging subsystem.*/
|
2004-10-26 23:48:41 +02:00
|
|
|
int is_syslog; /**< Boolean: send messages to syslog. */
|
2004-11-03 22:53:12 +01:00
|
|
|
log_callback callback; /**< If not NULL, send messages to this function. */
|
2003-09-16 19:58:36 +02:00
|
|
|
} logfile_t;
|
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** Helper: map a log severity to descriptive string. */
|
2003-09-16 19:58:36 +02:00
|
|
|
static INLINE const char *sev_to_string(int severity) {
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (severity) {
|
2003-06-25 06:47:54 +02:00
|
|
|
case LOG_DEBUG: return "debug";
|
|
|
|
case LOG_INFO: return "info";
|
2004-03-30 05:15:23 +02:00
|
|
|
case LOG_NOTICE: return "notice";
|
2003-10-10 03:48:03 +02:00
|
|
|
case LOG_WARN: return "warn";
|
2003-06-25 06:47:54 +02:00
|
|
|
case LOG_ERR: return "err";
|
2003-09-27 00:27:24 +02:00
|
|
|
default: assert(0); return "UNKNOWN";
|
2002-09-04 02:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** Linked list of logfile_t. */
|
2003-09-16 19:58:36 +02:00
|
|
|
static logfile_t *logfiles = NULL;
|
2004-10-26 23:48:41 +02:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
static int syslog_count = 0;
|
|
|
|
#endif
|
2003-06-21 21:03:22 +02:00
|
|
|
|
2004-09-23 06:59:02 +02:00
|
|
|
static void delete_log(logfile_t *victim);
|
2004-10-26 23:48:41 +02:00
|
|
|
static void close_log(logfile_t *victim);
|
|
|
|
static int reset_log(logfile_t *lf);
|
2004-09-23 06:59:02 +02:00
|
|
|
|
|
|
|
static INLINE size_t
|
|
|
|
_log_prefix(char *buf, size_t buf_len, int severity)
|
2002-06-27 00:45:49 +02:00
|
|
|
{
|
2002-09-04 02:39:33 +02:00
|
|
|
time_t t;
|
2002-11-24 09:45:13 +01:00
|
|
|
struct timeval now;
|
2005-02-22 08:03:03 +01:00
|
|
|
struct tm tm;
|
2004-03-09 23:01:17 +01:00
|
|
|
size_t n;
|
2004-10-27 08:37:34 +02:00
|
|
|
int r;
|
2003-09-16 19:58:36 +02:00
|
|
|
|
2003-10-04 05:29:09 +02:00
|
|
|
tor_gettimeofday(&now);
|
2003-09-16 19:58:36 +02:00
|
|
|
t = (time_t)now.tv_sec;
|
2003-12-14 10:58:43 +01:00
|
|
|
|
2005-02-22 08:03:03 +01:00
|
|
|
n = strftime(buf, buf_len, "%b %d %H:%M:%S", tor_localtime_r(&t, &tm));
|
2004-10-27 08:37:34 +02:00
|
|
|
r = tor_snprintf(buf+n, buf_len-n,
|
2003-12-17 22:14:13 +01:00
|
|
|
".%.3ld [%s] ",
|
2003-09-19 11:30:34 +02:00
|
|
|
(long)now.tv_usec / 1000, sev_to_string(severity));
|
2004-10-27 08:37:34 +02:00
|
|
|
if (r<0)
|
|
|
|
return buf_len-1;
|
|
|
|
else
|
|
|
|
return n+r;
|
2004-06-21 06:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** If lf refers to an actual file that we have just opened, and the file
|
2004-11-20 07:52:13 +01:00
|
|
|
* contains no data, log an "opening new logfile" message at the top.
|
|
|
|
*
|
|
|
|
* Return -1 if the log is broken and needs to be deleted, else return 0.
|
|
|
|
*/
|
|
|
|
static int log_tor_version(logfile_t *lf, int reset)
|
2004-06-21 06:37:27 +02:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
size_t n;
|
2004-06-30 18:35:11 +02:00
|
|
|
int is_new;
|
2004-06-21 06:37:27 +02:00
|
|
|
|
|
|
|
if (!lf->needs_close)
|
|
|
|
/* If it doesn't get closed, it isn't really a file. */
|
2004-11-20 07:52:13 +01:00
|
|
|
return 0;
|
2004-06-21 06:37:27 +02:00
|
|
|
if (lf->is_temporary)
|
|
|
|
/* If it's temporary, it isn't really a file. */
|
2004-11-20 07:52:13 +01:00
|
|
|
return 0;
|
2004-11-14 10:48:54 +01:00
|
|
|
#ifdef HAVE_FTELLO
|
2004-11-12 06:05:41 +01:00
|
|
|
is_new = (ftello(lf->file) == 0);
|
|
|
|
#else
|
2004-06-30 18:35:11 +02:00
|
|
|
is_new = (ftell(lf->file) == 0);
|
2004-11-12 06:05:41 +01:00
|
|
|
#endif
|
2004-06-30 18:35:11 +02:00
|
|
|
if (reset && !is_new)
|
|
|
|
/* We are resetting, but we aren't at the start of the file; no
|
|
|
|
* need to log again. */
|
2004-11-20 07:52:13 +01:00
|
|
|
return 0;
|
2004-10-27 08:37:34 +02:00
|
|
|
n = _log_prefix(buf, sizeof(buf), LOG_NOTICE);
|
|
|
|
tor_snprintf(buf+n, sizeof(buf)-n,
|
|
|
|
"Tor %s opening %slog file.\n", VERSION, is_new?"new ":"");
|
2004-11-24 09:42:06 +01:00
|
|
|
if (fputs(buf, lf->file) == EOF ||
|
2004-11-28 12:39:53 +01:00
|
|
|
fflush(lf->file) == EOF) /* error */
|
2004-11-20 07:52:13 +01:00
|
|
|
return -1; /* failed */
|
|
|
|
return 0;
|
2004-06-21 06:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper: Format a log message into a fixed-sized buffer. (This is
|
|
|
|
* factored out of <b>logv</b> so that we never format a message more
|
2004-10-26 23:48:41 +02:00
|
|
|
* than once.) Return a pointer to the first character of the message
|
|
|
|
* portion of the formatted string.
|
2004-06-21 06:37:27 +02:00
|
|
|
*/
|
2004-10-26 23:48:41 +02:00
|
|
|
static INLINE char *format_msg(char *buf, size_t buf_len,
|
2004-06-21 06:37:27 +02:00
|
|
|
int severity, const char *funcname,
|
|
|
|
const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
size_t n;
|
2004-10-27 08:37:34 +02:00
|
|
|
int r;
|
2004-10-26 23:48:41 +02:00
|
|
|
char *end_of_prefix;
|
2004-11-29 09:40:24 +01:00
|
|
|
|
|
|
|
tor_assert(buf_len >= 2); /* prevent integer underflow */
|
2004-06-21 06:37:27 +02:00
|
|
|
buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
|
|
|
|
|
|
|
|
n = _log_prefix(buf, buf_len, severity);
|
2004-10-26 23:48:41 +02:00
|
|
|
end_of_prefix = buf+n;
|
2003-09-25 12:42:07 +02:00
|
|
|
|
2003-09-19 11:30:34 +02:00
|
|
|
if (funcname) {
|
2004-10-27 08:37:34 +02:00
|
|
|
r = tor_snprintf(buf+n, buf_len-n, "%s(): ", funcname);
|
|
|
|
if (r<0)
|
|
|
|
n = strlen(buf);
|
|
|
|
else
|
|
|
|
n += r;
|
2003-09-19 11:30:34 +02:00
|
|
|
}
|
2004-11-09 21:04:00 +01:00
|
|
|
|
2004-10-27 23:14:11 +02:00
|
|
|
r = tor_vsnprintf(buf+n,buf_len-n,format,ap);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (r < 0) {
|
2004-11-29 21:39:55 +01:00
|
|
|
/* The message was too long; overwrite the end of the buffer with
|
|
|
|
* "[...truncated]" */
|
|
|
|
if (buf_len >= TRUNCATED_STR_LEN) {
|
2004-11-30 07:15:06 +01:00
|
|
|
int offset = buf_len-TRUNCATED_STR_LEN;
|
|
|
|
/* We have an extra 2 characters after buf_len to hold the \n\0,
|
|
|
|
* so it's safe to add 1 to the size here. */
|
|
|
|
strlcpy(buf+offset, TRUNCATED_STR, buf_len-offset+1);
|
2004-11-29 21:39:55 +01:00
|
|
|
}
|
|
|
|
/* Set 'n' to the end of the buffer, where we'll be writing \n\0.
|
|
|
|
* Since we already subtracted 2 from buf_len, this is safe.*/
|
|
|
|
n = buf_len;
|
2004-10-27 23:14:11 +02:00
|
|
|
} else {
|
|
|
|
n += r;
|
2004-08-15 22:13:07 +02:00
|
|
|
}
|
2003-09-16 19:58:36 +02:00
|
|
|
buf[n]='\n';
|
|
|
|
buf[n+1]='\0';
|
2004-10-26 23:48:41 +02:00
|
|
|
return end_of_prefix;
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Helper: sends a message to the appropriate logfiles, at loglevel
|
|
|
|
* <b>severity</b>. If provided, <b>funcname</b> is prepended to the
|
2004-10-27 08:37:34 +02:00
|
|
|
* message. The actual message is derived as from tor_snprintf(format,ap).
|
2004-05-10 05:53:24 +02:00
|
|
|
*/
|
2003-12-17 22:14:13 +01:00
|
|
|
static void
|
2003-09-16 19:58:36 +02:00
|
|
|
logv(int severity, const char *funcname, const char *format, va_list ap)
|
|
|
|
{
|
2003-09-30 21:53:25 +02:00
|
|
|
char buf[10024];
|
2003-09-16 19:58:36 +02:00
|
|
|
int formatted = 0;
|
|
|
|
logfile_t *lf;
|
2004-10-26 23:48:41 +02:00
|
|
|
char *end_of_prefix=NULL;
|
2003-12-14 10:58:43 +01:00
|
|
|
|
2003-06-21 21:03:22 +02:00
|
|
|
assert(format);
|
2004-09-23 05:51:45 +02:00
|
|
|
lf = logfiles;
|
2004-11-28 10:05:49 +01:00
|
|
|
while (lf) {
|
2004-10-26 23:48:41 +02:00
|
|
|
if (severity > lf->loglevel || severity < lf->max_loglevel) {
|
2004-09-23 05:51:45 +02:00
|
|
|
lf = lf->next;
|
2003-09-16 19:58:36 +02:00
|
|
|
continue;
|
2004-09-23 05:51:45 +02:00
|
|
|
}
|
2004-11-03 19:27:19 +01:00
|
|
|
if (! (lf->file || lf->is_syslog || lf->callback)) {
|
2004-09-23 05:51:45 +02:00
|
|
|
lf = lf->next;
|
2003-09-16 19:58:36 +02:00
|
|
|
continue;
|
2004-09-23 05:51:45 +02:00
|
|
|
}
|
2002-07-12 20:14:17 +02:00
|
|
|
|
2003-09-16 19:58:36 +02:00
|
|
|
if (!formatted) {
|
2004-10-26 23:48:41 +02:00
|
|
|
end_of_prefix =
|
|
|
|
format_msg(buf, sizeof(buf), severity, funcname, format, ap);
|
2003-09-16 19:58:36 +02:00
|
|
|
formatted = 1;
|
|
|
|
}
|
2004-10-26 23:48:41 +02:00
|
|
|
if (lf->is_syslog) {
|
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
syslog(severity, "%s", end_of_prefix);
|
|
|
|
#endif
|
|
|
|
lf = lf->next;
|
|
|
|
continue;
|
2004-11-03 19:27:19 +01:00
|
|
|
} else if (lf->callback) {
|
|
|
|
lf->callback(severity, end_of_prefix);
|
|
|
|
lf = lf->next;
|
|
|
|
continue;
|
2004-10-26 23:48:41 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (fputs(buf, lf->file) == EOF ||
|
2004-11-28 12:39:53 +01:00
|
|
|
fflush(lf->file) == EOF) { /* error */
|
2004-09-23 05:51:45 +02:00
|
|
|
/* don't log the error! Blow away this log entry and continue. */
|
|
|
|
logfile_t *victim = lf;
|
|
|
|
lf = victim->next;
|
2004-09-23 06:59:02 +02:00
|
|
|
delete_log(victim);
|
2004-09-23 05:51:45 +02:00
|
|
|
} else {
|
|
|
|
lf = lf->next;
|
2004-03-14 16:50:00 +01:00
|
|
|
}
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
2003-06-21 21:03:22 +02:00
|
|
|
}
|
2003-06-17 23:36:44 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Output a message to the log. */
|
2003-10-07 18:30:05 +02:00
|
|
|
void _log(int severity, const char *format, ...)
|
2003-06-17 23:36:44 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap,format);
|
|
|
|
logv(severity, NULL, format, ap);
|
|
|
|
va_end(ap);
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2002-09-04 02:39:33 +02:00
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** Output a message to the log, prefixed with a function name <b>fn</b>. */
|
2004-11-15 22:18:07 +01:00
|
|
|
#ifdef __GNUC__
|
2003-06-17 23:36:44 +02:00
|
|
|
void _log_fn(int severity, const char *fn, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap,format);
|
|
|
|
logv(severity, fn, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2004-11-15 22:18:07 +01:00
|
|
|
#else
|
|
|
|
const char *_log_fn_function_name=NULL;
|
|
|
|
void _log_fn(int severity, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap,format);
|
|
|
|
logv(severity, _log_fn_function_name, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
_log_fn_function_name = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2003-06-17 23:36:44 +02:00
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** Close all open log files. */
|
2003-09-16 19:58:36 +02:00
|
|
|
void close_logs()
|
|
|
|
{
|
2003-10-18 02:07:58 +02:00
|
|
|
logfile_t *victim;
|
2004-11-28 10:05:49 +01:00
|
|
|
while (logfiles) {
|
2003-10-18 02:07:58 +02:00
|
|
|
victim = logfiles;
|
|
|
|
logfiles = logfiles->next;
|
2004-10-26 23:48:41 +02:00
|
|
|
close_log(victim);
|
2004-05-20 21:47:28 +02:00
|
|
|
tor_free(victim->filename);
|
|
|
|
tor_free(victim);
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
|
|
|
}
|
2003-06-17 23:36:44 +02:00
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Close and re-open all log files; used to rotate logs on SIGHUP. */
|
2003-09-16 19:58:36 +02:00
|
|
|
void reset_logs()
|
|
|
|
{
|
2004-09-23 05:51:45 +02:00
|
|
|
logfile_t *lf = logfiles;
|
2004-11-28 10:05:49 +01:00
|
|
|
while (lf) {
|
2004-10-26 23:48:41 +02:00
|
|
|
if (reset_log(lf)) {
|
|
|
|
/* error. don't log it. delete the log entry and continue. */
|
|
|
|
logfile_t *victim = lf;
|
|
|
|
lf = victim->next;
|
|
|
|
delete_log(victim);
|
|
|
|
continue;
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
2004-09-23 05:51:45 +02:00
|
|
|
lf = lf->next;
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 06:59:02 +02:00
|
|
|
/** Remove and free the log entry <b>victim</b> from the linked-list
|
|
|
|
* logfiles (it must be present in the list when this function is
|
|
|
|
* called). After this function is called, the caller shouldn't refer
|
|
|
|
* to <b>victim</b> anymore.
|
|
|
|
*/
|
|
|
|
static void delete_log(logfile_t *victim) {
|
|
|
|
logfile_t *tmpl;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (victim == logfiles)
|
2004-09-23 06:59:02 +02:00
|
|
|
logfiles = victim->next;
|
|
|
|
else {
|
2004-11-28 10:05:49 +01:00
|
|
|
for (tmpl = logfiles; tmpl && tmpl->next != victim; tmpl=tmpl->next) ;
|
2004-10-17 00:28:11 +02:00
|
|
|
tor_assert(tmpl);
|
|
|
|
tor_assert(tmpl->next == victim);
|
2004-09-23 06:59:02 +02:00
|
|
|
tmpl->next = victim->next;
|
|
|
|
}
|
|
|
|
tor_free(victim->filename);
|
|
|
|
tor_free(victim);
|
|
|
|
}
|
|
|
|
|
2004-11-20 07:52:13 +01:00
|
|
|
/** DOCDOC */
|
2004-10-26 23:48:41 +02:00
|
|
|
static void close_log(logfile_t *victim)
|
|
|
|
{
|
|
|
|
if (victim->needs_close && victim->file) {
|
|
|
|
fclose(victim->file);
|
|
|
|
} else if (victim->is_syslog) {
|
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
if (--syslog_count == 0)
|
|
|
|
/* There are no other syslogs; close the logging facility. */
|
|
|
|
closelog();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-20 07:52:13 +01:00
|
|
|
/** DOCDOC */
|
2004-10-26 23:48:41 +02:00
|
|
|
static int reset_log(logfile_t *lf)
|
|
|
|
{
|
|
|
|
if (lf->needs_close) {
|
2004-11-20 07:52:13 +01:00
|
|
|
if (fclose(lf->file)==EOF ||
|
2004-10-26 23:48:41 +02:00
|
|
|
!(lf->file = fopen(lf->filename, "a"))) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
2004-11-20 07:52:13 +01:00
|
|
|
if (log_tor_version(lf, 1) < 0)
|
|
|
|
return -1;
|
2004-10-26 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/** Add a log handler to send all messages of severity <b>loglevel</b>
|
|
|
|
* or higher to <b>stream</b>. */
|
2004-05-19 22:07:08 +02:00
|
|
|
void add_stream_log(int loglevelMin, int loglevelMax, const char *name, FILE *stream)
|
2003-09-16 19:58:36 +02:00
|
|
|
{
|
|
|
|
logfile_t *lf;
|
2004-10-26 23:48:41 +02:00
|
|
|
lf = tor_malloc_zero(sizeof(logfile_t));
|
2004-05-20 21:47:28 +02:00
|
|
|
lf->filename = tor_strdup(name);
|
2004-05-19 22:07:08 +02:00
|
|
|
lf->loglevel = loglevelMin;
|
|
|
|
lf->max_loglevel = loglevelMax;
|
2003-09-16 19:58:36 +02:00
|
|
|
lf->file = stream;
|
|
|
|
lf->next = logfiles;
|
|
|
|
logfiles = lf;
|
|
|
|
}
|
|
|
|
|
2004-05-20 21:47:28 +02:00
|
|
|
/** Add a log handler to receive messages during startup (before the real
|
|
|
|
* logs are initialized).
|
|
|
|
*/
|
|
|
|
void add_temp_log(void)
|
|
|
|
{
|
2005-01-04 03:25:18 +01:00
|
|
|
add_stream_log(LOG_NOTICE, LOG_ERR, "<temp>", stdout);
|
2004-05-20 21:47:28 +02:00
|
|
|
logfiles->is_temporary = 1;
|
|
|
|
}
|
|
|
|
|
2004-11-03 19:27:19 +01:00
|
|
|
int add_callback_log(int loglevelMin, int loglevelMax, log_callback cb)
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
lf = tor_malloc_zero(sizeof(logfile_t));
|
|
|
|
lf->loglevel = loglevelMin;
|
|
|
|
lf->max_loglevel = loglevelMax;
|
2004-11-03 22:53:12 +01:00
|
|
|
lf->filename = tor_strdup("<callback>");
|
2004-11-03 19:27:19 +01:00
|
|
|
lf->callback = cb;
|
|
|
|
lf->next = logfiles;
|
|
|
|
logfiles = lf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-26 23:48:41 +02:00
|
|
|
/** Close any log handlers added by add_temp_log or marked by mark_logs_temp */
|
2004-05-20 21:47:28 +02:00
|
|
|
void close_temp_logs(void)
|
|
|
|
{
|
2004-11-20 13:16:47 +01:00
|
|
|
logfile_t *lf, **p;
|
|
|
|
for (p = &logfiles; *p; ) {
|
|
|
|
if ((*p)->is_temporary) {
|
|
|
|
lf = *p;
|
2004-11-20 13:17:19 +01:00
|
|
|
/* we use *p here to handle the edge case of the head of the list */
|
2004-11-20 13:16:47 +01:00
|
|
|
*p = (*p)->next;
|
|
|
|
close_log(lf);
|
|
|
|
tor_free(lf->filename);
|
|
|
|
tor_free(lf);
|
2004-05-20 21:47:28 +02:00
|
|
|
} else {
|
2004-11-20 13:16:47 +01:00
|
|
|
p = &((*p)->next);
|
2004-05-20 21:47:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-26 23:48:41 +02:00
|
|
|
/** Configure all log handles to be closed by close_temp_logs */
|
2004-06-02 21:18:37 +02:00
|
|
|
void mark_logs_temp(void)
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
for (lf = logfiles; lf; lf = lf->next)
|
|
|
|
lf->is_temporary = 1;
|
|
|
|
}
|
|
|
|
|
2004-05-10 05:53:24 +02:00
|
|
|
/**
|
|
|
|
* Add a log handler to send messages to <b>filename</b>. If opening
|
|
|
|
* the logfile fails, -1 is returned and errno is set appropriately
|
2004-05-10 12:27:54 +02:00
|
|
|
* (by fopen).
|
2003-11-19 03:09:43 +01:00
|
|
|
*/
|
2004-05-19 22:07:08 +02:00
|
|
|
int add_file_log(int loglevelMin, int loglevelMax, const char *filename)
|
2003-09-16 19:58:36 +02:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
f = fopen(filename, "a");
|
2003-10-15 20:38:38 +02:00
|
|
|
if (!f) return -1;
|
2004-05-19 22:07:08 +02:00
|
|
|
add_stream_log(loglevelMin, loglevelMax, filename, f);
|
2003-09-16 19:58:36 +02:00
|
|
|
logfiles->needs_close = 1;
|
2004-11-20 07:52:13 +01:00
|
|
|
if (log_tor_version(logfiles, 0) < 0) {
|
2004-12-22 09:16:42 +01:00
|
|
|
delete_log(logfiles);
|
2004-11-20 07:52:13 +01:00
|
|
|
}
|
2003-10-15 20:38:38 +02:00
|
|
|
return 0;
|
2003-09-16 19:58:36 +02:00
|
|
|
}
|
2003-09-27 00:27:24 +02:00
|
|
|
|
2004-10-26 23:48:41 +02:00
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
/**
|
|
|
|
* Add a log handler to send messages to they system log facility.
|
|
|
|
*/
|
|
|
|
int add_syslog_log(int loglevelMin, int loglevelMax)
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
if (syslog_count++ == 0)
|
2004-11-09 02:24:10 +01:00
|
|
|
/* This is the first syslog. */
|
2004-10-26 23:48:41 +02:00
|
|
|
openlog("Tor", LOG_NDELAY, LOG_DAEMON);
|
|
|
|
|
|
|
|
lf = tor_malloc_zero(sizeof(logfile_t));
|
|
|
|
lf->loglevel = loglevelMin;
|
|
|
|
lf->filename = tor_strdup("<syslog>");
|
|
|
|
lf->max_loglevel = loglevelMax;
|
|
|
|
lf->is_syslog = 1;
|
|
|
|
lf->next = logfiles;
|
|
|
|
logfiles = lf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-05-19 22:07:08 +02:00
|
|
|
/** If <b>level</b> is a valid log severity, return the corresponding
|
|
|
|
* numeric value. Otherwise, return -1. */
|
|
|
|
int parse_log_level(const char *level) {
|
|
|
|
if (!strcasecmp(level, "err"))
|
|
|
|
return LOG_ERR;
|
2004-05-21 14:38:52 +02:00
|
|
|
if (!strcasecmp(level, "warn"))
|
|
|
|
return LOG_WARN;
|
|
|
|
if (!strcasecmp(level, "notice"))
|
2004-05-19 22:07:08 +02:00
|
|
|
return LOG_NOTICE;
|
2004-05-21 14:38:52 +02:00
|
|
|
if (!strcasecmp(level, "info"))
|
2004-05-19 22:07:08 +02:00
|
|
|
return LOG_INFO;
|
2004-05-21 14:38:52 +02:00
|
|
|
if (!strcasecmp(level, "debug"))
|
2004-05-19 22:07:08 +02:00
|
|
|
return LOG_DEBUG;
|
2004-05-21 14:38:52 +02:00
|
|
|
return -1;
|
2004-05-19 22:07:08 +02:00
|
|
|
}
|
|
|
|
|
2004-11-05 06:50:35 +01:00
|
|
|
const char *log_level_to_string(int level)
|
|
|
|
{
|
|
|
|
return sev_to_string(level);
|
|
|
|
}
|
|
|
|
|
2004-05-19 22:07:08 +02:00
|
|
|
int get_min_log_level(void)
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
int min = LOG_ERR;
|
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
2004-10-26 23:48:41 +02:00
|
|
|
if (lf->loglevel > min)
|
2004-05-19 22:07:08 +02:00
|
|
|
min = lf->loglevel;
|
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
2004-11-20 08:33:55 +01:00
|
|
|
/** Switch all logs to output at most verbose level. */
|
|
|
|
void switch_logs_debug(void)
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
for (lf = logfiles; lf; lf=lf->next) {
|
|
|
|
lf->loglevel = LOG_DEBUG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|