Saved a few syscalls and opening/closing the log. Log either to a file or to stdout.

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

1
.gitignore vendored

@ -1,3 +1,4 @@
*.log *.log
*.swp
*.o *.o
webdis webdis

@ -71,7 +71,10 @@ conf_read(const char *filename) {
} else if(strcmp(json_object_iter_key(kv),"logfile") == 0 && json_typeof(jtmp) == JSON_STRING){ } else if(strcmp(json_object_iter_key(kv),"logfile") == 0 && json_typeof(jtmp) == JSON_STRING){
conf->logfile = strdup(json_string_value(jtmp)); conf->logfile = strdup(json_string_value(jtmp));
} else if(strcmp(json_object_iter_key(kv),"verbosity") == 0 && json_typeof(jtmp) == JSON_INTEGER){ } 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;
} }
} }

@ -13,23 +13,22 @@
void slog(const struct server *s, log_level level, const char *body) { 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; static FILE *fp = NULL;
char buf[64]; char buf[64];
char msg[1024]; char msg[124];
if(level > s->cfg->verbosity) { static pid_t self = 0;
return; 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; if(!fp) return;
/* limit message size */
snprintf(msg, sizeof(msg), "%s", body);
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)self, buf, c[level], msg);
fprintf(stdout,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg);
fflush(fp); fflush(fp);
if(s->cfg->logfile) fclose(fp);
} }

@ -2,7 +2,7 @@
#define SLOG_H #define SLOG_H
typedef enum { typedef enum {
WEBDIS_ERROR, WEBDIS_ERROR = 0,
WEBDIS_WARNING, WEBDIS_WARNING,
WEBDIS_NOTICE, WEBDIS_NOTICE,
WEBDIS_INFO, WEBDIS_INFO,

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

Loading…
Cancel
Save