Index: /trunk/psLib/src/sys/psLogMsg.c
===================================================================
--- /trunk/psLib/src/sys/psLogMsg.c	(revision 7567)
+++ /trunk/psLib/src/sys/psLogMsg.c	(revision 7568)
@@ -1,3 +1,2 @@
-
 /** @file  psLogMsg.c
  *  @brief Procedures for logging messages.
@@ -12,20 +11,10 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.53 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-02 21:33:34 $
+ *  @version $Revision: 1.54 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-15 00:14:49 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
  */
 
-/*****************************************************************************
-NOTES: currently, the prototype code has the following global variables:
-    static psS32 p_psGlobalLogDest;
-    static psS32 p_psGlobalLogLevel;
-    static psS32 p_psLogTime;
-    static psS32 p_psLogHost;
-    static psS32 p_psLogLevel;
-    static psS32 p_psLogName;
-    static psS32 p_psLogMsg;
- *****************************************************************************/
 #include "config.h"
 
@@ -39,7 +28,10 @@
 #include <fcntl.h>
 
+#include "psMemory.h"
 #include "psLogMsg.h"
 #include "psError.h"
 #include "psTrace.h"
+#include "psList.h"
+#include "psString.h"
 
 #include "psErrorText.h"
@@ -50,7 +42,6 @@
 #define MAX_LOG_LINE_LENGTH 256
 
-static FILE *logDest = (FILE *) 1;      // flag to initialize to stderr before using.
-static int logFD = 0;
-static psS32 globalLogLevel = PS_LOG_INFO;        // log all messages at this or above
+static int logFD = 2;                   // Log file descriptor
+static psS32 globalLogLevel = PS_LOG_INFO; // log all messages at this or above
 static psBool logTime = true;     // Flag to include time info
 static psBool logHost = true;     // Flag to include host info
@@ -84,5 +75,5 @@
 int psLogGetLevel()
 {
-    return (globalLogLevel);
+    return globalLogLevel;
 }
 
@@ -96,56 +87,20 @@
 bool psLogSetDestination(int fd)
 {
-    FILE *fp;
-    // if logDest has not been initialized, do so before using it
-    if (logDest == (FILE *) 1) {
-        logDest = stderr;
-        logFD = 2;
-    }
-    if (fd == -1)
+    if (fd <= -1) {
         return false;
-
-    if ( fd == 1 ) {
-        //        fp = stdout;
-        if (logDest != NULL && logDest != stderr && logDest != stdout) {
-            fclose(logDest);
-        }
-        logDest = stdout;
-        logFD = 1;
-    } else if (fd == 2) {
-        //        fp = stderr;
-        if (logDest != NULL && logDest != stderr && logDest != stdout) {
-            fclose(logDest);
-        }
-        logDest = stderr;
-        logFD = 2;
-    } else if (fd == 0) {
-        //        fp = NULL;
-        if (logDest != NULL && logDest != stderr && logDest != stdout) {
-            fclose(logDest);
-        }
-        logDest = NULL;
-        logFD = 0;
-        //        return true;
-    } else if (fd > 2) {
-        if (logDest != NULL && logDest != stderr && logDest != stdout) {
-            fclose(logDest);
-        }
-        fp = fdopen(fd, "w");
-        logDest = fp;
-        logFD = fd;
-        //        fclose(fp);
-        //        return true;
-    }
+    }
+
+    // Close the current FD if it's not stdout, stderr.
+    if (logFD > 2) {
+        close(logFD);
+    }
+    logFD = fd;
 
     return true;
-
 }
 
 int psLogGetDestination(void)
 {
-    if (logDest == (FILE *) 1) {
-        logFD = 1;
-    }
-    return (logFD);
+    return logFD;
 }
 
@@ -222,44 +177,30 @@
 }
 
