IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7568


Ignore:
Timestamp:
Jun 14, 2006, 2:14:49 PM (20 years ago)
Author:
Paul Price
Message:

Overhauled in preparation for the use of sockets, etc, for log
messages. Pulled out the use of streams (FILE*) in favour of file
descriptors (previously, was based on streams, though FDs were carried
around). Reworked psMessageDestination to correspond to the
description in the SDRS: it does the open and returns an FD.
psLogSetDestination does NOT do the open, but only works with an
already extant FD.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psLogMsg.c

    r7300 r7568  
    1 
    21/** @file  psLogMsg.c
    32 *  @brief Procedures for logging messages.
     
    1211 *  @author GLG, MHPCC
    1312 *
    14  *  @version $Revision: 1.53 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2006-06-02 21:33:34 $
     13 *  @version $Revision: 1.54 $ $Name: not supported by cvs2svn $
     14 *  @date $Date: 2006-06-15 00:14:49 $
    1615 *
    1716 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
    1817 */
    1918
    20 /*****************************************************************************
    21 NOTES: currently, the prototype code has the following global variables:
    22     static psS32 p_psGlobalLogDest;
    23     static psS32 p_psGlobalLogLevel;
    24     static psS32 p_psLogTime;
    25     static psS32 p_psLogHost;
    26     static psS32 p_psLogLevel;
    27     static psS32 p_psLogName;
    28     static psS32 p_psLogMsg;
    29  *****************************************************************************/
    3019#include "config.h"
    3120
     
    3928#include <fcntl.h>
    4029
     30#include "psMemory.h"
    4131#include "psLogMsg.h"
    4232#include "psError.h"
    4333#include "psTrace.h"
     34#include "psList.h"
     35#include "psString.h"
    4436
    4537#include "psErrorText.h"
     
    5042#define MAX_LOG_LINE_LENGTH 256
    5143
    52 static FILE *logDest = (FILE *) 1;      // flag to initialize to stderr before using.
    53 static int logFD = 0;
    54 static psS32 globalLogLevel = PS_LOG_INFO;        // log all messages at this or above
     44static int logFD = 2;                   // Log file descriptor
     45static psS32 globalLogLevel = PS_LOG_INFO; // log all messages at this or above
    5546static psBool logTime = true;     // Flag to include time info
    5647static psBool logHost = true;     // Flag to include host info
     
    8475int psLogGetLevel()
    8576{
    86     return (globalLogLevel);
     77    return globalLogLevel;
    8778}
    8879
     
    9687bool psLogSetDestination(int fd)
    9788{
    98     FILE *fp;
    99     // if logDest has not been initialized, do so before using it
    100     if (logDest == (FILE *) 1) {
    101         logDest = stderr;
    102         logFD = 2;
    103     }
    104     if (fd == -1)
     89    if (fd <= -1) {
    10590        return false;
    106 
    107     if ( fd == 1 ) {
    108         //        fp = stdout;
    109         if (logDest != NULL && logDest != stderr && logDest != stdout) {
    110             fclose(logDest);
    111         }
    112         logDest = stdout;
    113         logFD = 1;
    114     } else if (fd == 2) {
    115         //        fp = stderr;
    116         if (logDest != NULL && logDest != stderr && logDest != stdout) {
    117             fclose(logDest);
    118         }
    119         logDest = stderr;
    120         logFD = 2;
    121     } else if (fd == 0) {
    122         //        fp = NULL;
    123         if (logDest != NULL && logDest != stderr && logDest != stdout) {
    124             fclose(logDest);
    125         }
    126         logDest = NULL;
    127         logFD = 0;
    128         //        return true;
    129     } else if (fd > 2) {
    130         if (logDest != NULL && logDest != stderr && logDest != stdout) {
    131             fclose(logDest);
    132         }
    133         fp = fdopen(fd, "w");
    134         logDest = fp;
    135         logFD = fd;
    136         //        fclose(fp);
    137         //        return true;
    138     }
     91    }
     92
     93    // Close the current FD if it's not stdout, stderr.
     94    if (logFD > 2) {
     95        close(logFD);
     96    }
     97    logFD = fd;
    13998
    14099    return true;
    141 
    142100}
    143101
    144102int psLogGetDestination(void)
    145103{
    146     if (logDest == (FILE *) 1) {
    147         logFD = 1;
    148     }
    149     return (logFD);
     104    return logFD;
    150105}
    151106
     
    222177}
    223178
    224 int psMessageDestination (const char *dest)
    225 {
    226     char protocol[5];
    227     char location[257];
    228     if (logDest == (FILE *) 1) {
    229         logDest = stderr;
    230     }
    231 
     179int psMessageDestination(const char *dest)
     180{
     181    // No destination
    232182    if (dest == NULL || strcmp(dest, "none") == 0) {
    233         if (logDest != NULL && logDest != stderr && logDest != stdout) {
    234             fclose(logDest);
    235         }
    236         logDest = NULL;
    237183        return 0;
    238184    }
    239     if (sscanf(dest, "%4s:%256s", protocol, location) < 2) {
    240         psError(PS_ERR_LOCATION_INVALID, true,
    241                 PS_ERRORTEXT_psLogMsg_DESTINATION_MALFORMED,
    242                 dest);
     185
     186    // Special destinations: stdout, stderr
     187    if (strcmp(dest, "stdout") == 0) {
     188        return 1;
     189    }
     190    if (strcmp(dest, "stderr") == 0) {
     191        return 2;
     192    }
     193
     194    // Destination is protocol:location
     195    psList *protocolLocation = psStringSplit(dest, ":", false); // A list containing the protocol and location
     196    if (protocolLocation->n != 2) {
     197        psFree(protocolLocation);
     198        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unable to parse protocol:location from: %s\n", dest);
    243199        return -1;
    244200    }
    245     if (strcmp(protocol, "dest") == 0) {
    246         if (strcmp(location, "stderr") == 0) {
    247             if (logDest != NULL && logDest != stderr && logDest != stdout) {
    248                 fclose(logDest);
    249             }
    250             logDest = stderr;
    251             return 2;
    252         }
    253         if (strcmp(location, "stdout") == 0) {
    254             if (logDest != NULL && logDest != stderr && logDest != stdout) {
    255                 fclose(logDest);
    256             }
    257             logDest = stdout;
    258             return 1;
    259         }
    260         psError(PS_ERR_LOCATION_INVALID, true, PS_ERRORTEXT_psLogMsg_DEST_LOCATION_INVALID,
    261                 location);
    262         return -1;
    263     } else if (strcmp(protocol, "file") == 0) {
     201    const char *protocol = psListGet(protocolLocation, PS_LIST_HEAD); // The protocol
     202    const char *location = psListGet(protocolLocation, PS_LIST_TAIL); // The location
     203
     204    if (strcmp(protocol, "file") == 0) {
    264205        //        FILE *file = fopen(location, "w");
    265206        int fileD = creat(location, 0666);
     
    268209            psError(PS_ERR_IO, true, PS_ERRORTEXT_psLogMsg_OPEN_FILE_FAILED,
    269210                    location);
     211            psFree(protocolLocation);
    270212            return -1;
    271213        }
    272         if (logDest != NULL && logDest != stderr && logDest != stdout) {
    273             fclose(logDest);
    274         }
    275         logDest = fdopen(fileD, "w");
     214        psFree(protocolLocation);
    276215        return fileD;
    277216    }
     217
     218    psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unrecognised protocol: %s\n", protocol);
     219    psFree(protocolLocation);
    278220    return -1;
    279221}
     
    284226
    285227/*****************************************************************************
    286 psVLogMsg(): This routine sends the message, which is a printf style string
     228psLogMsgV(): This routine sends the message, which is a printf style string
    287229specified in the "..." argument, to the current message log destination with
    288230the severity specified by the "level" argument.
     
    306248    struct tm *utc = gmtime(&clock);    // The current gm time.
    307249
    308     // if logDest has not been initialized, do so before using it
    309     if (logDest == (FILE *) 1) {
    310         logDest = stderr;
    311     }
    312250    // If it's an abort, we always want to see the message
    313     if (logDest == NULL && level == PS_LOG_ABORT) {
    314         logDest = stderr;
     251    if (logFD == 0 && level == PS_LOG_ABORT) {
     252        logFD = 2;
    315253    }
    316254    // If logging is off, or if the level is too high, return immediately.
    317     if ((level > globalLogLevel) || (logDest == NULL)) {
     255    if ((level > globalLogLevel) || (logFD == 0)) {
    318256        return;
    319257    }
     
    395333    *head_ptr = '\0';
    396334
    397     fputs(head, logDest);
     335    write(logFD, head, strlen(head));
    398336    if (logMsg) {
    399337        char msg[1024];
     
    402340
    403341        // detect multiple lines in message and indent each line by 4 spaces.
    404         char* line = strtok_r(msg,"\n",&msgPtr);
    405         while (line != NULL) {
    406             fprintf(logDest,"    %s\n",line);
    407             line = strtok_r(NULL,"\n",&msgPtr);
    408         }
     342        char *line = strtok_r(msg, "\n", &msgPtr);
     343        do {
     344            write(logFD, "    ", 4);
     345            write(logFD, line, strlen(line));
     346            write(logFD, "\n", 1);
     347        } while ((line = strtok_r(NULL, "\n", &msgPtr)));
    409348    } else {
    410         fputc('\n', logDest);
     349        write(logFD, "\n", 1);
    411350    }
    412351
    413352    if (level == PS_LOG_ABORT) {
    414         fflush(logDest);
     353        switch (logFD) {
     354        case 1:
     355            fflush(stdout);
     356            break;
     357        case 2:
     358            fflush(stderr);
     359            break;
     360            // For the others, write() should send it unbuffered...?
     361        }
     362
    415363    }
    416364}
Note: See TracChangeset for help on using the changeset viewer.