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$ */
|
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-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 "orconfig.h"
|
|
|
|
#include "./util.h"
|
|
|
|
#include "./log.h"
|
|
|
|
|
2004-04-03 04:40:30 +02:00
|
|
|
|
2004-03-09 23:01:17 +01:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
#define vsnprintf _vsnprintf
|
2004-04-28 22:13:21 +02:00
|
|
|
#define snprintf _snprintf
|
2004-03-09 23:01:17 +01:00
|
|
|
#endif
|
2002-09-04 02:39:33 +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. */
|
|
|
|
const char *filename; /**< Filename to open. */
|
|
|
|
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. */
|
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) {
|
2002-09-04 02:39:33 +02: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;
|
2003-06-21 21:03:22 +02:00
|
|
|
|
2004-05-10 05:53:24 +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
|
|
|
|
* than once.)
|
2003-09-16 19:58:36 +02:00
|
|
|
*/
|
|
|
|
static INLINE void format_msg(char *buf, size_t buf_len,
|
2003-12-17 22:14:13 +01:00
|
|
|
int severity, const char *funcname,
|
|
|
|
const char *format, va_list ap)
|
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;
|
2004-03-09 23:01:17 +01:00
|
|
|
size_t n;
|
2003-09-16 19:58:36 +02:00
|
|
|
|
|
|
|
buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
|
2003-12-14 10:58:43 +01: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
|
|
|
|
2003-09-19 11:30:34 +02:00
|
|
|
n = strftime(buf, buf_len, "%b %d %H:%M:%S", localtime(&t));
|
2003-09-16 19:58:36 +02:00
|
|
|
n += 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));
|
|
|
|
if(n > buf_len)
|
2003-09-25 12:42:07 +02:00
|
|
|
n = buf_len-1; /* the *nprintf funcs return how many bytes they
|
|
|
|
* _would_ print, if the output is truncated.
|
|
|
|
* Subtract one because the count doesn't include the \0 */
|
|
|
|
|
2003-09-19 11:30:34 +02:00
|
|
|
if (funcname) {
|
2003-09-16 19:58:36 +02:00
|
|
|
n += snprintf(buf+n, buf_len-n, "%s(): ", funcname);
|
2003-09-19 11:30:34 +02:00
|
|
|
if(n > buf_len)
|
2003-09-25 12:42:07 +02:00
|
|
|
n = buf_len-1;
|
2003-09-19 11:30:34 +02:00
|
|
|
}
|
2003-09-16 19:58:36 +02:00
|
|
|
|
|
|
|
n += vsnprintf(buf+n,buf_len-n,format,ap);
|
2003-09-19 11:30:34 +02:00
|
|
|
if(n > buf_len)
|
2003-09-25 12:42:07 +02:00
|
|
|
n = buf_len-1;
|
2003-09-16 19:58:36 +02:00
|
|
|
buf[n]='\n';
|
|
|
|
buf[n+1]='\0';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* message. The actual message is derived as from vsprintf(format,ap).
|
|
|
|
*/
|
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;
|
2003-12-14 10:58:43 +01:00
|
|
|
|
2003-06-21 21:03:22 +02:00
|
|
|
assert(format);
|
2003-09-16 19:58:36 +02:00
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
|
|
|
if (severity < lf->loglevel || severity > lf->max_loglevel)
|
|
|
|
continue;
|
|
|
|
if (!lf->file)
|
|
|
|
continue;
|
2002-07-12 20:14:17 +02:00
|
|
|
|
2003-09-16 19:58:36 +02:00
|
|
|
if (!formatted) {
|
2003-09-30 21:53:25 +02:00
|
|
|
format_msg(buf, 10024, severity, funcname, format, ap);
|
2003-09-16 19:58:36 +02:00
|
|
|
formatted = 1;
|
|
|
|
}
|
2004-03-14 16:50:00 +01:00
|
|
|
if(fputs(buf, lf->file) == EOF) { /* error */
|
|
|
|
assert(0); /* XXX */
|
|
|
|
}
|
|
|
|
if(fflush(lf->file) == EOF) { /* error */
|
|
|
|
/* don't log the error! */
|
|
|
|
assert(0); /* XXX fail for now. what's better to do? */
|
|
|
|
}
|
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>. */
|
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-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;
|
|
|
|
while(logfiles) {
|
|
|
|
victim = logfiles;
|
|
|
|
logfiles = logfiles->next;
|
|
|
|
if (victim->needs_close)
|
|
|
|
fclose(victim->file);
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
|
|
|
if (lf->needs_close) {
|
|
|
|
fclose(lf->file);
|
|
|
|
lf->file = fopen(lf->filename, "a");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
lf = tor_malloc(sizeof(logfile_t));
|
|
|
|
lf->filename = name;
|
|
|
|
lf->needs_close = 0;
|
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-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;
|
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-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;
|
|
|
|
else if (!strcasecmp(level, "notice"))
|
|
|
|
return LOG_NOTICE;
|
|
|
|
else if (!strcasecmp(level, "info"))
|
|
|
|
return LOG_INFO;
|
|
|
|
else if (!strcasecmp(level, "debug"))
|
|
|
|
return LOG_DEBUG;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_min_log_level(void)
|
|
|
|
{
|
|
|
|
logfile_t *lf;
|
|
|
|
int min = LOG_ERR;
|
|
|
|
for (lf = logfiles; lf; lf = lf->next) {
|
|
|
|
if (lf->loglevel < min)
|
|
|
|
min = lf->loglevel;
|
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-06 05:44:36 +02:00
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|