X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Flog.c;h=ad05490c7232b900df69dcbc7c971955a645670f;hb=bb58415037ceff0a0668d4b8bd789ec947170b54;hp=39b9cf9e48bc3f8fcc0bf943735d9ce2c798bcfd;hpb=926276659270c80de3df1bdcf8059d6a6d4b1054;p=umurmur.git diff --git a/src/log.c b/src/log.c index 39b9cf9..ad05490 100644 --- a/src/log.c +++ b/src/log.c @@ -1,5 +1,5 @@ -/* Copyright (C) 2009-2010, Martin Johansson - Copyright (C) 2005-2010, Thorvald Natvig +/* Copyright (C) 2009-2013, Martin Johansson + Copyright (C) 2005-2013, Thorvald Natvig All rights reserved. @@ -53,10 +53,14 @@ static void openlogfile(const char *logfilename) Log_fatal("Failed to open log file '%s' for writing: %s\n", logfilename, strerror(errno)); } + /* Set the stream as line buffered */ + if (setvbuf(logfile, NULL, _IOLBF, 0) < 0) + Log_fatal("setvbuf() failed: %s\n", strerror(errno)); + /* XXX - Is it neccessary/appropriate that logging to file is non-blocking? * If not, there's a risk that execution blocks, meaning that voice blocks * as well since uMurmur is single threaded by design. OTOH, what could - * cause a block? If the disk causes blocking, it is probably br0ken. but + * cause a block? If the disk causes blocking, it is probably br0ken, but * the log could be on a nfs or smb share, so let's set it up as * non-blocking and we'll see what happens. */ @@ -65,6 +69,18 @@ static void openlogfile(const char *logfilename) fcntl(fd, F_SETFL, flags | O_NONBLOCK); } +static char *timestring(void) +{ + static char timebuf[32]; + time_t t; + struct tm *timespec; + + t= time(NULL); + timespec = localtime(&t); + strftime(timebuf, 32, "%b %e %T", timespec); + return timebuf; +} + void Log_init(bool_t terminal) { const char *logfilename; @@ -110,10 +126,11 @@ void logthis(const char *logstring, ...) va_start(argp, logstring); vsnprintf(&buf[0], STRSIZE, logstring, argp); va_end(argp); + if (termprint) fprintf(stderr, "%s\n", buf); else if (logfile) - fprintf(logfile, "%s\n", buf); + fprintf(logfile, "%s %s\n", timestring(), buf); else syslog(LOG_INFO, "%s", buf); } @@ -124,14 +141,17 @@ void Log_warn(const char *logstring, ...) char buf[STRSIZE + 1]; int offset = 0; + if (termprint || logfile) + offset = sprintf(buf, "WARN: "); + va_start(argp, logstring); - offset = sprintf(buf, "WARN: "); vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); + if (termprint) fprintf(stderr, "%s\n", buf); else if (logfile) - fprintf(logfile, "%s\n", buf); + fprintf(logfile, "%s %s\n", timestring(), buf); else syslog(LOG_WARNING, "%s", buf); } @@ -142,16 +162,17 @@ void Log_info(const char *logstring, ...) char buf[STRSIZE + 1]; int offset = 0; - va_start(argp, logstring); - offset = sprintf(buf, "INFO: "); + if (termprint || logfile) + offset = sprintf(buf, "INFO: "); + + va_start(argp, logstring); vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); + if (termprint) fprintf(stderr, "%s\n", buf); - else if (logfile) { - fprintf(logfile, "%s\n", buf); - fflush(logfile); - } + else if (logfile) + fprintf(logfile, "%s %s\n", timestring(), buf); else syslog(LOG_INFO, "%s", buf); } @@ -162,10 +183,13 @@ void Log_info_client(client_t *client, const char *logstring, ...) char buf[STRSIZE + 1]; int offset = 0; + if (termprint || logfile) + offset = sprintf(buf, "INFO: "); + va_start(argp, logstring); - offset = sprintf(buf, "INFO: "); offset += vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); + offset += snprintf(&buf[offset], STRSIZE - offset, " - [%d] %s@%s:%d", client->sessionId, client->username == NULL ? "" : client->username, @@ -174,7 +198,7 @@ void Log_info_client(client_t *client, const char *logstring, ...) if (termprint) fprintf(stderr, "%s\n", buf); else if (logfile) - fprintf(logfile, "%s\n", buf); + fprintf(logfile, "%s %s\n", timestring(), buf); else syslog(LOG_INFO, "%s", buf); } @@ -186,14 +210,16 @@ void Log_debug(const char *logstring, ...) char buf[STRSIZE + 1]; int offset = 0; - va_start(argp, logstring); - offset = sprintf(buf, "DEBUG: "); + if (termprint || logfile) + offset = sprintf(buf, "DEBUG: "); + + va_start(argp, logstring); vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); if (termprint) fprintf(stderr, "%s\n", buf); else if (logfile) - fprintf(logfile, "%s\n", buf); + fprintf(logfile, "%s %s\n", timestring(), buf); else syslog(LOG_DEBUG, "%s", buf); } @@ -204,19 +230,25 @@ void Log_fatal(const char *logstring, ...) va_list argp; char buf[STRSIZE + 1]; int offset = 0; - va_start(argp, logstring); - offset = sprintf(buf, "FATAL: "); + + if (termprint || logfile) + offset = sprintf(buf, "FATAL: "); + + va_start(argp, logstring); vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); + if (termprint) fprintf(stderr, "%s\n", buf); else if (logfile) - fprintf(logfile, "%s\n", buf); - else { /* If logging subsystem is not initialized, fall back to syslog logging - * for fatal errors. Only config file reading that needs this currently. + fprintf(logfile, "%s %s\n", timestring(), buf); + else { /* If logging subsystem is not initialized, fall back to stderr + + * syslog logging for fatal errors. */ - if (!init) + if (!init) { openlog("uMurmurd", LOG_PID, LOG_DAEMON); + fprintf(stderr, "%s\n", buf); + } syslog(LOG_CRIT, "%s", buf); }