Log refactoring.

master
Nicolas Favre-Felix 14 years ago
parent bec19d06eb
commit 4741fed926

@ -33,7 +33,7 @@ conf_read(const char *filename) {
conf->user = getuid(); conf->user = getuid();
conf->group = getgid(); conf->group = getgid();
conf->logfile = "webdis.log"; conf->logfile = "webdis.log";
conf->verbosity = WEBDIS_VERBOSE; conf->verbosity = WEBDIS_NOTICE;
j = json_load_file(filename, 0, &error); j = json_load_file(filename, 0, &error);
if(!j) { if(!j) {

@ -1,15 +1,8 @@
#ifndef CONF_H #ifndef CONF_H
#define CONF_H #define CONF_H
#define WEBDIS_VERBOSE 0
#define WEBDIS_QUIET 1
#define WEBDIS_SILENT 2
#include <sys/types.h> #include <sys/types.h>
#include "slog.h"
typedef enum {
WARNING = 0
} log_level;
struct conf { struct conf {

@ -131,26 +131,24 @@ on_request(struct evhttp_request *rq, void *ctx) {
/* check that the command can be executed */ /* check that the command can be executed */
switch(rq->type) { switch(rq->type) {
case EVHTTP_REQ_GET: case EVHTTP_REQ_GET:
webdis_log(s,1,uri); slog(s, WEBDIS_DEBUG, uri);
ret = cmd_run(s, rq, 1+uri, strlen(uri)-1); ret = cmd_run(s, rq, 1+uri, strlen(uri)-1);
break; break;
case EVHTTP_REQ_POST: case EVHTTP_REQ_POST:
webdis_log(s,1,uri); slog(s, WEBDIS_DEBUG, uri);
ret = cmd_run(s, rq, ret = cmd_run(s, rq,
(const char*)EVBUFFER_DATA(rq->input_buffer), (const char*)EVBUFFER_DATA(rq->input_buffer),
EVBUFFER_LENGTH(rq->input_buffer)); EVBUFFER_LENGTH(rq->input_buffer));
break; break;
default: default:
printf("405\n"); slog(s, WEBDIS_DEBUG, "405");
webdis_log(s,1,"405");
evhttp_send_reply(rq, 405, "Method Not Allowed", NULL); evhttp_send_reply(rq, 405, "Method Not Allowed", NULL);
return; return;
} }
if(ret < 0) { if(ret < 0) {
slog(s->cfg->logfile,2, uri);
evhttp_send_reply(rq, 403, "Forbidden", NULL); evhttp_send_reply(rq, 403, "Forbidden", NULL);
} }
} }
@ -164,13 +162,13 @@ server_start(struct server *s) {
#endif #endif
/* start http server */ /* start http server */
slog(s->cfg->logfile,1,"Starting HTTP Server"); slog(s, WEBDIS_INFO, "Starting HTTP Server");
evhttp_bind_socket(s->http, s->cfg->http_host, s->cfg->http_port); evhttp_bind_socket(s->http, s->cfg->http_host, s->cfg->http_port);
evhttp_set_cb(s->http, "/crossdomain.xml", on_flash_request, s); evhttp_set_cb(s->http, "/crossdomain.xml", on_flash_request, s);
evhttp_set_gencb(s->http, on_request, s); evhttp_set_gencb(s->http, on_request, s);
/* drop privileges */ /* drop privileges */
slog(s->cfg->logfile,1,"Dropping Privileges"); slog(s, WEBDIS_INFO, "Dropping Privileges");
setuid(s->cfg->user); setuid(s->cfg->user);
setgid(s->cfg->group); setgid(s->cfg->group);
@ -180,10 +178,3 @@ server_start(struct server *s) {
/* loop */ /* loop */
event_base_dispatch(s->base); 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);
}
}

@ -6,22 +6,30 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
void slog(const char *logfile, int level, const char *body) { #include "slog.h"
#include "server.h"
#include "conf.h"
void slog(const struct server *s, log_level level, const char *body) {
const char *c = ".-*#"; const char *c = ".-*#";
time_t now = time(NULL); time_t now = time(NULL);
FILE *fp; FILE *fp;
char buf[64]; char buf[64];
char msg[1024]; char msg[1024];
if(level > s->cfg->verbosity) {
return;
}
snprintf(msg, sizeof(msg), "%s", body); snprintf(msg, sizeof(msg), "%s", body);
fp = (logfile == NULL) ? stdout : fopen(logfile,"a"); fp = (s->cfg->logfile == NULL) ? stdout : fopen(s->cfg->logfile,"a");
if (!fp) return; if(!fp) return;
strftime(buf,sizeof(buf),"%d %b %H:%M:%S",localtime(&now)); 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(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(stdout,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg);
fflush(fp); fflush(fp);
if (logfile) fclose(fp); if(s->cfg->logfile) fclose(fp);
} }

@ -1,6 +1,16 @@
#ifndef SLOG_H #ifndef SLOG_H
#define SLOG_H #define SLOG_H
void slog(const char *logfile, int level, const char *body); typedef enum {
WEBDIS_ERROR,
WEBDIS_WARNING,
WEBDIS_NOTICE,
WEBDIS_INFO,
WEBDIS_DEBUG
} log_level;
struct server;
void slog(const struct server *s, log_level level, const char *body);
#endif #endif

@ -17,6 +17,6 @@
"enabled": ["DEBUG"] "enabled": ["DEBUG"]
} }
], ],
"verbosity": 0, "verbosity": 3,
"logfile": "webdis.log" "logfile": "webdis.log"
} }

Loading…
Cancel
Save