From 9d2cd7fc6e01ee5aafeaf222b5020c464a01a03d Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 19 May 2004 20:07:08 +0000 Subject: Allow multiple logfiles at different severity ranges svn:r1899 --- src/common/log.c | 38 +++++++++++++++++++++++++++++++++----- src/common/log.h | 6 ++++-- 2 files changed, 37 insertions(+), 7 deletions(-) (limited to 'src/common') diff --git a/src/common/log.c b/src/common/log.c index 136ea66fc..0581bd792 100644 --- a/src/common/log.c +++ b/src/common/log.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "orconfig.h" #include "./util.h" #include "./log.h" @@ -162,14 +163,14 @@ void reset_logs() /** Add a log handler to send all messages of severity loglevel * or higher to stream. */ -void add_stream_log(int loglevel, const char *name, FILE *stream) +void add_stream_log(int loglevelMin, int loglevelMax, const char *name, FILE *stream) { logfile_t *lf; lf = tor_malloc(sizeof(logfile_t)); lf->filename = name; lf->needs_close = 0; - lf->loglevel = loglevel; - lf->max_loglevel = LOG_ERR; + lf->loglevel = loglevelMin; + lf->max_loglevel = loglevelMax; lf->file = stream; lf->next = logfiles; logfiles = lf; @@ -180,16 +181,43 @@ void add_stream_log(int loglevel, const char *name, FILE *stream) * the logfile fails, -1 is returned and errno is set appropriately * (by fopen). */ -int add_file_log(int loglevel, const char *filename) +int add_file_log(int loglevelMin, int loglevelMax, const char *filename) { FILE *f; f = fopen(filename, "a"); if (!f) return -1; - add_stream_log(loglevel, filename, f); + add_stream_log(loglevelMin, loglevelMax, filename, f); logfiles->needs_close = 1; return 0; } +/** If level 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; +} + + /* Local Variables: mode:c diff --git a/src/common/log.h b/src/common/log.h index fa830a9b4..786e7fcc1 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -46,8 +46,10 @@ #define CHECK_PRINTF(formatIdx, firstArg) #endif -void add_stream_log(int loglevel, const char *name, FILE *stream); -int add_file_log(int severity, const char *filename); +int parse_log_level(const char *level); +void add_stream_log(int severityMin, int severityMax, const char *name, FILE *stream); +int add_file_log(int severityMin, int severityMax, const char *filename); +int get_min_log_level(void); void close_logs(); void reset_logs(); -- cgit v1.2.3