X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Flog.c;h=5d2af2b8bf295e2b80f1fc0c7c6dfcdd540c8563;hb=ed9b54bd89b704e6111acbd91b6a7b50e3c30cf5;hp=9dceaad67bfa7cfdc34651f5608dcd47c81bf875;hpb=bee8601f8bc30306ea8839c4de0d06d732a2a6ba;p=umurmur.git diff --git a/src/log.c b/src/log.c index 9dceaad..5d2af2b 100644 --- a/src/log.c +++ b/src/log.c @@ -1,5 +1,5 @@ -/* Copyright (C) 2009, Martin Johansson - Copyright (C) 2005-2009, Thorvald Natvig +/* Copyright (C) 2009-2010, Martin Johansson + Copyright (C) 2005-2010, Thorvald Natvig All rights reserved. @@ -37,7 +37,7 @@ #include "log.h" -#define BUFSIZE 254 +#define STRSIZE 254 static bool_t termprint; @@ -45,7 +45,7 @@ void Log_init(bool_t terminal) { termprint = terminal; if (!termprint) - openlog("uMurmud", LOG_PID, LOG_DAEMON); + openlog("uMurmurd", LOG_PID, LOG_DAEMON); } void Log_free() @@ -58,84 +58,100 @@ void Log_free() void logthis(const char *logstring, ...) { va_list argp; - char buf[BUFSIZE + 2]; + char buf[STRSIZE + 1]; va_start(argp, logstring); - vsnprintf(&buf[0], BUFSIZE, logstring, argp); + vsnprintf(&buf[0], STRSIZE, logstring, argp); va_end(argp); - strcat(buf, "\n"); if (termprint) - fprintf(stderr, "%s", buf); /* XXX - other targets for logging */ + fprintf(stderr, "%s\n", buf); else - syslog(LOG_INFO, buf); + syslog(LOG_INFO, "%s", buf); } void Log_warn(const char *logstring, ...) { va_list argp; - char buf[BUFSIZE + 2]; + char buf[STRSIZE + 1]; int offset = 0; va_start(argp, logstring); offset = sprintf(buf, "WARN: "); - vsnprintf(&buf[offset], BUFSIZE - offset, logstring, argp); + vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); - strcat(buf, "\n"); if (termprint) - fprintf(stderr, "%s", buf); /* XXX - other targets for logging */ + fprintf(stderr, "%s\n", buf); else - syslog(LOG_WARNING, buf); + syslog(LOG_WARNING, "%s", buf); } void Log_info(const char *logstring, ...) { va_list argp; - char buf[BUFSIZE + 2]; + char buf[STRSIZE + 1]; int offset = 0; va_start(argp, logstring); offset = sprintf(buf, "INFO: "); - vsnprintf(&buf[offset], BUFSIZE - offset, logstring, argp); + vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); - strcat(buf, "\n"); if (termprint) - fprintf(stderr, "%s", buf); /* XXX - other targets for logging */ + fprintf(stderr, "%s\n", buf); else - syslog(LOG_INFO, buf); + syslog(LOG_INFO, "%s", buf); +} +void Log_info_client(client_t *client, const char *logstring, ...) +{ + va_list argp; + char buf[STRSIZE + 1]; + int offset = 0; + + 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, + inet_ntoa(client->remote_tcp.sin_addr), + ntohs(client->remote_tcp.sin_port)); + if (termprint) + fprintf(stderr, "%s\n", buf); + else + syslog(LOG_INFO, "%s", buf); + } #ifdef DEBUG void Log_debug(const char *logstring, ...) { va_list argp; - char buf[BUFSIZE + 2]; + char buf[STRSIZE + 1]; int offset = 0; va_start(argp, logstring); offset = sprintf(buf, "DEBUG: "); - vsnprintf(&buf[offset], BUFSIZE - offset, logstring, argp); + vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); - strcat(buf, "\n"); if (termprint) - fprintf(stderr, "%s", buf); /* XXX - other targets for logging */ + fprintf(stderr, "%s\n", buf); else - syslog(LOG_DEBUG, buf); + syslog(LOG_DEBUG, "%s", buf); } #endif void Log_fatal(const char *logstring, ...) { va_list argp; - char buf[BUFSIZE + 2]; + char buf[STRSIZE + 1]; int offset = 0; va_start(argp, logstring); offset = sprintf(buf, "FATAL: "); - vsnprintf(&buf[offset], BUFSIZE - offset, logstring, argp); + vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp); va_end(argp); - strcat(buf, "\n"); if (termprint) - fprintf(stderr, "%s", buf); /* XXX - other targets for logging */ + fprintf(stderr, "%s\n", buf); else - syslog(LOG_CRIT, buf); + syslog(LOG_CRIT, "%s", buf); exit(1); }