From 752c00bbc7a5ca4beee73757df6462fb042c472a Mon Sep 17 00:00:00 2001 From: Nicolas Favre-Felix Date: Tue, 4 Jan 2011 20:04:56 +0100 Subject: [PATCH] Saved a few syscalls and opening/closing the log. Log either to a file or to stdout. --- .gitignore | 1 + conf.c | 5 ++++- slog.c | 21 ++++++++++----------- slog.h | 2 +- webdis.json | 1 + 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 0c6994b..8da65d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.log +*.swp *.o webdis diff --git a/conf.c b/conf.c index 192c611..37696c3 100644 --- a/conf.c +++ b/conf.c @@ -71,7 +71,10 @@ conf_read(const char *filename) { } else if(strcmp(json_object_iter_key(kv),"logfile") == 0 && json_typeof(jtmp) == JSON_STRING){ conf->logfile = strdup(json_string_value(jtmp)); } else if(strcmp(json_object_iter_key(kv),"verbosity") == 0 && json_typeof(jtmp) == JSON_INTEGER){ - conf->verbosity = (short)json_integer_value(jtmp); + 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; } } diff --git a/slog.c b/slog.c index d37b5a8..9a749e4 100644 --- a/slog.c +++ b/slog.c @@ -13,23 +13,22 @@ void slog(const struct server *s, log_level level, const char *body) { const char *c = ".-*#"; time_t now = time(NULL); - FILE *fp; + static FILE *fp = NULL; char buf[64]; - char msg[1024]; + char msg[124]; - if(level > s->cfg->verbosity) { - return; - } + static pid_t self = 0; + if(!self) self = getpid(); - snprintf(msg, sizeof(msg), "%s", body); + if(level > s->cfg->verbosity) return; /* too verbose */ - fp = (s->cfg->logfile == NULL) ? stdout : fopen(s->cfg->logfile,"a"); + if(!fp) fp = (s->cfg->logfile == NULL) ? stdout : fopen(s->cfg->logfile, "a"); if(!fp) return; + /* limit message size */ + snprintf(msg, sizeof(msg), "%s", body); + strftime(buf,sizeof(buf),"%d %b %H:%M:%S",localtime(&now)); - fprintf(fp,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg); - fprintf(stdout,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg); + fprintf(fp,"[%d] %s %c %s\n", (int)self, buf, c[level], msg); fflush(fp); - - if(s->cfg->logfile) fclose(fp); } diff --git a/slog.h b/slog.h index d402d92..047b064 100644 --- a/slog.h +++ b/slog.h @@ -2,7 +2,7 @@ #define SLOG_H typedef enum { - WEBDIS_ERROR, + WEBDIS_ERROR = 0, WEBDIS_WARNING, WEBDIS_NOTICE, WEBDIS_INFO, diff --git a/webdis.json b/webdis.json index ed15a29..15db98d 100644 --- a/webdis.json +++ b/webdis.json @@ -17,6 +17,7 @@ "enabled": ["DEBUG"] } ], + "verbosity": 3, "logfile": "webdis.log" }