X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Flog.c;h=f7e39bc5d58447d6dd0476bdfdf907631cf67b03;hb=496c40213e1cd6eee6cee3f1cabc307c2c893893;hp=62fe890a8f989ef3505b976551df91f556d2964d;hpb=733a9b968264c48b5340693ce17e1874c1aeff77;p=matches%2FMCTX3420.git diff --git a/server/log.c b/server/log.c index 62fe890..f7e39bc 100644 --- a/server/log.c +++ b/server/log.c @@ -5,13 +5,15 @@ #include +#include // --- Custom headers --- // +#include "common.h" #include "log.h" #include "options.h" // --- Static variables --- // -static char * unspecified_funct = (char*)"???"; +static const char * unspecified_funct = "???"; // --- Function implementations --- // @@ -24,36 +26,41 @@ static char * unspecified_funct = (char*)"???"; * @param fmt - A format string * @param ... - Arguments to be printed according to the format string */ -void Log(int level, char * funct, char * fmt, ...) +void LogEx(int level, const char * funct, ...) { + const char *fmt; + va_list va; + va_start(va, funct); + fmt = va_arg(va, const char*); + if (fmt == NULL) // sanity check - Fatal("Log", "Format string is NULL"); + FatalEx("Log", "Format string is NULL"); // Don't print the message unless we need to - if (level > g_options.verbosity) + if (level > g_options.verbosity) return; if (funct == NULL) funct = unspecified_funct; // Make a human readable severity string - char severity[BUFSIZ]; + const char *severity; switch (level) { case LOGERR: - sprintf(severity, "ERROR"); + severity = "ERROR"; break; case LOGWARN: - sprintf(severity, "WARNING"); + severity = "WARNING"; break; case LOGNOTE: - sprintf(severity, "NOTICE"); + severity = "NOTICE"; break; case LOGINFO: - sprintf(severity, "INFO"); + severity = "INFO"; break; default: - sprintf(severity, "DEBUG"); + severity = "DEBUG"; break; } @@ -61,8 +68,6 @@ void Log(int level, char * funct, char * fmt, ...) fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct); // Then pass additional arguments with the format string to vfprintf for printing - va_list va; - va_start(va, fmt); vfprintf(stderr, fmt, va); va_end(va); @@ -72,19 +77,23 @@ void Log(int level, char * funct, char * fmt, ...) /** * Handle a Fatal error in the program by printing a message and exiting the program - CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT + * CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT * @param funct - Name of the calling function * @param fmt - A format string * @param ... - Arguments to be printed according to the format string */ -void Fatal(char * funct, char * fmt, ...) +void FatalEx(const char * funct, ...) { + const char *fmt; + va_list va; + va_start(va, funct); + fmt = va_arg(va, const char*); if (fmt == NULL) { // Fatal error in the Fatal function. // (This really shouldn't happen unless someone does something insanely stupid) - Fatal("Fatal", "Format string is NULL"); + FatalEx("Fatal", "Format string is NULL"); return; // Should never get here } @@ -93,8 +102,6 @@ void Fatal(char * funct, char * fmt, ...) fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct); - va_list va; - va_start(va, fmt); vfprintf(stderr, fmt, va); va_end(va); fprintf(stderr, "\n");