From 363ffc1a84d30ce5b90e27b4fa522cd9c4a336ef Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Wed, 26 Jan 2011 14:54:32 +0200 Subject: [PATCH] logging: LG_STATS loglevel for liveness messages --- test/compile.c | 1 + usual/logging.c | 23 ++++++++++++++--------- usual/logging.h | 19 ++++++++++++++++--- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/test/compile.c b/test/compile.c index 6a61408..b3a0e55 100644 --- a/test/compile.c +++ b/test/compile.c @@ -47,6 +47,7 @@ int main(void) log_debug("test"); if (!parse_ini_file("foo", NULL, NULL)) log_debug("test"); + log_stats("1"); file_size("foo"); md5_reset(&md5); strlcpy(buf, "foo", sizeof(buf)); diff --git a/usual/logging.c b/usual/logging.c index 175ecf5..d9ae024 100644 --- a/usual/logging.c +++ b/usual/logging.c @@ -57,6 +57,10 @@ int cf_syslog = 0; const char *cf_syslog_ident = NULL; const char *cf_syslog_facility = NULL; +enum LogLevel cf_syslog_level = LG_INFO; +enum LogLevel cf_logfile_level = LG_NOISE; +enum LogLevel cf_stderr_level = LG_NOISE; + /* optional function to fill prefix */ logging_prefix_fn_t logging_prefix_cb; @@ -69,12 +73,13 @@ struct LevelInfo { }; static const struct LevelInfo log_level_list[] = { - { "FATAL", LOG_CRIT }, - { "ERROR", LOG_ERR }, - { "WARNING", LOG_WARNING }, - { "LOG", LOG_INFO }, - { "DEBUG", LOG_DEBUG }, - { "NOISE", LOG_DEBUG }, + { "FATAL", LOG_CRIT }, /* LG_FATAL */ + { "ERROR", LOG_ERR }, /* LG_ERROR */ + { "WARNING", LOG_WARNING },/* LG_WARNING */ + { "LOG", LOG_INFO }, /* LG_STATS*/ + { "LOG", LOG_INFO }, /* LG_INFO */ + { "DEBUG", LOG_DEBUG }, /* LG_DEBUG */ + { "NOISE", LOG_DEBUG }, /* LG_NOISE */ }; struct FacName { const char *name; int code; }; @@ -194,13 +199,13 @@ void log_generic(enum LogLevel level, void *ctx, const char *fmt, ...) } } - if (!cf_quiet) + if (!cf_quiet && level <= cf_stderr_level) fprintf(stderr, "%s %u %s %s\n", timebuf, pid, lev->tag, msg); - if (log_file) + if (log_file && level <= cf_logfile_level) fprintf(log_file, "%s %u %s %s\n", timebuf, pid, lev->tag, msg); - if (cf_syslog) { + if (cf_syslog && level <= cf_syslog_level) { if (!syslog_started) start_syslog(); syslog(lev->syslog_prio, "%s", msg); diff --git a/usual/logging.h b/usual/logging.h index f6276ab..a045c28 100644 --- a/usual/logging.h +++ b/usual/logging.h @@ -48,9 +48,10 @@ enum LogLevel { LG_FATAL = 0, LG_ERROR = 1, LG_WARNING = 2, - LG_INFO = 3, - LG_DEBUG = 4, - LG_NOISE = 5, + LG_STATS = 3, + LG_INFO = 4, + LG_DEBUG = 5, + LG_NOISE = 6, }; #ifndef LOG_CONTEXT_DEF /** Example: Prepare dummy context pointer */ @@ -101,6 +102,13 @@ extern const char *cf_syslog_ident; /** Facility name */ extern const char *cf_syslog_facility; +/** Max log level for syslog writer */ +extern enum LogLevel cf_syslog_level; +/** Max log level for logfile writer */ +extern enum LogLevel cf_logfile_level; +/** Max log level for stderr writer */ +extern enum LogLevel cf_stderr_level; + /* * Internal API. */ @@ -126,6 +134,11 @@ void log_fatal(const char *file, int line, const char *func, bool show_perror, log_generic(LG_WARNING, LOG_CONTEXT, fmt, ## args); \ } while (0) +/** Log stats (liveness) message */ +#define log_stats(fmt, args...) do { LOG_CONTEXT_DEF; \ + log_generic(LG_STATS, LOG_CONTEXT, fmt, ## args); \ + } while (0) + /** Log info message */ #define log_info(fmt, args...) do { LOG_CONTEXT_DEF; \ log_generic(LG_INFO, LOG_CONTEXT, fmt, ## args); \ -- 2.39.5