Use a spinlock on the log writer.

master
Nicolas Favre-Felix 14 years ago
parent 0b5f2c98d7
commit 9f92ecfd12

@ -77,6 +77,7 @@ server_new(const char *cfg_file) {
struct server *s = calloc(1, sizeof(struct server));
s->cfg = conf_read(cfg_file);
pthread_spin_init(&s->log_lock, PTHREAD_PROCESS_SHARED);
/* workers */
s->w = calloc(s->cfg->http_threads, sizeof(struct worker*));

@ -3,6 +3,7 @@
#include <event.h>
#include <hiredis/async.h>
#include <pthread.h>
struct worker;
struct conf;
@ -18,6 +19,9 @@ struct server {
/* worker threads */
struct worker **w;
int next_worker;
/* log lock */
pthread_spinlock_t log_lock;
};
struct server *

@ -5,12 +5,14 @@
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include "slog.h"
#include "server.h"
#include "conf.h"
void slog(const struct server *s, log_level level,
void
slog(struct server *s, log_level level,
const char *body, size_t sz) {
const char *c = ".-*#";
@ -19,13 +21,16 @@ void slog(const struct server *s, log_level level,
char buf[64];
char msg[124];
static pid_t self = 0;
if(!self) self = getpid();
if(level > s->cfg->verbosity) return; /* too verbose */
pthread_spin_lock(&s->log_lock);
if(!fp) fp = (s->cfg->logfile == NULL) ? stdout : fopen(s->cfg->logfile, "a");
if(!fp) return;
if(!fp) goto end;
/* limit message size */
snprintf(msg, sz + 1 > sizeof(msg) ? sizeof(msg) : sz + 1, "%s", body);
@ -33,4 +38,7 @@ void slog(const struct server *s, log_level level,
strftime(buf,sizeof(buf),"%d %b %H:%M:%S",localtime(&now));
fprintf(fp,"[%d] %s %c %s\n", (int)self, buf, c[level], msg);
fflush(fp);
end:
pthread_spin_unlock(&s->log_lock);
}

@ -11,7 +11,7 @@ typedef enum {
struct server;
void slog(const struct server *s, log_level level,
void slog(struct server *s, log_level level,
const char *body, size_t sz);
#endif

@ -204,10 +204,12 @@ worker_process_client(struct http_client *c) {
switch(ret) {
case CMD_ACL_FAIL:
case CMD_PARAM_ERROR:
slog(w->s, WEBDIS_DEBUG, "403", 3);
http_send_error(c, 403, "Forbidden");
break;
case CMD_REDIS_UNAVAIL:
slog(w->s, WEBDIS_DEBUG, "503", 3);
http_send_error(c, 503, "Service Unavailable");
break;
default:

Loading…
Cancel
Save