Changeset 560 for trunk/psLib/src/sys/psTrace.c
- Timestamp:
- May 1, 2004, 1:04:35 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psTrace.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psTrace.c
r559 r560 1 1 /***************************************************************************** 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. 9 5 10 6 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. 13 12 14 13 We build a tree of trace components. Every node in the tree has a 15 14 depth, which is it's distance from the root. However, this is not 16 15 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 ob duscated by the fact that component names are of the form16 trace level of that node. 17 18 This code is obfuscated by the fact that component names are of the form 20 19 ".a.b.c.d" where "." is always the root of the tree, and for tree that have 21 20 a depth of 3 or higher (including the root ".") the "." character is also … … 24 23 while the last three dots merely act as separators between the node names 25 24 "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. 26 29 *****************************************************************************/ 27 30 #include <stdlib.h> … … 29 32 #include <string.h> 30 33 #include <stdarg.h> 31 /* #include "ps Lib.h" */34 /* #include "pslib.h" */ 32 35 #include "psMemory.h" 33 36 #include "psTrace.h" 34 37 #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" 52 39 53 40 static 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. Defining59 // this variable allows us to reduce component-tree searches that tell60 // us if a message should be printed.61 static char cachedName[CACHE_NAME_LEN + 1] = NO_CACHE;62 // last name looked up63 static int cachedLevel; // level of last looked up name64 65 66 41 /***************************************************************************** 67 42 componentAlloc(): allocate memory for a new node, and initialize members. … … 70 45 int level) 71 46 { 72 //printf("Calling componentAlloc(%s, %d)\n", name, level);73 47 Component *comp = psAlloc(sizeof(Component)); 74 48 comp->name = psStringCopy(name); 75 49 comp->level = level; 76 comp-> dimen = comp->n = 0;50 comp->n = 0; 77 51 comp->subcomp = NULL; 78 52 return comp; … … 108 82 { 109 83 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 *****************************************************************************/ 92 void 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 113 void psTraceReset() 114 { 115 p_psTraceReset(croot); 116 } 117 118 119 void psTraceFree() 119 120 { 120 121 componentFree(croot); 121 croot = NULL;122 122 } 123 123 … … 140 140 int nodeExists = 0; 141 141 142 // printf("Calling componentAdd(%s, %d)\n", addNodeName, level);143 142 // Is this the root node? If so, simply set level and return. 144 143 if (strcmp(".", addNodeName) == 0) { … … 185 184 186 185 /***************************************************************************** 187 setHighestLevel(): traverse the component tree recursively. For each188 node encountered, if it's trace level is higher than the global189 variable highest_level, then set highest_level to that nodes trace190 level.191 Input:192 comp193 Output:194 none195 Returns:196 NULL197 *****************************************************************************/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_level208 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 /*****************************************************************************220 186 psSetTraceLevel(): add the component named "comp" to the component tree, 221 187 if it is not already there, and set it's trace level to "level". … … 236 202 } 237 203 238 //printf("Calling psSetTraceLevel(%s, %d)\n", comp, level);239 // invalidate cache240 strncpy(cachedName, NO_CACHE, CACHE_NAME_LEN);241 242 204 // Add the new component to the component tree. 243 205 componentAdd(comp, level); 244 206 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 253 207 // return 0 on success. 254 //printf("Called psSetTraceLevel(%s, %d)\n", comp, level);255 208 return 0; 256 209 } … … 279 232 int i = 0; 280 233 234 if (NULL == currentNode) { 235 return(UNKNOWN_TRACE_LEVEL); 236 } 237 281 238 if (strcmp(".", aname) == 0) { 282 239 return(croot->level); … … 292 249 firstComponent = strsep(&pname, "."); 293 250 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 294 257 if (strcmp(currentNode->subcomp[i]->name, firstComponent) == 0) { 295 258 currentNode = currentNode->subcomp[i]; … … 318 281 int psGetTraceLevel(const char *name) 319 282 { 320 int level = -1;321 322 //printf("Calling psGetTraceLevel(%s)\n", name);323 // If the root component tree does not exist, then initialize it.324 283 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); 332 285 } 333 286 334 287 // 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)); 343 289 } 344 290 … … 392 338 void psPrintTraceLevels(void) 393 339 { 394 // If the root component tree does not exist, then initialize it.395 340 if (croot == NULL) { 396 initTrace();341 return; 397 342 } 398 343 … … 423 368 int i = 0; 424 369 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)) { 432 378 va_start(ap, level); 433 379 … … 444 390 va_end(ap); 445 391 } 392 446 393 // NOTE: should we free *fmt as well? Read the man page. 447 394 }
Note:
See TracChangeset
for help on using the changeset viewer.
