From 958c6f3295035eeba79c5c8160355b627f8401e0 Mon Sep 17 00:00:00 2001 From: mrb Date: Mon, 3 Jan 2011 20:07:19 -0500 Subject: [PATCH] logging tweaks --- Makefile | 8 ++------ conf.c | 3 +++ conf.h | 11 ++++++++++- server.c | 12 +++++++++--- server.h | 3 +++ webdis.json | 4 +++- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 635ae37..32f62dc 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,9 @@ OUT=webdis HIREDIS_OBJ=hiredis/hiredis.o hiredis/sds.o hiredis/net.o hiredis/async.o hiredis/dict.o JANSSON_OBJ=jansson/src/dump.o jansson/src/error.o jansson/src/hashtable.o jansson/src/load.o jansson/src/strbuffer.o jansson/src/utf.o jansson/src/value.o jansson/src/variadic.o -<<<<<<< HEAD -FORMAT_OBJS=formats/json.o formats/raw.o formats/common.o -OBJS=webdis.o conf.o $(FORMAT_OBJS) cmd.o slog.o server.o $(HIREDIS_OBJ) $(JANSSON_OBJ) libb64/cencode.o acl.o -======= FORMAT_OBJS=formats/json.o formats/raw.o formats/common.o formats/custom-type.o -OBJS=webdis.o conf.o $(FORMAT_OBJS) cmd.o server.o $(HIREDIS_OBJ) $(JANSSON_OBJ) libb64/cencode.o acl.o ->>>>>>> d8298c355662c701727ae270897923bdbfa58aac +OBJS=webdis.o conf.o $(FORMAT_OBJS) cmd.o slog.o server.o $(HIREDIS_OBJ) $(JANSSON_OBJ) libb64/cencode.o acl.o + CFLAGS=-O3 -Wall -Wextra -I. -Ijansson/src LDFLAGS=-levent diff --git a/conf.c b/conf.c index 4ab33c8..7b0b961 100644 --- a/conf.c +++ b/conf.c @@ -33,6 +33,7 @@ conf_read(const char *filename) { conf->user = getuid(); conf->group = getgid(); conf->logfile = "webdis.log"; + conf->verbosity = WEBDIS_VERBOSE; j = json_load_file(filename, 0, &error); if(!j) { @@ -69,6 +70,8 @@ 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); } } diff --git a/conf.h b/conf.h index 3d96522..edfc35f 100644 --- a/conf.h +++ b/conf.h @@ -1,8 +1,16 @@ #ifndef CONF_H #define CONF_H +#define WEBDIS_VERBOSE 0 +#define WEBDIS_QUIET 1 +#define WEBDIS_SILENT 2 + #include +typedef enum { + WARNING = 0 +} log_level; + struct conf { /* connection to Redis */ @@ -21,8 +29,9 @@ struct conf { uid_t user; gid_t group; - /* Logfile */ + /* Logging */ char *logfile; + log_level verbosity; }; struct conf * diff --git a/server.c b/server.c index f574711..255ab60 100644 --- a/server.c +++ b/server.c @@ -131,12 +131,12 @@ on_request(struct evhttp_request *rq, void *ctx) { /* check that the command can be executed */ switch(rq->type) { case EVHTTP_REQ_GET: - slog(s->cfg->logfile,1, uri); + webdis_log(s,1,uri); ret = cmd_run(s, rq, 1+uri, strlen(uri)-1); break; case EVHTTP_REQ_POST: - slog(s->cfg->logfile,1, uri); + webdis_log(s,1,uri); ret = cmd_run(s, rq, (const char*)EVBUFFER_DATA(rq->input_buffer), EVBUFFER_LENGTH(rq->input_buffer)); @@ -144,7 +144,7 @@ on_request(struct evhttp_request *rq, void *ctx) { default: printf("405\n"); - slog(s->cfg->logfile,2, uri); + webdis_log(s,1,"405"); evhttp_send_reply(rq, 405, "Method Not Allowed", NULL); return; } @@ -181,3 +181,9 @@ server_start(struct server *s) { event_base_dispatch(s->base); } +void +webdis_log(struct server *s, int level, const char *body){ + if(level > (int)s->cfg->verbosity){ + slog(s->cfg->logfile,level,body); + } +} diff --git a/server.h b/server.h index 998c9a0..a4f9528 100644 --- a/server.h +++ b/server.h @@ -31,5 +31,8 @@ server_copy(const struct server *s); void server_start(struct server *s); +void +webdis_log(struct server *s, int level, const char *body); + #endif diff --git a/webdis.json b/webdis.json index 611c3f5..0e2e4e1 100644 --- a/webdis.json +++ b/webdis.json @@ -16,5 +16,7 @@ "http_basic_auth": "user:password", "enabled": ["DEBUG"] } - ] + ], + "verbosity": 0, + "logfile": "webdis.log" }