IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 11, 2004, 7:50:01 PM (22 years ago)
Author:
gusciora
Message:

cosmetics...

File:
1 edited

Legend:

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

    r979 r1013  
    1 /*****************************************************************************
    2     A simple implementation of a logging facility for Pan-STARRS
    3  
     1/** @file  psLogMsg.c
     2 *  @brief Procedures for logging messages.
     3 *  \ingroup LogTrace
     4 *
     5 *  This file will hold the prototypes for defining procedure which set
     6 *  message log levels, messahe log formats, message log destinations, and
     7 *  for generating the messages themselves.
     8 *  @ingroup LogTrace
     9 *
     10 *  @author Robert Lupton, Princeton University
     11 *  @author George Gusciora, MHPCC
     12 *
     13 *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
     14 *  @date $Date: 2004-06-12 05:50:01 $
     15 *
     16 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     17 */
     18
     19/*****************************************************************************
    420NOTES: currently, the prototype code has the following global variables:
    5     static int logDest;
    6     static int logLevel;
    7     static int log_time;
    8     static int log_host;
    9     static int log_level;
    10     static int log_name;
    11     static int log_msg;
     21    static int p_psGlobalLogDest;
     22    static int p_psGlobalLogLevel;
     23    static int p_psLogTime;
     24    static int p_psLogHost;
     25    static int p_psLogLevel;
     26    static int p_psLogName;
     27    static int p_psLogMsg;
    1228 *****************************************************************************/
    1329#include <limits.h>
     
    1834#include <time.h>
    1935#include <unistd.h>
    20 
    21 /* #include "psLib.h" */
    2236#include "psLogMsg.h"
    2337#include "psError.h"
     
    2741#define MIN_LOG_LEVEL 0
    2842#define MAX_LOG_LEVEL 9
    29 static int logDest = PS_LOG_TO_STDERR; // where to log messages
    30 static int logLevel = PS_LOG_INFO;     // log all messages at this or above
     43static int p_psGlobalLogDest = PS_LOG_TO_STDERR; // where to log messages
     44static int p_psGlobalLogLevel = PS_LOG_INFO;     // log all messages at this or above
    3145//static FILE *p_logFP = NULL;
    3246// The following variables control
    3347// which information is displayed in
    3448// log messages.
    35 static int log_time = 1;                // Flag to include time info
    36 static int log_host = 1;                // Flag to include host info
    37 static int log_level = 1;               // Flag to include level info
    38 static int log_name = 1;                // Flag to include name info
    39 static int log_msg = 1;                 // Flag to include message info
     49static int p_psLogTime = 1;                // Flag to include time info
     50static int p_psLogHost = 1;                // Flag to include host info
     51static int p_psLogLevel = 1;               // Flag to include level info
     52static int p_psLogName = 1;                // Flag to include name info
     53static int p_psLogMsg = 1;                 // Flag to include message info
    4054/*****************************************************************************
    4155psLogSetLevel(): Set the current log level and return old level.
     
    4963int psLogSetLevel(int level)
    5064{
    51     int oldLevel = logLevel;
     65    // Save old global log level for changing it.
     66    int oldLevel = p_psGlobalLogLevel;
    5267
    5368    if ((level < MIN_LOG_LEVEL) || (level > MAX_LOG_LEVEL)) {
     
    5772    }
    5873
    59     logLevel = level;
     74    // Set new global log level
     75    p_psGlobalLogLevel = level;
     76
     77    // Return old global log level
    6078    return oldLevel;
    6179}
     
    7593int psLogSetDestination(int dest)
    7694{
    77     int old = logDest;
     95    // Save old global log destination before changing it.
     96    int old = p_psGlobalLogDest;
    7897
    7998    switch (dest) {
     
    81100    case PS_LOG_TO_STDOUT:
    82101    case PS_LOG_TO_STDERR:
    83         logDest = dest;
     102        // Set new global log destination
     103        p_psGlobalLogDest = dest;
    84104        break;
    85105
    86106    default:
    87107        // GUS: Should you log this error properly?
    88         fprintf(stderr,"Unknown logDestination: %d (ignored)\n", dest);
    89         break;
    90     }
    91 
     108        fprintf(stderr,"Unknown p_psGlobalLogDest: %d (ignored)\n", dest);
     109        break;
     110    }
     111
     112    // Return old global log destination
    92113    return old;
    93114}
     
    112133void psLogSetFormat(const char *fmt)
    113134{
    114     int nlog_time = 0;
    115     int nlog_host = 0;
    116     int nlog_level = 0;
    117     int nlog_name = 0;
    118     int nlog_msg = 0;
     135
     136    // Step through each character in the format string.  For each letter
     137    // in that string, set/unset the appropriate logging.
    119138
    120139    for (const char *ptr = fmt; *ptr != '\0'; ptr++) {
     
    122141        case 'H':
    123142        case 'h':
    124             nlog_host = 1;
     143            p_psLogHost = 1;
    125144            break;
    126145        case 'L':
    127146        case 'l':
    128             nlog_level = 1;
     147            p_psLogLevel = 1;
    129148            break;
    130149        case 'M':
    131150        case 'm':
    132             nlog_msg = 1;
     151            p_psLogMsg = 1;
    133152            break;
    134153        case 'N':
    135154        case 'n':
    136             nlog_name = 1;
     155            p_psLogName = 1;
    137156            break;
    138157        case 'T':
    139158        case 't':
    140             nlog_time = 1;
     159            p_psLogTime = 1;
    141160            break;
    142161        default:
    143             // GUS: figure out the psError() format:
    144             //     psError(__func__, PS_ERR_UNKNOWN, 1,
    145             //            "Unknown logging keyword %c", *ptr);
    146162            psError(__func__, "Unknown logging keyword %c", *ptr);
    147163            break;
     
    149165    }
    150166
    151     if (!nlog_msg) {
     167    if (!p_psLogMsg) {
    152168        psTrace("utils.logMsg", 1,
    153169                "You must at least log error messages (You chose \"%s\")",
    154170                fmt);
    155171    }
    156 
    157     log_host = nlog_host;
    158     log_level = nlog_level;
    159     log_msg = nlog_msg;
    160     log_name = nlog_name;
    161     log_time = nlog_time;
    162172}
    163173
     
    186196               va_list ap)
    187197{
    188     static int first = 1;
     198    static int first = 1;               // Flag for calling gethostname()
    189199    static char hostname[HOST_NAME_MAX + 1];
     200    // Buffer for hostname.
    190201    char clevel=0;                      // letter-name for level
    191202    char head[HOST_NAME_MAX + 40];      // yes, this is long enough
    192203    char *head_ptr = head;              // where we've got to in head
    193     time_t clock = time(NULL);
    194     struct tm *utc = gmtime(&clock);
    195 
    196     if ((level > logLevel) || (logDest == PS_LOG_NONE)) {
     204    time_t clock = time(NULL);          // The current time.
     205    struct tm *utc = gmtime(&clock);    // The current gm time.
     206
     207    // If logging is off, or if the level is too high, return immediately.
     208    if ((level > p_psGlobalLogLevel) || (p_psGlobalLogDest == PS_LOG_NONE)) {
    197209        return;
    198210    }
    199211
     212    // If I have not been here yet, determine my hostname and save it.
    200213    if (first) {
    201214        first = 0;
     
    236249    }
    237250
    238 
    239     if (log_time) {
     251    // Create the various log fields...
     252    if (p_psLogTime) {
    240253        sprintf(head_ptr, "%4d:%02d:%02d %02d:%02d:%02dZ",
    241254                utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday,
     
    243256        head_ptr += strlen(head_ptr);
    244257    }
    245     if (log_host) {
     258    // Hostname should be 20 characters.
     259    if (p_psLogHost) {
    246260        if (head_ptr > head) {
    247261            *head_ptr++ = '|';
     
    250264        head_ptr += strlen(head_ptr);
    251265    }
    252     if (log_level) {
     266    if (p_psLogLevel) {
    253267        if (head_ptr > head) {
    254268            *head_ptr++ = '|';
     
    257271        head_ptr += strlen(head_ptr);
    258272    }
    259     if (log_name) {
     273    // The name field must be 15 characters.
     274    if (p_psLogName) {
    260275        if (head_ptr > head) {
    261276            *head_ptr++ = '|';
    262277        }
    263         sprintf(head_ptr, "%-15s", name);
     278        sprintf(head_ptr, "%15.15s", name);
    264279        head_ptr += strlen(head_ptr);
    265280    }
     
    270285    *head_ptr = '\0';
    271286
    272     switch (logDest) {
     287    switch (p_psGlobalLogDest) {
    273288    case PS_LOG_TO_STDOUT:
    274289        puts(head);
    275         if (log_msg) {
     290        if (p_psLogMsg) {
    276291            vprintf(fmt, ap);
    277292            if (fmt[strlen(fmt) - 1] != '\n') {
     
    285300    case PS_LOG_TO_STDERR:
    286301        fputs(head, stderr);
    287         if (log_msg) {
     302        if (p_psLogMsg) {
    288303            vfprintf(stderr, fmt, ap);
    289304            if (fmt[strlen(fmt) - 1] != '\n') {
Note: See TracChangeset for help on using the changeset viewer.