IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 1, 2004, 1:04:35 PM (22 years ago)
Author:
gusciora
Message:

This is a fairly large checkin. I had been sitting on a lot of test code
that was not quite in sync with the current test harness. I modified it
somewhat so it mostly works with the test harness, though there are still
problems with the printFooter() function not having a status for "status
unknown". Also, the runTest is not correctly parsing my date strings in
psLogMsg calls. Also, I added an individual entry for each test in the
Makefile in the test directory. I did this because the generic rules were
failing, and I did not want to break the tests for those people who were
successfully using the generic rules.

File:
1 edited

Legend:

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

    r559 r560  
    11/*****************************************************************************
    2     A simple implementation of a tracing facility for Pan-STARRS
    3  
    4     Tracing is controlled on a per "component" basis, where a "component" is
    5     a name of the form aaa.bbb.ccc where aaa is the most significant part.
    6     For example, the utilities library might be called "utils", the
    7     doubly-linked list "utils.dlist", and the code to destroy a list
    8     "utils.dlist.del".
     2    A simple implementation of a tracing facility for Pan-STARRS.  Tracing
     3    is controlled on a per "component" basis, where a "component" is a name
     4    of the form aaa.bbb.ccc where aaa is the most significant part.
    95 
    106    NOTES:
    11  In the SRD, higher trace levels correspond to a numerically lower
    12  trace value in the code.  This is a bit confusing.
     7 In the SRD, higher trace levels correspond to a numerically lower trace
     8 value in the code.  This is a bit confusing.  For example, a high-level
     9 message might be something like "Begin Processing".  The module programmer
     10 might give that a numerically low trace level, such as 1, so then any
     11 non-zero trace level in that code component will display thatmessage.
    1312 
    1413 We build a tree of trace components.  Every node in the tree has a
    1514 depth, which is it's distance from the root.  However, this is not
    1615 not the same thing as a node's "level", which corresponds to the
    17  trace level of that node.  I should probably rename some variables.
    18  
    19  This code is obduscated by the fact that component names are of the form
     16 trace level of that node.
     17 
     18 This code is obfuscated by the fact that component names are of the form
    2019 ".a.b.c.d" where "." is always the root of the tree, and for tree that have
    2120 a depth of 3 or higher (including the root ".") the "."  character is also
     
    2423 while the last three dots merely act as separators between the node names
    2524 "b", "c", and "d".
     25 
     26 I added a function psTraceFree() which frees all nodes in the trace component
     27 tree.  Previously, there had been no function in the API which accomplished
     28 this purpose.
    2629 *****************************************************************************/
    2730#include <stdlib.h>
     
    2932#include <string.h>
    3033#include <stdarg.h>
    31 /* #include "psLib.h" */
     34/* #include "pslib.h" */
    3235#include "psMemory.h"
    3336#include "psTrace.h"
    3437#include "psString.h"
    35 
    36 #define CACHE_NAME_LEN 50
    37 #define NO_CACHE "\a"                     // no name is cached
    38 /*****************************************************************************
    39     A component is a string of the form aaa.bbb.ccc, and may itself contain
    40     further subcomponents.  The Component structure doesn't in fact contain
    41     it's full name, but only the last part.
    42  *****************************************************************************/
    43 typedef struct Component
    44 {
    45     const char *name;                     // last part of name of component
    46     int level;                            // trace level for this component
    47     int dimen;                            // dimension of subcomponents
    48     int n;                                // number of subcomponents
    49     struct Component **subcomp;           // next level of subcomponents
    50 }
    51 Component;
     38#include "psError.h"
    5239
    5340static Component *croot = NULL;           // The root of the trace component
    54 // tree.
    55 
    56 // NOTE: the next tree globals exist for optimization purposes only.
    57 // Consider removing them.
    58 static int highest_level = 0;             // Highest level requested.  Defining
    59 // this variable allows us to reduce component-tree searches that tell
    60 // us if a message should be printed.
    61 static char cachedName[CACHE_NAME_LEN + 1] = NO_CACHE;
    62 // last name looked up
    63 static int cachedLevel;                   // level of last looked up name
    64 
    65 
    6641/*****************************************************************************
    6742    componentAlloc(): allocate memory for a new node, and initialize members.
     
    7045                          int level)
    7146{
    72     //printf("Calling componentAlloc(%s, %d)\n", name, level);
    7347    Component *comp = psAlloc(sizeof(Component));
    7448    comp->name = psStringCopy(name);
    7549    comp->level = level;
    76     comp->dimen = comp->n = 0;
     50    comp->n = 0;
    7751    comp->subcomp = NULL;
    7852    return comp;
     
    10882{
    10983    if (croot == NULL) {
    110         croot = componentAlloc(".", UNKNOWN_TRACE_LEVEL);
    111     }
    112 }
    113 
    114 
    115 /*****************************************************************************
    116     Free the trace tree information
    117  *****************************************************************************/
    118 void psTraceReset(void)
     84        croot = componentAlloc(".", DEFAULT_TRACE_LEVEL);
     85    }
     86}
     87
     88
     89/*****************************************************************************
     90    Set all trace levels to zero.
     91 *****************************************************************************/
     92void p_psTraceReset(Component *currentNode)
     93{
     94    int i = 0;
     95
     96    if (NULL == currentNode) {
     97        return;
     98    }
     99
     100    currentNode->level = 0;
     101    for (i=0;i<currentNode->n;i++) {
     102        if (NULL == currentNode->subcomp[i]) {
     103            psError(__func__,
     104                    "Sub-component %d of node %s in the trace tree is NULL.\n",
     105                    i, currentNode->name);
     106        } else {
     107            p_psTraceReset(currentNode->subcomp[i]);
     108        }
     109    }
     110    return;
     111}
     112
     113void psTraceReset()
     114{
     115    p_psTraceReset(croot);
     116}
     117
     118
     119void psTraceFree()
    119120{
    120121    componentFree(croot);
    121     croot = NULL;
    122122}
    123123
     
    140140    int        nodeExists = 0;
    141141
    142     // printf("Calling componentAdd(%s, %d)\n", addNodeName, level);
    143142    // Is this the root node?  If so, simply set level and return.
    144143    if (strcmp(".", addNodeName) == 0) {
     
    185184
    186185/*****************************************************************************
    187     setHighestLevel(): traverse the component tree recursively.  For each
    188  node encountered, if it's trace level is higher than the global
    189  variable highest_level, then set highest_level to that nodes trace
    190  level.
    191     Input:
    192  comp
    193     Output:
    194  none
    195     Returns:
    196  NULL
    197  *****************************************************************************/
    198 static void setHighestLevel(const Component *comp)
    199 {
    200     int i = 0;
    201 
    202     if (comp == NULL) {
    203         printf("ERROR: setHighestLevel(NULL)\n");
    204         exit(1);
    205     }
    206 
    207     // If this nodes trace level is the max so far, save it in highest_level
    208     if (comp->level > highest_level) {
    209         highest_level = comp->level;
    210     }
    211 
    212     // Do the same for all children nodes.
    213     for (i = 0; i < comp->n; i++) {
    214         setHighestLevel(comp->subcomp[i]);
    215     }
    216 }
    217 
    218 
    219 /*****************************************************************************
    220186    psSetTraceLevel(): add the component named "comp" to the component tree,
    221187 if it is not already there, and set it's trace level to "level".
     
    236202    }
    237203
    238     //printf("Calling psSetTraceLevel(%s, %d)\n", comp, level);
    239     // invalidate cache
    240     strncpy(cachedName, NO_CACHE, CACHE_NAME_LEN);
    241 
    242204    // Add the new component to the component tree.
    243205    componentAdd(comp, level);
    244206
    245     // Save this search info in our depth-one cache.
    246     if (level > highest_level) {
    247         highest_level = level;
    248     } else {
    249         highest_level = UNKNOWN_TRACE_LEVEL;
    250         setHighestLevel(croot);
    251     }
    252 
    253207    // return 0 on success.
    254     //printf("Called psSetTraceLevel(%s, %d)\n", comp, level);
    255208    return 0;
    256209}
     
    279232    int        i = 0;
    280233
     234    if (NULL == currentNode) {
     235        return(UNKNOWN_TRACE_LEVEL);
     236    }
     237
    281238    if (strcmp(".", aname) == 0) {
    282239        return(croot->level);
     
    292249        firstComponent = strsep(&pname, ".");
    293250        for (i = 0; i < currentNode->n; i++) {
     251            if (NULL == currentNode->subcomp[i]) {
     252                psError(__func__,
     253                        "Sub-component %d of node %s in trace tree is NULL.\n",
     254                        i, currentNode->name);
     255            }
     256
    294257            if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) {
    295258                currentNode = currentNode->subcomp[i];
     
    318281int psGetTraceLevel(const char *name)
    319282{
    320     int level = -1;
    321 
    322     //printf("Calling psGetTraceLevel(%s)\n", name);
    323     // If the root component tree does not exist, then initialize it.
    324283    if (croot == NULL) {
    325         initTrace();
    326     }
    327 
    328     // Before we traverse the component tree, check if we just searched for
    329     // this name, and if so, return that level.
    330     if (strcmp(name, cachedName) == 0) {
    331         return cachedLevel;
     284        return(UNKNOWN_TRACE_LEVEL);
    332285    }
    333286
    334287    // Search the component root tree, determine the trace level.
    335     level = doGetTraceLevel(name);
    336 
    337     // Save this search info in our depth-one cache.
    338     strncpy(cachedName, name, CACHE_NAME_LEN);
    339     cachedLevel = level;
    340 
    341     //printf("Called psGetTraceLevel(%s) (level was %d)\n", name, level);
    342     return level;
     288    return(doGetTraceLevel(name));
    343289}
    344290
     
    392338void psPrintTraceLevels(void)
    393339{
    394     // If the root component tree does not exist, then initialize it.
    395340    if (croot == NULL) {
    396         initTrace();
     341        return;
    397342    }
    398343
     
    423368    int i = 0;
    424369
    425     // We first check if the level of this message is less than the highest
    426     // level in the tree.  If so, we don't go any further.  If not, we need
    427     // to determine the trace level of comp, and then display the message if
    428     // the comp's trace level is high enough.
    429 
    430     if ((level <= highest_level) &&
    431             (level <= psGetTraceLevel(comp))) {
     370    if (NULL == comp) {
     371        psError(__func__,
     372                "p_psTrace() called on a NULL trace level tree\n");
     373    }
     374
     375    // Only display this message if it's trace level is less than the level
     376    // of it's associatedcomponent.
     377    if (level <= psGetTraceLevel(comp)) {
    432378        va_start(ap, level);
    433379
     
    444390        va_end(ap);
    445391    }
     392
    446393    // NOTE: should we free *fmt as well?  Read the man page.
    447394}
Note: See TracChangeset for help on using the changeset viewer.