IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 21, 2006, 12:11:39 PM (20 years ago)
Author:
Paul Price
Message:

Making fix using variable length arrays to concatenate strings
permanent --- it is SO much faster than calling psStringAppend (which
uses two vsnprintf calls). Cleaned up p_psTrace and psTraceV.

File:
1 edited

Legend:

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

    r8416 r8441  
    99 *  @author GLG, MHPCC
    1010 *
    11  *  @version $Revision: 1.69 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2006-08-18 02:24:13 $
     11 *  @version $Revision: 1.70 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2006-08-21 22:11:39 $
    1313 *
    1414 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3838 *****************************************************************************/
    3939
    40 
    41 // NOTE: We want to avoid using the psString functions, or any function that calls the PS memory management,
    42 // since there seems to be a problem with memory fragmentation.  Since psTrace can get called a LOT, it
    43 // manifests this problem.  To get around this, we will use variable length char arrays, instead of the
    44 // psString functions.  This can be revised once the PS memory management system is fixed.
    45 #define USE_PS_MEMORY 0
    46 
    47 
    4840#ifndef PS_NO_TRACE
    4941
     
    6153#include "psLogMsg.h"
    6254
     55#define MAX_HOSTNAME_LENGTH 256
     56#define MAX_HEADER_LENGTH 256
    6357
    6458
     
    414408    // If the component name has no leading dot, then supply it.
    415409    if (name[0] != '.') {
    416         #if USE_PS_MEMORY
    417         psString compName = (char *) psAlloc(10 + strlen(name));
    418         strcpy(compName, ".");
    419         compName = strcat(compName, name);
    420         #else
    421 
    422410        char compName[strlen(name) + 2];
    423411        compName[0] = '.';
    424412        strcpy(&compName[1], name);
    425         #endif
    426413
    427414        traceLevel = doGetTraceLevel(compName);
    428         #if USE_PS_MEMORY
    429 
    430         psFree(compName);
    431         #endif
    432 
    433415    } else {
    434416        // Search the component root tree, determine the trace level.
     
    520502              va_list ap)
    521503{
    522     // char *fmt = NULL;
    523     //    va_list ap;
    524     static psS32 first = 1;       // Flag for calling gethostname()
    525     static char hostname[256 + 1];
    526     char clevel = 0;            // letter-name for level
    527     psS32 i = 0;
    528     char head[256 + 2]; // the added two are for the ending | and \0
    529     char *head_ptr = head;      // where we've got to in head
    530     psS32 maxLength = 256;
    531     time_t clock = time(NULL);  // The current time.
    532     struct tm *utc = gmtime(&clock);    // The current gm time.
     504    PS_ASSERT_PTR_NON_NULL(comp, );
     505    PS_ASSERT_PTR_NON_NULL(format, );
    533506
    534507    // XXX EAM : fd < 1 does not print messages
    535     if (traceFD < 1)
     508    if (traceFD < 1) {
    536509        return;
    537 
    538     if (NULL == comp) {
    539         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    540                 _("Function %s called on a NULL trace level tree."),
    541                 __func__);
    542         return;
    543     }
    544 
     510    }
     511
     512    static bool first = true;           // First time we've been called?
     513    static char hostname[MAX_HOSTNAME_LENGTH + 1]; // Host name
    545514    if (first) {
    546         first = 0;
     515        first = false;
    547516        gethostname(hostname, 256);
    548517    }
    549518
    550     if (level == PS_LOG_ABORT) {
     519    char clevel = 0;                    // letter-name for level
     520    switch (level) {
     521    case PS_LOG_ABORT:
    551522        clevel = 'A';
    552     } else if (level == PS_LOG_ERROR) {
     523        break;
     524    case PS_LOG_ERROR:
    553525        clevel = 'E';
    554     } else if (level == PS_LOG_WARN) {
     526        break;
     527    case PS_LOG_WARN:
    555528        clevel = 'W';
    556     } else if (level == PS_LOG_INFO) {
     529        break;
     530    case PS_LOG_INFO:
    557531        clevel = 'I';
    558     } else if (level >= 4) {
    559         clevel = level + '0';
    560     } else {
    561         psTrace("psLib.sys", 2, "Invalid logMsg level: %d (%s)\n", level, format);
    562         level = (level < 0) ? 0 : 9;
    563         clevel = level + '0';
     532        break;
     533    default:
     534        if (level >= 4) {
     535            clevel = level + '0';
     536        } else {
     537            psTrace("psLib.sys", 2, "Invalid logMsg level: %d (%s)\n", level, format);
     538            level = (level < 0) ? 0 : 9;
     539            clevel = level + '0';
     540        }
    564541    }
    565542
     
    567544    // of it's associatedcomponent.
    568545    if (level <= psTraceGetLevel(comp)) {
    569         //        va_start(ap, format);
     546        int maxLength = MAX_HEADER_LENGTH; // Maximum length of header string
     547        char head[maxLength + 2];       // the added two are for the ending | and \0
     548        char *head_ptr = head;          // where we've got to in head
    570549
    571550        // Create the various log fields...
    572551        if (traceTime) {
     552            time_t clock = time(NULL);  // The current time.
     553            struct tm *utc = gmtime(&clock);    // The current gm time.
    573554            maxLength -= snprintf(head_ptr, maxLength, "%4d:%02d:%02d %02d:%02d:%02dZ",
    574555                                  utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday,
     
    607588        *head_ptr = '\0';
    608589
    609         write (traceFD, head, strlen(head));
     590        write(traceFD, head, strlen(head));
    610591
    611592        if (traceMsg) {
    612593            char line[1024];
    613594
    614             // The following functions get the variable list of parameters with
    615             // which this function was called, and print them to the standard
    616             // output.
    617             // fmt = va_arg(ap, char *);
    618 
    619595            // We indent each message one space for each level of the message.
    620             for (i = 0; i < level; i++) {
     596            for (int i = 0; i < level; i++) {
    621597                write (traceFD, " ", 1);
    622598            }
     
    630606        }
    631607
    632 
    633     }
    634 
     608        // XXX: what is fd-appropriate equivalent of fflush?
     609    }
    635610}
    636611
     
    665640
    666641    // XXX file & lineo are currently unused
    667     va_list ap;
    668     va_start(ap, format);
    669     //    format = va_arg(ap, char *);
    670 
    671     #if USE_PS_MEMORY
    672 
    673     psString fullFacil = NULL;
    674     psStringAppend(&fullFacil, "%s.%s", facil, func);
    675     #else
    676 
     642
     643    // Append the function name to the facility
    677644    size_t facilLength = strlen(facil); // Length of facility name
    678645    size_t funcLength = strlen(func);   // Length of function name
     
    681648    fullFacil[facilLength] = '.';
    682649    strcpy(&fullFacil[facilLength + 1], func);
    683     #endif
    684 
     650
     651    va_list ap;
     652    va_start(ap, format);
    685653    psTraceV(fullFacil, level, format, ap);
    686     #if USE_PS_MEMORY
    687 
    688     psFree(fullFacil);
    689     #endif
    690 
    691654    va_end(ap);
    692     // fflush(traceFP);
    693     // XXX EAM : what is fd-appropriate equivalent?
    694655}
    695656
Note: See TracChangeset for help on using the changeset viewer.