Switch to syslog for logging messages.
[matches/MCTX3420.git] / server / log.c
index d964e61..d41021f 100644 (file)
@@ -4,24 +4,20 @@
  */
 
 
-#include <unistd.h>
-#include <stdarg.h>
-
 // --- Custom headers --- //
 #include "common.h"
 #include "log.h"
 #include "options.h"
 
-// --- Static variables --- //
-static const char * unspecified_funct = "???";
-
-// --- Function implementations --- //
+#include <unistd.h>
+#include <syslog.h>
+#include <stdarg.h>
 
-//TODO: Migrate to syslog (shouldn't be too hard; these functions basically do what syslog does)
-//             Note that we will want to have a seperate log as well as syslog; give the user the option to view the log using the GUI
+static const char * unspecified_funct = "???";
 
 /**
- * Print a message to stderr
+ * Print a message to stderr and log it via syslog. The message must be
+ * less than BUFSIZ characters long, or it will be truncated.
  * @param level - Specify how severe the message is.
        If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed
  * @param funct - String indicating the function name from which this function was called.
@@ -31,17 +27,23 @@ static const char * unspecified_funct = "???";
  */
 void LogEx(int level, const char * funct, ...)
 {
+       //Todo: consider setlogmask(3) to filter messages
        const char *fmt;
+       char buffer[BUFSIZ];
        va_list va;
+
+       // Don't print the message unless we need to
+       if (level > g_options.verbosity)
+               return;
+
        va_start(va, funct);
        fmt = va_arg(va, const char*);
        
        if (fmt == NULL) // sanity check
-               FatalEx("Log", "Format string is NULL");
+               Fatal("Format string is NULL");
 
-       // Don't print the message unless we need to
-       if (level > g_options.verbosity)
-               return;
+       vsnprintf(buffer, BUFSIZ, fmt, va);
+       va_end(va);
 
        if (funct == NULL)
                funct = unspecified_funct;
@@ -51,31 +53,28 @@ void LogEx(int level, const char * funct, ...)
        switch (level)
        {
                case LOGERR:
+                       level = LOG_ERR;
                        severity = "ERROR";
                        break;
                case LOGWARN:
+                       level = LOG_WARNING;
                        severity = "WARNING";
                        break;
                case LOGNOTE:
+                       level = LOG_NOTICE;
                        severity = "NOTICE";
                        break;
                case LOGINFO:
+                       level = LOG_INFO;
                        severity = "INFO";
                        break;
                default:
+                       level = LOG_DEBUG;
                        severity = "DEBUG";
                        break;
        }
 
-       // Print: Program name, PID, severity string, function name first
-       fprintf(stderr, "%s [%d] : %s : %s - ", g_options.program, getpid(), severity, funct);
-
-       // Then pass additional arguments with the format string to vfprintf for printing
-       vfprintf(stderr, fmt, va);
-       va_end(va);
-
-       // End log messages with a newline
-       fprintf(stderr, "\n");
+       syslog(level, "%s: %s - %s", severity, funct, buffer);
 }
 
 /**
@@ -88,6 +87,7 @@ void LogEx(int level, const char * funct, ...)
 void FatalEx(const char * funct, ...)
 {
        const char *fmt;
+       char buffer[BUFSIZ];
        va_list va;
        va_start(va, funct);
        fmt = va_arg(va, const char*);
@@ -96,19 +96,17 @@ void FatalEx(const char * funct, ...)
        {
                // Fatal error in the Fatal function.
                // (This really shouldn't happen unless someone does something insanely stupid)
-               FatalEx("Fatal", "Format string is NULL");
+               Fatal("Format string is NULL");
                return; // Should never get here
        }
 
+       vsnprintf(buffer, BUFSIZ, fmt,va);
+       va_end(va);
+
        if (funct == NULL)
                funct = unspecified_funct;
 
-       fprintf(stderr, "%s [%d] : %s : FATAL - ", g_options.program, getpid(), funct);
-
-       vfprintf(stderr, fmt, va);
-       va_end(va);
-       fprintf(stderr, "\n");
-
+       syslog(LOG_CRIT, "FATAL: %s - %s", funct, buffer);
        exit(EXIT_FAILURE);
 }
 

UCC git Repository :: git.ucc.asn.au