Changeset 560
- Timestamp:
- May 1, 2004, 1:04:35 PM (22 years ago)
- Location:
- trunk/psLib
- Files:
-
- 30 added
- 19 edited
-
src/sys/psLogMsg.c (modified) (3 diffs)
-
src/sys/psTrace.c (modified) (14 diffs)
-
src/sys/psTrace.h (modified) (3 diffs)
-
src/sysUtils/psHash.c (modified) (10 diffs)
-
src/sysUtils/psHash.h (modified) (2 diffs)
-
src/sysUtils/psLogMsg.c (modified) (3 diffs)
-
src/sysUtils/psTrace.c (modified) (14 diffs)
-
src/sysUtils/psTrace.h (modified) (3 diffs)
-
test/sysUtils/Makefile (modified) (6 diffs)
-
test/sysUtils/tst_psHash00.c (added)
-
test/sysUtils/tst_psHash01.c (added)
-
test/sysUtils/tst_psHash02.c (added)
-
test/sysUtils/tst_psHash03.c (added)
-
test/sysUtils/tst_psHash04.c (added)
-
test/sysUtils/tst_psLogMsg00.c (added)
-
test/sysUtils/tst_psLogMsg01.c (added)
-
test/sysUtils/tst_psLogMsg02.c (added)
-
test/sysUtils/tst_psLogMsg03.c (added)
-
test/sysUtils/tst_psTrace00.c (modified) (5 diffs)
-
test/sysUtils/tst_psTrace01.c (modified) (3 diffs)
-
test/sysUtils/tst_psTrace02.c (modified) (2 diffs)
-
test/sysUtils/tst_psTrace03.c (modified) (2 diffs)
-
test/sysUtils/tst_psTrace04.c (added)
-
test/sysUtils/verified/tst_psHash00.stderr (added)
-
test/sysUtils/verified/tst_psHash00.stdout (added)
-
test/sysUtils/verified/tst_psHash01.stderr (added)
-
test/sysUtils/verified/tst_psHash01.stdout (added)
-
test/sysUtils/verified/tst_psHash02.stderr (added)
-
test/sysUtils/verified/tst_psHash02.stdout (added)
-
test/sysUtils/verified/tst_psHash03.stderr (added)
-
test/sysUtils/verified/tst_psHash03.stdout (added)
-
test/sysUtils/verified/tst_psHash04.stderr (added)
-
test/sysUtils/verified/tst_psHash04.stdout (added)
-
test/sysUtils/verified/tst_psLogMsg00.stderr (added)
-
test/sysUtils/verified/tst_psLogMsg00.stdout (added)
-
test/sysUtils/verified/tst_psLogMsg01.stderr (added)
-
test/sysUtils/verified/tst_psLogMsg01.stdout (added)
-
test/sysUtils/verified/tst_psLogMsg02.stderr (added)
-
test/sysUtils/verified/tst_psLogMsg02.stdout (added)
-
test/sysUtils/verified/tst_psLogMsg03.stderr (added)
-
test/sysUtils/verified/tst_psLogMsg03.stdout (added)
-
test/sysUtils/verified/tst_psTrace00.stderr (modified) (1 diff)
-
test/sysUtils/verified/tst_psTrace00.stdout (modified) (1 diff)
-
test/sysUtils/verified/tst_psTrace01.stderr (modified) (1 diff)
-
test/sysUtils/verified/tst_psTrace01.stdout (modified) (1 diff)
-
test/sysUtils/verified/tst_psTrace02.stdout (modified) (1 diff)
-
test/sysUtils/verified/tst_psTrace03.stdout (modified) (2 diffs)
-
test/sysUtils/verified/tst_psTrace04.stderr (added)
-
test/sysUtils/verified/tst_psTrace04.stdout (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psLogMsg.c
r500 r560 261 261 head_ptr += strlen(head_ptr); 262 262 } 263 263 264 if (head_ptr > head) { 264 265 *head_ptr++ = '|'; … … 269 270 case PS_LOG_TO_STDOUT: 270 271 puts(head); 271 vprintf(fmt, ap); 272 if (fmt[strlen(fmt) - 1] != '\n') { 272 if (log_msg) { 273 vprintf(fmt, ap); 274 if (fmt[strlen(fmt) - 1] != '\n') { 275 putc('\n', stdout); 276 } 277 } else { 273 278 putc('\n', stdout); 274 279 } … … 277 282 case PS_LOG_TO_STDERR: 278 283 fputs(head, stderr); 279 vfprintf(stderr, fmt, ap); 280 if (fmt[strlen(fmt) - 1] != '\n') { 284 if (log_msg) { 285 vfprintf(stderr, fmt, ap); 286 if (fmt[strlen(fmt) - 1] != '\n') { 287 putc('\n', stderr); 288 } 289 } else { 281 290 putc('\n', stderr); 282 291 } -
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 } -
trunk/psLib/src/sys/psTrace.h
r556 r560 1 1 #if !defined(PS_TRACE_H) 2 2 #define PS_TRACE_H 1 3 #define UNKNOWN_TRACE_LEVEL -9999 // we don't know this name's level 3 #define UNKNOWN_TRACE_LEVEL -9999 // we don't know this name's level 4 #define DEFAULT_TRACE_LEVEL 0 4 5 5 6 /** \file psTrace.h … … 7 8 * \ingroup SystemGroup 8 9 */ 10 11 /***************************************************************************** 12 A component is a string of the form aaa.bbb.ccc, and may itself contain 13 further subcomponents. The Component structure doesn't in fact contain 14 it's full name, but only the last part. 15 *****************************************************************************/ 16 typedef struct Component 17 { 18 const char *name; // last part of name of component 19 int level; // trace level for this component 20 int n; // number of subcomponents 21 struct Component **subcomp; // next level of subcomponents 22 } 23 Component; 9 24 10 25 /** Functions **************************************************************/ … … 28 43 ; 29 44 30 /// turn off all tracing, and free trace's allocated memory 31 void psTraceReset(void) 45 /// Set all trace levels to zero (do not free nodes in the component tree). 46 void psTraceReset() 47 ; 48 49 /// Free all nodes in the component tree. 50 void psTraceFree() 32 51 ; 33 52 -
trunk/psLib/src/sysUtils/psHash.c
r494 r560 13 13 #include "psTrace.h" 14 14 15 // A bucket that holds an item of data 16 typedef struct HashBucket 17 { 18 char *key; // key for this item of data 19 void *data; // the data itself 20 struct HashBucket *next; // list of other possible keys 21 } 22 HashBucket; 23 24 // An entire hash table 25 struct HashTable 26 { 27 int nbucket; // number of buckets 28 HashBucket **buckets; // the buckets themselves 29 }; 30 31 /****************************************************************************** 32 Con/Destruct buckets 33 *****************************************************************************/ 34 static HashBucket *hashBucketAlloc(const char *key, 35 void *data, 36 HashBucket *next) 37 { 38 HashBucket *bucket = psAlloc(sizeof(HashBucket)); 15 /****************************************************************************** 16 Construct buckets 17 *****************************************************************************/ 18 static psHashBucket *hashBucketAlloc(const char *key, 19 void *data, 20 psHashBucket *next) 21 { 22 psHashBucket *bucket = psAlloc(sizeof(psHashBucket)); 39 23 40 24 bucket->key = psStringCopy(key); … … 46 30 47 31 /****************************************************************************** 48 49 *****************************************************************************/ 50 static void hashBucketFree( 51 HashBucket *bucket, // bucket to free 52 void (*itemFree)(void *item)) // how to free hashed data; 53 // or NULL 32 Destruct buckets 33 *****************************************************************************/ 34 static void hashBucketFree(psHashBucket *bucket, // bucket to free 35 void (*itemFree)(void *item)) // how to free data; 54 36 { 55 37 if (bucket == NULL) … … 75 57 { 76 58 psHash *table = psAlloc(sizeof(psHash)); 77 table->buckets = psAlloc(nbucket*sizeof( HashBucket *));59 table->buckets = psAlloc(nbucket*sizeof(psHashBucket *)); 78 60 table->nbucket = nbucket; 79 61 … … 104 86 { 105 87 if (table->buckets[i] != NULL) { 106 HashBucket *ptr = table->buckets[i];88 psHashBucket *ptr = table->buckets[i]; 107 89 while (ptr != NULL) { 108 HashBucket *tmp = ptr->next;90 psHashBucket *tmp = ptr->next; 109 91 hashBucketFree(ptr, itemFree); 110 92 ptr = tmp; … … 124 106 N.b. this is NOT a good hash function! See Knuth for some better ones 125 107 *****************************************************************************/ 126 static void *doHashWork( 127 psHash *table, // table to insert in 128 const char *key, // key to use 129 void *data, // data to insert, 130 // or (if NULL) retrieve/remove 131 int remove 132 , // remove the item from the list? 133 void (*itemFree)(void *item)) // how to free hashed data 134 // or NULL 108 static void *doHashWork(psHash *table, // table to insert in 109 const char *key, // key to use 110 void *data, // data to insert, 111 // or (if NULL) retrieve/remove 112 int remove 113 , // remove the item from the list? 114 void (*itemFree)(void *item)) 115 // how to free hashed data 135 116 { 136 117 long int hash = 1; … … 142 123 hash &= (table->nbucket - 1); 143 124 144 HashBucket *ptr = table->buckets[hash]; 145 /* 146 * We've found the correct hash bucket, now we need to know what to do 147 */ 125 psHashBucket *ptr = table->buckets[hash]; 126 127 // We've found the correct hash bucket, now we need to know what to do 148 128 if (data == NULL) { // retrieve/remove 149 129 if (remove 150 130 ) { 151 HashBucket *optr = ptr;131 psHashBucket *optr = ptr; 152 132 while (ptr != NULL) { 153 133 if (strcmp(key, ptr->key) == 0) { // found it! … … 196 176 } 197 177 } 198 /* 199 * Not found, so insert at the front of the list 200 */ 178 179 // Not found, so insert at the front of the list 201 180 table->buckets[hash] = hashBucketAlloc(key, data, table->buckets[hash]); 202 181 … … 208 187 Insert a value into a hash table 209 188 *****************************************************************************/ 210 void *psHashInsert(psHash *table, // table to insert in189 void *psHashInsert(psHash *table, // table to insert in 211 190 const char *key, // key to use 212 void *data, // data to insert191 void *data, // data to insert 213 192 void (*itemFree)(void *item)) // how to free hashed data; 214 // or NULL215 193 { 216 194 return doHashWork(table, key, data, 0, itemFree); … … 220 198 Lookup a value in a hash table 221 199 *****************************************************************************/ 222 void *psHashLookup(psHash *table, // table to lookup key in200 void *psHashLookup(psHash *table, // table to lookup key in 223 201 const char *key) // key to lookup 224 202 { … … 229 207 Remove and return a value from a hash table 230 208 *****************************************************************************/ 231 void *psHashRemove(psHash *table, // table to lookup key in209 void *psHashRemove(psHash *table, // table to lookup key in 232 210 const char *key) // key to lookup 233 211 { -
trunk/psLib/src/sysUtils/psHash.h
r494 r560 8 8 9 9 /** DO WE NEED TO DEFINE HashTable? */ 10 typedef struct HashTable psHash; ///< Opaque type for a hash table 10 //typedef struct HashTable psHash; ///< Opaque type for a hash table 11 // An entire hash table 12 13 // A bucket that holds an item of data 14 typedef struct psHashBucket 15 { 16 char *key; // key for this item of data 17 void *data; // the data itself 18 struct psHashBucket *next; // list of other possible keys 19 } 20 psHashBucket; 21 22 typedef struct psHash 23 { 24 int nbucket; 25 psHashBucket **buckets; 26 } 27 psHash; 11 28 12 29 /** Functions **************************************************************/ … … 19 36 20 37 /// Free hash buckets from table. 21 void psHashFree(psHash *table, ///< hash table to be freed38 void psHashFree(psHash *table, ///< hash table to be freed 22 39 void (*itemFree)(void *item) ///< how to free hashed data; or NULL 23 40 ); 24 41 25 42 /// Insert entry into table. 26 void *psHashInsert(psHash *table, ///< table to insert in27 const char *key, ///< key to use28 void *data, ///< data to insert43 void *psHashInsert(psHash *table, ///< table to insert in 44 const char *key, ///< key to use 45 void *data, ///< data to insert 29 46 void (*itemFree)(void *item) ///< how to free hashed data; or NULL 30 47 ); -
trunk/psLib/src/sysUtils/psLogMsg.c
r500 r560 261 261 head_ptr += strlen(head_ptr); 262 262 } 263 263 264 if (head_ptr > head) { 264 265 *head_ptr++ = '|'; … … 269 270 case PS_LOG_TO_STDOUT: 270 271 puts(head); 271 vprintf(fmt, ap); 272 if (fmt[strlen(fmt) - 1] != '\n') { 272 if (log_msg) { 273 vprintf(fmt, ap); 274 if (fmt[strlen(fmt) - 1] != '\n') { 275 putc('\n', stdout); 276 } 277 } else { 273 278 putc('\n', stdout); 274 279 } … … 277 282 case PS_LOG_TO_STDERR: 278 283 fputs(head, stderr); 279 vfprintf(stderr, fmt, ap); 280 if (fmt[strlen(fmt) - 1] != '\n') { 284 if (log_msg) { 285 vfprintf(stderr, fmt, ap); 286 if (fmt[strlen(fmt) - 1] != '\n') { 287 putc('\n', stderr); 288 } 289 } else { 281 290 putc('\n', stderr); 282 291 } -
trunk/psLib/src/sysUtils/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 } -
trunk/psLib/src/sysUtils/psTrace.h
r556 r560 1 1 #if !defined(PS_TRACE_H) 2 2 #define PS_TRACE_H 1 3 #define UNKNOWN_TRACE_LEVEL -9999 // we don't know this name's level 3 #define UNKNOWN_TRACE_LEVEL -9999 // we don't know this name's level 4 #define DEFAULT_TRACE_LEVEL 0 4 5 5 6 /** \file psTrace.h … … 7 8 * \ingroup SystemGroup 8 9 */ 10 11 /***************************************************************************** 12 A component is a string of the form aaa.bbb.ccc, and may itself contain 13 further subcomponents. The Component structure doesn't in fact contain 14 it's full name, but only the last part. 15 *****************************************************************************/ 16 typedef struct Component 17 { 18 const char *name; // last part of name of component 19 int level; // trace level for this component 20 int n; // number of subcomponents 21 struct Component **subcomp; // next level of subcomponents 22 } 23 Component; 9 24 10 25 /** Functions **************************************************************/ … … 28 43 ; 29 44 30 /// turn off all tracing, and free trace's allocated memory 31 void psTraceReset(void) 45 /// Set all trace levels to zero (do not free nodes in the component tree). 46 void psTraceReset() 47 ; 48 49 /// Free all nodes in the component tree. 50 void psTraceFree() 32 51 ; 33 52 -
trunk/psLib/test/sysUtils/Makefile
r557 r560 3 3 ## Makefile: test/sysUtils 4 4 ## 5 ## $Revision: 1. 5$ $Name: not supported by cvs2svn $6 ## $Date: 2004-0 4-30 06:19:57$5 ## $Revision: 1.6 $ $Name: not supported by cvs2svn $ 6 ## $Date: 2004-05-01 23:04:35 $ 7 7 ## 8 8 ## Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 10 10 ############################################################################### 11 11 12 ############################################################################### 13 # Note: I added specific entries for the psTrace files. The reason I did 14 # this was because the generic rules were failing on the existing test 15 # code (and hence, on my tst_psTraceXX.c code as well). I am tempted to 16 # fix the generic rules, but that might break existing codes. When this 17 # problem is resolved, I'll remove the specific rules for psTrace. -GLG 18 # Also, psLogMsg. 19 # Also, psHash. 20 ############################################################################### 12 21 include ../../src/Makefile.Globals 13 22 … … 20 29 atst_psAbort_02 \ 21 30 atst_psAbort_03 \ 22 tst_psTrace00 \23 tst_psTrace01 \24 tst_psTrace02 \25 tst_psTrace03 \26 31 tst_psStringCopy 32 TARGET_TRACE = tst_psTrace00 tst_psTrace01 tst_psTrace02 tst_psTrace03 \ 33 tst_psTrace04 34 TARGET_LOGMSG = tst_psLogMsg00 tst_psLogMsg01 tst_psLogMsg02 tst_psLogMsg03 35 TARGET_HASH = tst_psHash00 tst_psHash01 tst_psHash02 tst_psHash03 tst_psHash04 27 36 28 all: $(TARGET) 37 all: $(TARGET) 38 psTrace: $(TARGET_TRACE) 39 psLogMsg: $(TARGET_LOGMSG) 40 psHash: $(TARGET_HASH) 29 41 30 42 tst_psError: tst_psError.o … … 37 49 @echo " Deleting executable and binary files for 'test/sysUtils'" 38 50 $(RM) $(TARGET) *.o *.lint 51 52 tst_psHash00: tst_psHash00.c 53 $(CC) tst_psHash00.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psHash00 54 55 tst_psHash01: tst_psHash01.c 56 $(CC) tst_psHash01.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psHash01 57 58 tst_psHash02: tst_psHash02.c 59 $(CC) tst_psHash02.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psHash02 60 61 tst_psHash03: tst_psHash03.c 62 $(CC) tst_psHash03.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psHash03 63 64 tst_psHash04: tst_psHash04.c 65 $(CC) tst_psHash04.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psHash04 66 67 tst_psLogMsg00: tst_psLogMsg00.c 68 $(CC) tst_psLogMsg00.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psLogMsg00 69 70 tst_psLogMsg01: tst_psLogMsg01.c 71 $(CC) tst_psLogMsg01.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psLogMsg01 72 73 tst_psLogMsg02: tst_psLogMsg02.c 74 $(CC) tst_psLogMsg02.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psLogMsg02 75 76 tst_psLogMsg03: tst_psLogMsg03.c 77 $(CC) tst_psLogMsg03.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psLogMsg03 39 78 40 79 tst_psTrace00: tst_psTrace00.c … … 50 89 $(CC) tst_psTrace03.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psTrace03 51 90 91 tst_psTrace04: tst_psTrace04.c 92 $(CC) tst_psTrace04.c $(LDFLAGS) -L$(PSLIB_LIB_DIR) -L.. -lpslib -lpstest -I$(PSLIB_INCL_DIR) -I.. -o tst_psTrace04 93 52 94 %.o : %.c 53 95 $(CC) $(CFLAGS) $(CPPFLAGS) -I.. -I$(PSLIB_INCL_DIR) -c -o $@ $< … … 60 102 61 103 distclean: 62 rm DistCleanFiles104 rm -rf $(TARGET_TRACE) $(TARGET_LOGMSG) $(TARGET_HASH) -
trunk/psLib/test/sysUtils/tst_psTrace00.c
r558 r560 10 10 int i = 0; 11 11 int lev = 0; 12 int errorFlag= true;12 int testStatus = true; 13 13 14 printPositiveTestHeader(std err,14 printPositiveTestHeader(stdout, 15 15 "psTrace functions", 16 " Testing psSetTraceLevel and psGetTraceLevel");16 "psSetTraceLevel() and psGetTraceLevel()"); 17 17 for (i=0;i<10;i++) { 18 18 psSetTraceLevel(".", i); … … 21 21 printf("ERROR: (.) expected trace level was %d, actual was %d\n", 22 22 i, lev); 23 errorFlag= false;23 testStatus = false; 24 24 } 25 25 } … … 32 32 printf("ERROR: (.NODE00) expected trace level was %d, actual was %d\n", 33 33 i, lev); 34 errorFlag= false;34 testStatus = false; 35 35 } 36 36 … … 39 39 printf("ERROR: (.) expected trace level was %d, actual was %d\n", 40 40 i, 3); 41 errorFlag= false;41 testStatus = false; 42 42 } 43 43 } … … 51 51 printf("ERROR: (.NODE00.NODE01) expected trace level was %d, actual was %d\n", 52 52 i, lev); 53 errorFlag= false;53 testStatus = false; 54 54 } 55 55 } 56 56 57 printFooter(std err,57 printFooter(stdout, 58 58 "psTrace functions", 59 59 "Testing psSetTraceLevel and psGetTraceLevel", 60 errorFlag);61 exit( 0);60 testStatus); 61 exit(!testStatus); 62 62 } -
trunk/psLib/test/sysUtils/tst_psTrace01.c
r555 r560 12 12 int successFlag = true; 13 13 14 printPositiveTestHeader(std err,14 printPositiveTestHeader(stdout, 15 15 "psTrace functions", 16 " TestingpsTraceReset()");16 "psTraceReset()"); 17 17 18 18 for (i=0;i<10;i++) { … … 21 21 22 22 lev = psGetTraceLevel("."); 23 if (lev != UNKNOWN_TRACE_LEVEL) {23 if (lev != DEFAULT_TRACE_LEVEL) { 24 24 printf("ERROR: expected trace level was %d, actual was %d\n", 25 25 i, lev); … … 32 32 psSetTraceLevel(".a.b.c", 2); 33 33 psTraceReset(); 34 if (( UNKNOWN_TRACE_LEVEL != psGetTraceLevel(".")) ||35 ( UNKNOWN_TRACE_LEVEL != psGetTraceLevel(".a")) ||36 ( UNKNOWN_TRACE_LEVEL != psGetTraceLevel(".a.b")) ||37 ( UNKNOWN_TRACE_LEVEL != psGetTraceLevel(".a.b.c"))) {34 if ((DEFAULT_TRACE_LEVEL != psGetTraceLevel(".")) || 35 (DEFAULT_TRACE_LEVEL != psGetTraceLevel(".a")) || 36 (DEFAULT_TRACE_LEVEL != psGetTraceLevel(".a.b")) || 37 (DEFAULT_TRACE_LEVEL != psGetTraceLevel(".a.b.c"))) { 38 38 printf("ERROR: trace levels were not reset properly\n"); 39 39 successFlag = false; 40 40 } 41 41 42 printFooter(std err,42 printFooter(stdout, 43 43 "psTrace functions", 44 " TestingpsTraceReset()",44 "psTraceReset()", 45 45 successFlag); 46 exit(0); 46 47 exit(!successFlag); 47 48 } -
trunk/psLib/test/sysUtils/tst_psTrace02.c
r555 r560 12 12 int lev = 0; 13 13 int errorFlag = 0; 14 15 printPositiveTestHeader(stdout, 16 "psTrace functions", 17 "psTrace()"); 14 18 15 19 psSetTraceLevel(".", 4); … … 35 39 0xbeefface); 36 40 41 printFooter(stdout, 42 "psTrace functions", 43 "psTrace()", 44 true); 45 37 46 exit(0); 38 47 } -
trunk/psLib/test/sysUtils/tst_psTrace03.c
r558 r560 9 9 main() 10 10 { 11 printPositiveTestHeader(stdout, 12 "psTrace functions", 13 "psTracePrintLevels()"); 14 11 15 psSetTraceLevel(".", 9); 12 16 … … 26 30 27 31 psPrintTraceLevels(); 32 33 printFooter(stdout, 34 "psTrace functions", 35 "psTracePrintLevels()", 36 true); 37 28 38 exit(0); 29 39 } -
trunk/psLib/test/sysUtils/verified/tst_psTrace00.stderr
r554 r560 1 /----------------------------- TESTPOINT --------------------------------\2 | TestFile: tst_psTrace00.c |3 | TestPoint: psTrace functions{Testing psSetTraceLevel and psGetTraceLevel} |4 | TestType: Positive |5 \------------------------------------------------------------------------/6 7 8 ---> TESTPOINT PASSED (psTrace functions{Testing psSetTraceLevel and psGetTraceLevel} | tst_psTrace00.c)9 -
trunk/psLib/test/sysUtils/verified/tst_psTrace00.stdout
r554 r560 1 /----------------------------- TESTPOINT --------------------------------\ 2 | TestFile: tst_psTrace00.c | 3 | TestPoint: psTrace functions{psSetTraceLevel() and psGetTraceLevel()} | 4 | TestType: Positive | 5 \------------------------------------------------------------------------/ 6 7 8 ---> TESTPOINT PASSED (psTrace functions{Testing psSetTraceLevel and psGetTraceLevel} | tst_psTrace00.c) 9 -
trunk/psLib/test/sysUtils/verified/tst_psTrace01.stderr
r554 r560 1 /----------------------------- TESTPOINT --------------------------------\2 | TestFile: tst_psTrace01.c |3 | TestPoint: psTrace functions{Testing psTraceReset()} |4 | TestType: Positive |5 \------------------------------------------------------------------------/6 7 8 ---> TESTPOINT PASSED (psTrace functions{Testing psTraceReset()} | tst_psTrace01.c)9 -
trunk/psLib/test/sysUtils/verified/tst_psTrace01.stdout
r554 r560 1 /----------------------------- TESTPOINT --------------------------------\ 2 | TestFile: tst_psTrace01.c | 3 | TestPoint: psTrace functions{psTraceReset()} | 4 | TestType: Positive | 5 \------------------------------------------------------------------------/ 6 7 8 ---> TESTPOINT PASSED (psTrace functions{psTraceReset()} | tst_psTrace01.c) 9 -
trunk/psLib/test/sysUtils/verified/tst_psTrace02.stdout
r554 r560 1 /----------------------------- TESTPOINT --------------------------------\ 2 | TestFile: tst_psTrace02.c | 3 | TestPoint: psTrace functions{psTrace()} | 4 | TestType: Positive | 5 \------------------------------------------------------------------------/ 6 1 7 (0) This message should be displayed (beefface) 2 8 (1) This message should be displayed (beefface) 3 9 (2) This message should be displayed (beefface) 10 11 ---> TESTPOINT PASSED (psTrace functions{psTrace()} | tst_psTrace02.c) 12 -
trunk/psLib/test/sysUtils/verified/tst_psTrace03.stdout
r558 r560 1 /----------------------------- TESTPOINT --------------------------------\ 2 | TestFile: tst_psTrace03.c | 3 | TestPoint: psTrace functions{psTracePrintLevels()} | 4 | TestType: Positive | 5 \------------------------------------------------------------------------/ 6 1 7 . 9 2 8 a 8 … … 10 16 b 3 11 17 c 5 18 19 ---> TESTPOINT PASSED (psTrace functions{psTracePrintLevels()} | tst_psTrace03.c) 20
Note:
See TracChangeset
for help on using the changeset viewer.
