IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 9, 2004, 3:01:28 PM (22 years ago)
Author:
rhl
Message:

Added psSetLogFormat()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/archive/pslib/src/Utils/logmsg.c

    r141 r174  
    5656
    5757/*****************************************************************************/
     58/*
     59 * Set the format of psLogMsg output.
     60 *
     61 * More precisely, provide a string consisting of the letters H (host), L (level), M (message),
     62 * N (name), and T (time).  The default is "HLMNT"
     63 */
     64static int log_time = 1, log_host = 1, log_level = 1, log_name = 1, log_msg = 1;
     65
     66void psSetLogFormat(const char *fmt)
     67{
     68    int nlog_time, nlog_host, nlog_level, nlog_name, nlog_msg; // new values of log_time etc.
     69
     70    nlog_host = nlog_level = nlog_msg = nlog_name = nlog_time = 0; // don't log anything
     71    for (const char *ptr = fmt; *ptr != '\0'; ptr++) {
     72        switch (*ptr) {
     73          case 'H': case 'h':
     74            nlog_host = 1;
     75            break;
     76          case 'L': case 'l':
     77            nlog_level = 1;
     78            break;
     79          case 'M': case 'm':
     80            nlog_msg = 1;
     81            break;
     82          case 'N': case 'n':
     83            nlog_name = 1;
     84            break;
     85          case 'T': case 't':
     86            nlog_time = 1;
     87            break;
     88          default:
     89            psError(__func__, "Unknown logging keyword %c", *ptr);
     90            break;
     91        }
     92    }
     93
     94    if (!nlog_msg) {
     95        psTrace("utils.logMsg", 1, "You must at least log error messages (You chose \"%s\")", fmt);
     96    }
     97
     98    log_host = nlog_host;
     99    log_level = nlog_level;
     100    log_msg = nlog_msg;
     101    log_name = nlog_name;
     102    log_time = nlog_time;
     103}
     104
     105
     106/*****************************************************************************/
    58107#if !defined(HOST_NAME_MAX)             // should be in limits.h
    59108#  define HOST_NAME_MAX 256
     
    109158
    110159    char head[HOST_NAME_MAX + 40];      // yes, this is long enough
     160    char *head_ptr = head;              // where we've got to in head
    111161    time_t clock = time(NULL);
    112162    struct tm *utc = gmtime(&clock);
    113163
    114     sprintf(head, "%4d:%02d:%02d %02d:%02d:%02dZ|%-20s|%c|%-15s|",
    115             utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday,
    116             utc->tm_hour, utc->tm_min, utc->tm_sec,
    117             hostname, clevel, name);
    118 
     164    if (log_time) {
     165        sprintf(head_ptr, "%4d:%02d:%02d %02d:%02d:%02dZ",
     166                utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday,
     167                utc->tm_hour, utc->tm_min, utc->tm_sec);
     168        head_ptr += strlen(head_ptr);
     169    }
     170    if (log_host) {
     171        if (head_ptr > head) {
     172            *head_ptr++ = '|';
     173        }
     174        sprintf(head_ptr, "%-20s", hostname);
     175        head_ptr += strlen(head_ptr);
     176    }
     177    if (log_level) {
     178        if (head_ptr > head) {
     179            *head_ptr++ = '|';
     180        }
     181        sprintf(head_ptr, "%c", clevel);
     182        head_ptr += strlen(head_ptr);
     183    }
     184    if (log_name) {
     185        if (head_ptr > head) {
     186            *head_ptr++ = '|';
     187        }
     188        sprintf(head_ptr, "%-15s", name);
     189        head_ptr += strlen(head_ptr);
     190    }
     191    if (head_ptr > head) {
     192        *head_ptr++ = '|';
     193    }
     194    *head_ptr = '\0';
     195   
    119196    switch (logDest) {
    120197      case PS_LOG_TO_STDOUT:
     
    139216        abort();
    140217    }
    141    
    142     va_end(ap);
    143218}
    144219
     
    149224
    150225    p_psVLogMsg(name, level, fmt, ap);
     226    va_end(ap);
    151227}
    152228
     
    156232int main(void)
    157233{
    158     psSetTraceLevel("utils.logMsg", 10);
     234    psSetTraceLevel("utils.logMsg", 1);
    159235   
    160236    psSetLogDestination(PS_LOG_TO_STDERR);
    161237    psSetLogLevel(PS_LOG_INFO);
     238    psSetLogFormat("NM");
    162239   
    163240    psLogMsg("aaa", PS_LOG_INFO, "Hello World\n");
Note: See TracChangeset for help on using the changeset viewer.