-int psMessageDestination (const char *dest)
-{
-    char protocol[5];
-    char location[257];
-    if (logDest == (FILE *) 1) {
-        logDest = stderr;
-    }
-
+int psMessageDestination(const char *dest)
+{
+    // No destination
     if (dest == NULL || strcmp(dest, "none") == 0) {
-        if (logDest != NULL && logDest != stderr && logDest != stdout) {
-            fclose(logDest);
-        }
-        logDest = NULL;
         return 0;
     }
-    if (sscanf(dest, "%4s:%256s", protocol, location) < 2) {
-        psError(PS_ERR_LOCATION_INVALID, true,
-                PS_ERRORTEXT_psLogMsg_DESTINATION_MALFORMED,
-                dest);
+
+    // Special destinations: stdout, stderr
+    if (strcmp(dest, "stdout") == 0) {
+        return 1;
+    }
+    if (strcmp(dest, "stderr") == 0) {
+        return 2;
+    }
+
+    // Destination is protocol:location
+    psList *protocolLocation = psStringSplit(dest, ":", false); // A list containing the protocol and location
+    if (protocolLocation->n != 2) {
+        psFree(protocolLocation);
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unable to parse protocol:location from: %s\n", dest);
         return -1;
     }
-    if (strcmp(protocol, "dest") == 0) {
-        if (strcmp(location, "stderr") == 0) {
-            if (logDest != NULL && logDest != stderr && logDest != stdout) {
-                fclose(logDest);
-            }
-            logDest = stderr;
-            return 2;
-        }
-        if (strcmp(location, "stdout") == 0) {
-            if (logDest != NULL && logDest != stderr && logDest != stdout) {
-                fclose(logDest);
-            }
-            logDest = stdout;
-            return 1;
-        }
-        psError(PS_ERR_LOCATION_INVALID, true, PS_ERRORTEXT_psLogMsg_DEST_LOCATION_INVALID,
-                location);
-        return -1;
-    } else if (strcmp(protocol, "file") == 0) {
+    const char *protocol = psListGet(protocolLocation, PS_LIST_HEAD); // The protocol
+    const char *location = psListGet(protocolLocation, PS_LIST_TAIL); // The location
+
+    if (strcmp(protocol, "file") == 0) {
         //        FILE *file = fopen(location, "w");
         int fileD = creat(location, 0666);
@@ -268,12 +209,13 @@
             psError(PS_ERR_IO, true, PS_ERRORTEXT_psLogMsg_OPEN_FILE_FAILED,
                     location);
+            psFree(protocolLocation);
             return -1;
         }
-        if (logDest != NULL && logDest != stderr && logDest != stdout) {
-            fclose(logDest);
-        }
-        logDest = fdopen(fileD, "w");
+        psFree(protocolLocation);
         return fileD;
     }
+
+    psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unrecognised protocol: %s\n", protocol);
+    psFree(protocolLocation);
     return -1;
 }
@@ -284,5 +226,5 @@
 
 /*****************************************************************************
-psVLogMsg(): This routine sends the message, which is a printf style string
+psLogMsgV(): This routine sends the message, which is a printf style string
 specified in the "..." argument, to the current message log destination with
 the severity specified by the "level" argument.
@@ -306,14 +248,10 @@
     struct tm *utc = gmtime(&clock);    // The current gm time.
 
-    // if logDest has not been initialized, do so before using it
-    if (logDest == (FILE *) 1) {
-        logDest = stderr;
-    }
     // If it's an abort, we always want to see the message
-    if (logDest == NULL && level == PS_LOG_ABORT) {
-        logDest = stderr;
+    if (logFD == 0 && level == PS_LOG_ABORT) {
+        logFD = 2;
     }
     // If logging is off, or if the level is too high, return immediately.
-    if ((level > globalLogLevel) || (logDest == NULL)) {
+    if ((level > globalLogLevel) || (logFD == 0)) {
         return;
     }
@@ -395,5 +333,5 @@
     *head_ptr = '\0';
 
-    fputs(head, logDest);
+    write(logFD, head, strlen(head));
     if (logMsg) {
         char msg[1024];
@@ -402,15 +340,25 @@
 
         // detect multiple lines in message and indent each line by 4 spaces.
-        char* line = strtok_r(msg,"\n",&msgPtr);
-        while (line != NULL) {
-            fprintf(logDest,"    %s\n",line);
-            line = strtok_r(NULL,"\n",&msgPtr);
-        }
+        char *line = strtok_r(msg, "\n", &msgPtr);
+        do {
+            write(logFD, "    ", 4);
+            write(logFD, line, strlen(line));
+            write(logFD, "\n", 1);
+        } while ((line = strtok_r(NULL, "\n", &msgPtr)));
     } else {
-        fputc('\n', logDest);
+        write(logFD, "\n", 1);
     }
 
     if (level == PS_LOG_ABORT) {
-        fflush(logDest);
+        switch (logFD) {
+        case 1:
+            fflush(stdout);
+            break;
+        case 2:
+            fflush(stderr);
+            break;
+            // For the others, write() should send it unbuffered...?
+        }
+
     }
 }
