diff --git a/src/conf.c b/src/conf.c index 92a68fc..f886b57 100644 --- a/src/conf.c +++ b/src/conf.c @@ -159,9 +159,11 @@ conf_read(const char *filename) { } } else if(strcmp(json_object_iter_key(kv),"verbosity") == 0 && json_typeof(jtmp) == JSON_INTEGER){ int tmp = json_integer_value(jtmp); - if(tmp < 0) conf->verbosity = WEBDIS_ERROR; - else if(tmp > (int)WEBDIS_DEBUG) conf->verbosity = WEBDIS_DEBUG; - else conf->verbosity = (log_level)tmp; + if(tmp < 0 || tmp > (int)WEBDIS_TRACE) { + fprintf(stderr, "Invalid log verbosity: %d. Acceptable range: [%d .. %d]\n", + tmp, WEBDIS_ERROR, WEBDIS_TRACE); + } + conf->verbosity = (tmp < 0 ? WEBDIS_ERROR : (tmp > WEBDIS_TRACE ? WEBDIS_TRACE : (log_level)tmp)); } else if(strcmp(json_object_iter_key(kv), "daemonize") == 0 && json_typeof(jtmp) == JSON_TRUE) { conf->daemonize = 1; } else if(strcmp(json_object_iter_key(kv), "daemonize") == 0 && json_typeof(jtmp) == JSON_STRING) { diff --git a/src/slog.c b/src/slog.c index 4c30438..3a48680 100644 --- a/src/slog.c +++ b/src/slog.c @@ -76,6 +76,14 @@ slog_fsync_init(struct server *s) { } } +/** + * Returns whether this log level is enabled. + */ +int +slog_enabled(struct server *s, log_level level) { + return level <= s->cfg->verbosity ? 1 : 0; +} + /** * Write log message to disk, or stderr. */ @@ -83,6 +91,8 @@ void slog(struct server *s, log_level level, const char *body, size_t sz) { + if(level > s->cfg->verbosity) return; /* too verbose */ + const char *c = "EWNID"; time_t now; struct tm now_tm, *lt_ret; @@ -91,8 +101,6 @@ slog(struct server *s, log_level level, char line[256]; /* bounds are checked. */ int line_sz, ret; - if(level > s->cfg->verbosity) return; /* too verbose */ - if(!s->log.fd) return; /* limit message size */ diff --git a/src/slog.h b/src/slog.h index f521d9a..9400702 100644 --- a/src/slog.h +++ b/src/slog.h @@ -6,7 +6,8 @@ typedef enum { WEBDIS_WARNING, WEBDIS_NOTICE, WEBDIS_INFO, - WEBDIS_DEBUG + WEBDIS_DEBUG, + WEBDIS_TRACE } log_level; typedef enum { @@ -26,6 +27,9 @@ slog_init(struct server *s); void slog_fsync_init(struct server *s); +int +slog_enabled(struct server *s, log_level level); + void slog(struct server *s, log_level level, const char *body, size_t sz);