Changeset 1013
- Timestamp:
- Jun 11, 2004, 7:50:01 PM (22 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 9 edited
-
sys/psLogMsg.c (modified) (17 diffs)
-
sys/psLogMsg.h (modified) (6 diffs)
-
sys/psTrace.c (modified) (14 diffs)
-
sys/psTrace.h (modified) (4 diffs)
-
sysUtils/psHash.h (modified) (4 diffs)
-
sysUtils/psLogMsg.c (modified) (17 diffs)
-
sysUtils/psLogMsg.h (modified) (6 diffs)
-
sysUtils/psTrace.c (modified) (14 diffs)
-
sysUtils/psTrace.h (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psLogMsg.c
r979 r1013 1 /***************************************************************************** 2 A simple implementation of a logging facility for Pan-STARRS 3 1 /** @file psLogMsg.c 2 * @brief Procedures for logging messages. 3 * \ingroup LogTrace 4 * 5 * This file will hold the prototypes for defining procedure which set 6 * message log levels, messahe log formats, message log destinations, and 7 * for generating the messages themselves. 8 * @ingroup LogTrace 9 * 10 * @author Robert Lupton, Princeton University 11 * @author George Gusciora, MHPCC 12 * 13 * @version $Revision: 1.15 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-06-12 05:50:01 $ 15 * 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 17 */ 18 19 /***************************************************************************** 4 20 NOTES: currently, the prototype code has the following global variables: 5 static int logDest;6 static int logLevel;7 static int log_time;8 static int log_host;9 static int log_level;10 static int log_name;11 static int log_msg;21 static int p_psGlobalLogDest; 22 static int p_psGlobalLogLevel; 23 static int p_psLogTime; 24 static int p_psLogHost; 25 static int p_psLogLevel; 26 static int p_psLogName; 27 static int p_psLogMsg; 12 28 *****************************************************************************/ 13 29 #include <limits.h> … … 18 34 #include <time.h> 19 35 #include <unistd.h> 20 21 /* #include "psLib.h" */22 36 #include "psLogMsg.h" 23 37 #include "psError.h" … … 27 41 #define MIN_LOG_LEVEL 0 28 42 #define MAX_LOG_LEVEL 9 29 static int logDest = PS_LOG_TO_STDERR; // where to log messages30 static int logLevel = PS_LOG_INFO; // log all messages at this or above43 static int p_psGlobalLogDest = PS_LOG_TO_STDERR; // where to log messages 44 static int p_psGlobalLogLevel = PS_LOG_INFO; // log all messages at this or above 31 45 //static FILE *p_logFP = NULL; 32 46 // The following variables control 33 47 // which information is displayed in 34 48 // log messages. 35 static int log_time = 1; // Flag to include time info36 static int log_host = 1; // Flag to include host info37 static int log_level = 1; // Flag to include level info38 static int log_name = 1; // Flag to include name info39 static int log_msg = 1; // Flag to include message info49 static int p_psLogTime = 1; // Flag to include time info 50 static int p_psLogHost = 1; // Flag to include host info 51 static int p_psLogLevel = 1; // Flag to include level info 52 static int p_psLogName = 1; // Flag to include name info 53 static int p_psLogMsg = 1; // Flag to include message info 40 54 /***************************************************************************** 41 55 psLogSetLevel(): Set the current log level and return old level. … … 49 63 int psLogSetLevel(int level) 50 64 { 51 int oldLevel = logLevel; 65 // Save old global log level for changing it. 66 int oldLevel = p_psGlobalLogLevel; 52 67 53 68 if ((level < MIN_LOG_LEVEL) || (level > MAX_LOG_LEVEL)) { … … 57 72 } 58 73 59 logLevel = level; 74 // Set new global log level 75 p_psGlobalLogLevel = level; 76 77 // Return old global log level 60 78 return oldLevel; 61 79 } … … 75 93 int psLogSetDestination(int dest) 76 94 { 77 int old = logDest; 95 // Save old global log destination before changing it. 96 int old = p_psGlobalLogDest; 78 97 79 98 switch (dest) { … … 81 100 case PS_LOG_TO_STDOUT: 82 101 case PS_LOG_TO_STDERR: 83 logDest = dest; 102 // Set new global log destination 103 p_psGlobalLogDest = dest; 84 104 break; 85 105 86 106 default: 87 107 // GUS: Should you log this error properly? 88 fprintf(stderr,"Unknown logDestination: %d (ignored)\n", dest); 89 break; 90 } 91 108 fprintf(stderr,"Unknown p_psGlobalLogDest: %d (ignored)\n", dest); 109 break; 110 } 111 112 // Return old global log destination 92 113 return old; 93 114 } … … 112 133 void psLogSetFormat(const char *fmt) 113 134 { 114 int nlog_time = 0; 115 int nlog_host = 0; 116 int nlog_level = 0; 117 int nlog_name = 0; 118 int nlog_msg = 0; 135 136 // Step through each character in the format string. For each letter 137 // in that string, set/unset the appropriate logging. 119 138 120 139 for (const char *ptr = fmt; *ptr != '\0'; ptr++) { … … 122 141 case 'H': 123 142 case 'h': 124 nlog_host = 1;143 p_psLogHost = 1; 125 144 break; 126 145 case 'L': 127 146 case 'l': 128 nlog_level = 1;147 p_psLogLevel = 1; 129 148 break; 130 149 case 'M': 131 150 case 'm': 132 nlog_msg = 1;151 p_psLogMsg = 1; 133 152 break; 134 153 case 'N': 135 154 case 'n': 136 nlog_name = 1;155 p_psLogName = 1; 137 156 break; 138 157 case 'T': 139 158 case 't': 140 nlog_time = 1;159 p_psLogTime = 1; 141 160 break; 142 161 default: 143 // GUS: figure out the psError() format:144 // psError(__func__, PS_ERR_UNKNOWN, 1,145 // "Unknown logging keyword %c", *ptr);146 162 psError(__func__, "Unknown logging keyword %c", *ptr); 147 163 break; … … 149 165 } 150 166 151 if (! nlog_msg) {167 if (!p_psLogMsg) { 152 168 psTrace("utils.logMsg", 1, 153 169 "You must at least log error messages (You chose \"%s\")", 154 170 fmt); 155 171 } 156 157 log_host = nlog_host;158 log_level = nlog_level;159 log_msg = nlog_msg;160 log_name = nlog_name;161 log_time = nlog_time;162 172 } 163 173 … … 186 196 va_list ap) 187 197 { 188 static int first = 1; 198 static int first = 1; // Flag for calling gethostname() 189 199 static char hostname[HOST_NAME_MAX + 1]; 200 // Buffer for hostname. 190 201 char clevel=0; // letter-name for level 191 202 char head[HOST_NAME_MAX + 40]; // yes, this is long enough 192 203 char *head_ptr = head; // where we've got to in head 193 time_t clock = time(NULL); 194 struct tm *utc = gmtime(&clock); 195 196 if ((level > logLevel) || (logDest == PS_LOG_NONE)) { 204 time_t clock = time(NULL); // The current time. 205 struct tm *utc = gmtime(&clock); // The current gm time. 206 207 // If logging is off, or if the level is too high, return immediately. 208 if ((level > p_psGlobalLogLevel) || (p_psGlobalLogDest == PS_LOG_NONE)) { 197 209 return; 198 210 } 199 211 212 // If I have not been here yet, determine my hostname and save it. 200 213 if (first) { 201 214 first = 0; … … 236 249 } 237 250 238 239 if ( log_time) {251 // Create the various log fields... 252 if (p_psLogTime) { 240 253 sprintf(head_ptr, "%4d:%02d:%02d %02d:%02d:%02dZ", 241 254 utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday, … … 243 256 head_ptr += strlen(head_ptr); 244 257 } 245 if (log_host) { 258 // Hostname should be 20 characters. 259 if (p_psLogHost) { 246 260 if (head_ptr > head) { 247 261 *head_ptr++ = '|'; … … 250 264 head_ptr += strlen(head_ptr); 251 265 } 252 if ( log_level) {266 if (p_psLogLevel) { 253 267 if (head_ptr > head) { 254 268 *head_ptr++ = '|'; … … 257 271 head_ptr += strlen(head_ptr); 258 272 } 259 if (log_name) { 273 // The name field must be 15 characters. 274 if (p_psLogName) { 260 275 if (head_ptr > head) { 261 276 *head_ptr++ = '|'; 262 277 } 263 sprintf(head_ptr, "% -15s", name);278 sprintf(head_ptr, "%15.15s", name); 264 279 head_ptr += strlen(head_ptr); 265 280 } … … 270 285 *head_ptr = '\0'; 271 286 272 switch ( logDest) {287 switch (p_psGlobalLogDest) { 273 288 case PS_LOG_TO_STDOUT: 274 289 puts(head); 275 if ( log_msg) {290 if (p_psLogMsg) { 276 291 vprintf(fmt, ap); 277 292 if (fmt[strlen(fmt) - 1] != '\n') { … … 285 300 case PS_LOG_TO_STDERR: 286 301 fputs(head, stderr); 287 if ( log_msg) {302 if (p_psLogMsg) { 288 303 vfprintf(stderr, fmt, ap); 289 304 if (fmt[strlen(fmt) - 1] != '\n') { -
trunk/psLib/src/sys/psLogMsg.h
r979 r1013 1 /** @file psLogMsg.h 2 * @brief Procedures for logging messages. 3 * \ingroup LogTrace 4 * 5 * This file will hold the prototypes for defining procedure which set 6 * message log levels, messahe log formats, message log destinations, and 7 * for generating the messages themselves. 8 * @ingroup LogTrace 9 * 10 * @author Robert Lupton, Princeton University 11 * @author George Gusciora, MHPCC 12 * 13 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-06-12 05:50:01 $ 15 * 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 17 */ 1 18 #if !defined(PS_LOG_MSG_H) 2 19 #define PS_LOG_MSG_H 3 4 /** \file psLogMsg.h5 * \brief log messaging facilities6 * \ingroup LogTrace7 */8 9 20 #include <stdarg.h> 10 21 … … 18 29 /// In future versions, this procedure will take a character string as an 19 30 /// argument which can specify more general log destinations. 20 int psLogSetDestination(int dest); 31 int psLogSetDestination(int dest ///< Specifies where to send messages. 32 ); 33 21 34 22 35 /// This procedure sets the message level for future log messages. Subsequent … … 25 38 /// Ie. higher values set by this procedure will cause more log messages to 26 39 /// be displayed. 27 int psLogSetLevel(int level); 40 int psLogSetLevel(int level ///< Specifies the system log level 41 ); 42 28 43 29 44 /// This procedure sets the log format for future log messages. The argument … … 32 47 /// Deleting a letter from the string will cause the associated information 33 48 /// to not be logged. 34 void psLogSetFormat(const char *fmt); 49 void psLogSetFormat(const char *fmt ///< Specifies the system log format 50 ); 35 51 36 52 … … 41 57 void psLogMsg(const char *name, ///< name of the log source 42 58 int myLevel, ///< severity level of this log message 43 const char *fmt, ...) ///< printf-style format command 44 ; 45 59 const char *fmt, ... ///< printf-style format command 60 ); 46 61 47 62 … … 51 66 int myLevel, ///< severity level of this log message 52 67 const char *fmt, ///< printf-style format command 53 va_list ap) ///< varargs argument list 54 ; 55 68 va_list ap ///< varargs argument list 69 ); 56 70 57 71 ///< Status codes for log messages -
trunk/psLib/src/sys/psTrace.c
r928 r1013 1 /***************************************************************************** 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. 5 1 /** @file psTrace.c 2 * \brief basic run-time trace facilities 3 * \ingroup LogTrace 4 * 5 * This file will hold the code for procedures to insert 6 * trace messages into the code. 7 * 8 * @author Robert Lupton, Princeton University 9 * @author George Gusciora, MHPCC 10 * 11 * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-06-12 05:50:01 $ 13 * 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 */ 16 /***************************************************************************** 6 17 NOTES: 7 18 In the SRD, higher trace levels correspond to a numerically lower trace … … 16 27 trace level of that node. 17 28 18 This code is obfuscated by the fact that component names are of the form19 ".a.b.c.d" where "." is always the root of the tree, and for tree that have20 a depth of 3 or higher (including the root ".") the "." character is also21 the deliminator between different individual nodes in the full component22 name. So, for ".a.b.c.d", the first "." signifies the root of the tree,23 while the last three dots merely act as separators between the node names24 "b", "c", and "d".25 26 29 I added a function psTraceFree() which frees all nodes in the trace component 27 30 tree. Previously, there had been no function in the API which accomplished … … 32 35 #include <string.h> 33 36 #include <stdarg.h> 34 /* #include "pslib.h" */35 37 #include "psMemory.h" 36 38 #include "psTrace.h" … … 38 40 #include "psError.h" 39 41 40 static Component * croot = NULL;// The root of the trace component41 static FILE * traceFP = NULL;42 /***************************************************************************** 43 componentAlloc(): allocate memory for a new node, and initialize members.42 static Component *p_psCroot = NULL; // The root of the trace component 43 static FILE *p_psTraceFP = NULL; // File destination for messages. 44 /***************************************************************************** 45 componentAlloc(): allocate memory for a new node, and initialize members. 44 46 *****************************************************************************/ 45 47 Component *componentAlloc(const char *name, … … 56 58 57 59 /***************************************************************************** 58 componentFree(): free the current node in the root tree, and all 59 childrennodes as well.60 componentFree(): free the current node in the root tree, and all children 61 nodes as well. 60 62 *****************************************************************************/ 61 63 static void componentFree(Component *comp) … … 78 80 79 81 /***************************************************************************** 80 initTrace(): simply initialize the component root tree.82 initTrace(): simply initialize the component root tree. 81 83 *****************************************************************************/ 82 84 static void initTrace(void) 83 85 { 84 if ( croot == NULL) {85 croot = componentAlloc(".", DEFAULT_TRACE_LEVEL);86 } 87 } 88 89 90 /***************************************************************************** 91 Set all trace levels to zero.86 if (p_psCroot == NULL) { 87 p_psCroot = componentAlloc(".", DEFAULT_TRACE_LEVEL); 88 } 89 } 90 91 92 /***************************************************************************** 93 Set all trace levels to zero. 92 94 *****************************************************************************/ 93 95 void p_psTraceReset(Component *currentNode) … … 112 114 } 113 115 116 /***************************************************************************** 117 Set all trace levels to zero. 118 *****************************************************************************/ 114 119 void psTraceReset() 115 120 { 116 p_psTraceReset(croot); 117 } 118 119 121 p_psTraceReset(p_psCroot); 122 } 123 124 125 /***************************************************************************** 126 Free all nodes in the component tree. 127 *****************************************************************************/ 120 128 void psTraceFree() 121 129 { 122 componentFree( croot);123 } 124 125 126 /***************************************************************************** 127 componentAdd(): Adds the component named "addNodeName" to the root tree.130 componentFree(p_psCroot); 131 } 132 133 134 /***************************************************************************** 135 componentAdd(): Adds the component named "addNodeName" to the root tree. 128 136 129 NOTE: replace the call to strsep() with a call to strtok(), which conforms130 to ANSI-C.137 NOTE: replace the call to strsep() with a call to strtok(), which conforms 138 to ANSI-C. 131 139 *****************************************************************************/ 132 140 static void componentAdd(const char *addNodeName, 133 141 int level) 134 142 { 135 int i = 0; 136 char name[strlen(addNodeName) + 1]; 137 // buffer for writeable copy. 143 int i = 0; // Loop index variable. 144 char name[strlen(addNodeName) + 1]; // buffer for writeable copy. 138 145 char *pname=name; 139 146 char *firstComponent = NULL; // first component of name 140 Component *currentNode = croot;147 Component *currentNode = p_psCroot; 141 148 int nodeExists = 0; 142 149 143 150 // Is this the root node? If so, simply set level and return. 144 151 if (strcmp(".", addNodeName) == 0) { 145 croot->level = level;152 p_psCroot->level = level; 146 153 return; 147 154 } … … 199 206 { 200 207 // If the root component tree does not exist, then initialize it. 201 if ( croot == NULL) {208 if (p_psCroot == NULL) { 202 209 initTrace(); 203 210 } … … 230 237 char *pname=name; 231 238 char *firstComponent = NULL; // first component of name 232 Component *currentNode = croot;239 Component *currentNode = p_psCroot; 233 240 int i = 0; 234 241 … … 238 245 239 246 if (strcmp(".", aname) == 0) { 240 return( croot->level);247 return(p_psCroot->level); 241 248 } 242 249 … … 282 289 int psTraceGetLevel(const char *name) 283 290 { 284 if ( croot == NULL) {291 if (p_psCroot == NULL) { 285 292 return(UNKNOWN_TRACE_LEVEL); 286 293 } … … 328 335 329 336 /***************************************************************************** 330 psPrintTraceLevels() 331 Simply print all the trace levels in the trace levelcomponent tree.332 Inputs:333 none 334 Outputs:335 none 336 Returns:337 psPrintTraceLevels(): Simply print all the trace levels in the trace level 338 component tree. 339 Inputs: 340 none 341 Outputs: 342 none 343 Returns: 337 344 null 338 345 *****************************************************************************/ 339 346 void psTracePrintLevels(void) 340 347 { 341 if ( croot == NULL) {348 if (p_psCroot == NULL) { 342 349 return; 343 350 } 344 351 345 doPrintTraceLevels( croot, 0);346 } 347 348 349 /***************************************************************************** 350 p_psTrace(): we display the trace message to standard output if the trace351 level of that message, supplied by the parameter "level" is higher 352 than the trace level that is currently associated with the component 353 named by theparameter "comp".354 Input:352 doPrintTraceLevels(p_psCroot, 0); 353 } 354 355 356 /***************************************************************************** 357 p_psTrace(): we display the trace message to standard output if the trace 358 level of that message, supplied by the parameter "level" is higher than the 359 trace level that is currently associated with the component named by the 360 parameter "comp". 361 Input: 355 362 comp 356 363 level 357 364 ... a printf-style output string. 358 Output:359 none 360 Return:365 Output: 366 none 367 Return: 361 368 null 362 369 *****************************************************************************/ … … 384 391 fmt = va_arg(ap, char *); 385 392 386 if ( traceFP != NULL) {393 if (p_psTraceFP != NULL) { 387 394 // We indent each message one space for each level of the message. 388 395 for (i = 0; i < level; i++) { 389 fprintf( traceFP, " ");396 fprintf(p_psTraceFP, " "); 390 397 } 391 vfprintf( traceFP, fmt, ap);398 vfprintf(p_psTraceFP, fmt, ap); 392 399 } else { 393 400 // We indent each message one space for each level of the message. … … 405 412 void psTraceSetDestination(FILE *fp) 406 413 { 407 traceFP = fp;408 } 414 p_psTraceFP = fp; 415 } -
trunk/psLib/src/sys/psTrace.h
r978 r1013 1 /** @file psTrace.h 2 * \brief basic run-time trace facilities 3 * \ingroup LogTrace 4 * 5 * This file will hold the prototypes for defining procedures to insert 6 * trace messages into the code. 7 * 8 * @author Robert Lupton, Princeton University 9 * @author George Gusciora, MHPCC 10 * 11 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-06-12 05:50:01 $ 13 * 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 */ 1 16 #if !defined(PS_TRACE_H) 2 17 #define PS_TRACE_H 1 … … 4 19 #define DEFAULT_TRACE_LEVEL 0 5 20 6 /** \file psTrace.h 7 * \brief basic run-time trace facilities 8 * \ingroup LogTrace 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 *****************************************************************************/ 21 /** Basic structure for the component tree. A component is a string of the 22 form aaa.bbb.ccc, and may itself contain further subcomponents. The 23 Component structure doesn't in fact contain it's full name, but only the 24 last part. */ 16 25 typedef struct Component 17 26 { … … 26 35 * \{ 27 36 */ 28 29 37 30 38 /** Functions **************************************************************/ … … 59 67 ; 60 68 69 /// Set the destination of future trace messages. 61 70 void psTraceSetDestination(FILE *fp); 62 71 -
trunk/psLib/src/sysUtils/psHash.h
r1012 r1013 1 1 /** @file psHash.h 2 *3 2 * @brief Contains support for basic hashing functions. 3 * @ingroup HashTable 4 4 * 5 5 * This file will hold the prototypes for defining a hash table with arbitrary … … 7 7 * data from that hash table, and listing all keys defined in the hash table. 8 8 * 9 * @ingroup HashTable10 *11 9 * @author Robert Lupton, Princeton University 12 10 * @author George Gusciora, MHPCC 13 11 * 14 * @version $Revision: 1.1 2$ $Name: not supported by cvs2svn $15 * @date $Date: 2004-06-12 0 4:55:21 $12 * @version $Revision: 1.13 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-06-12 05:50:01 $ 16 14 * 17 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 18 16 */ 19 20 17 #if !defined(PS_HASH_H) 21 18 #define PS_HASH_H … … 25 22 */ 26 23 #include "psList.h" 27 28 24 29 25 /** A bucket that holds an item of data. */ … … 40 36 typedef struct psHash 41 37 { 42 int nbucket; // < Number of buckets in hash table.43 psHashBucket **buckets; // < The bucket data.38 int nbucket; ///< Number of buckets in hash table. 39 psHashBucket **buckets; ///< The bucket data. 44 40 } 45 41 psHash; 46 42 47 43 /// Allocate hash buckets in table. 48 psHash *psHashAlloc(int nbucket); 44 psHash *psHashAlloc(int nbucket ///< The number of buckets to allocate. 45 ); 49 46 50 47 /// Free hash buckets from table. -
trunk/psLib/src/sysUtils/psLogMsg.c
r979 r1013 1 /***************************************************************************** 2 A simple implementation of a logging facility for Pan-STARRS 3 1 /** @file psLogMsg.c 2 * @brief Procedures for logging messages. 3 * \ingroup LogTrace 4 * 5 * This file will hold the prototypes for defining procedure which set 6 * message log levels, messahe log formats, message log destinations, and 7 * for generating the messages themselves. 8 * @ingroup LogTrace 9 * 10 * @author Robert Lupton, Princeton University 11 * @author George Gusciora, MHPCC 12 * 13 * @version $Revision: 1.15 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-06-12 05:50:01 $ 15 * 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 17 */ 18 19 /***************************************************************************** 4 20 NOTES: currently, the prototype code has the following global variables: 5 static int logDest;6 static int logLevel;7 static int log_time;8 static int log_host;9 static int log_level;10 static int log_name;11 static int log_msg;21 static int p_psGlobalLogDest; 22 static int p_psGlobalLogLevel; 23 static int p_psLogTime; 24 static int p_psLogHost; 25 static int p_psLogLevel; 26 static int p_psLogName; 27 static int p_psLogMsg; 12 28 *****************************************************************************/ 13 29 #include <limits.h> … … 18 34 #include <time.h> 19 35 #include <unistd.h> 20 21 /* #include "psLib.h" */22 36 #include "psLogMsg.h" 23 37 #include "psError.h" … … 27 41 #define MIN_LOG_LEVEL 0 28 42 #define MAX_LOG_LEVEL 9 29 static int logDest = PS_LOG_TO_STDERR; // where to log messages30 static int logLevel = PS_LOG_INFO; // log all messages at this or above43 static int p_psGlobalLogDest = PS_LOG_TO_STDERR; // where to log messages 44 static int p_psGlobalLogLevel = PS_LOG_INFO; // log all messages at this or above 31 45 //static FILE *p_logFP = NULL; 32 46 // The following variables control 33 47 // which information is displayed in 34 48 // log messages. 35 static int log_time = 1; // Flag to include time info36 static int log_host = 1; // Flag to include host info37 static int log_level = 1; // Flag to include level info38 static int log_name = 1; // Flag to include name info39 static int log_msg = 1; // Flag to include message info49 static int p_psLogTime = 1; // Flag to include time info 50 static int p_psLogHost = 1; // Flag to include host info 51 static int p_psLogLevel = 1; // Flag to include level info 52 static int p_psLogName = 1; // Flag to include name info 53 static int p_psLogMsg = 1; // Flag to include message info 40 54 /***************************************************************************** 41 55 psLogSetLevel(): Set the current log level and return old level. … … 49 63 int psLogSetLevel(int level) 50 64 { 51 int oldLevel = logLevel; 65 // Save old global log level for changing it. 66 int oldLevel = p_psGlobalLogLevel; 52 67 53 68 if ((level < MIN_LOG_LEVEL) || (level > MAX_LOG_LEVEL)) { … … 57 72 } 58 73 59 logLevel = level; 74 // Set new global log level 75 p_psGlobalLogLevel = level; 76 77 // Return old global log level 60 78 return oldLevel; 61 79 } … … 75 93 int psLogSetDestination(int dest) 76 94 { 77 int old = logDest; 95 // Save old global log destination before changing it. 96 int old = p_psGlobalLogDest; 78 97 79 98 switch (dest) { … … 81 100 case PS_LOG_TO_STDOUT: 82 101 case PS_LOG_TO_STDERR: 83 logDest = dest; 102 // Set new global log destination 103 p_psGlobalLogDest = dest; 84 104 break; 85 105 86 106 default: 87 107 // GUS: Should you log this error properly? 88 fprintf(stderr,"Unknown logDestination: %d (ignored)\n", dest); 89 break; 90 } 91 108 fprintf(stderr,"Unknown p_psGlobalLogDest: %d (ignored)\n", dest); 109 break; 110 } 111 112 // Return old global log destination 92 113 return old; 93 114 } … … 112 133 void psLogSetFormat(const char *fmt) 113 134 { 114 int nlog_time = 0; 115 int nlog_host = 0; 116 int nlog_level = 0; 117 int nlog_name = 0; 118 int nlog_msg = 0; 135 136 // Step through each character in the format string. For each letter 137 // in that string, set/unset the appropriate logging. 119 138 120 139 for (const char *ptr = fmt; *ptr != '\0'; ptr++) { … … 122 141 case 'H': 123 142 case 'h': 124 nlog_host = 1;143 p_psLogHost = 1; 125 144 break; 126 145 case 'L': 127 146 case 'l': 128 nlog_level = 1;147 p_psLogLevel = 1; 129 148 break; 130 149 case 'M': 131 150 case 'm': 132 nlog_msg = 1;151 p_psLogMsg = 1; 133 152 break; 134 153 case 'N': 135 154 case 'n': 136 nlog_name = 1;155 p_psLogName = 1; 137 156 break; 138 157 case 'T': 139 158 case 't': 140 nlog_time = 1;159 p_psLogTime = 1; 141 160 break; 142 161 default: 143 // GUS: figure out the psError() format:144 // psError(__func__, PS_ERR_UNKNOWN, 1,145 // "Unknown logging keyword %c", *ptr);146 162 psError(__func__, "Unknown logging keyword %c", *ptr); 147 163 break; … … 149 165 } 150 166 151 if (! nlog_msg) {167 if (!p_psLogMsg) { 152 168 psTrace("utils.logMsg", 1, 153 169 "You must at least log error messages (You chose \"%s\")", 154 170 fmt); 155 171 } 156 157 log_host = nlog_host;158 log_level = nlog_level;159 log_msg = nlog_msg;160 log_name = nlog_name;161 log_time = nlog_time;162 172 } 163 173 … … 186 196 va_list ap) 187 197 { 188 static int first = 1; 198 static int first = 1; // Flag for calling gethostname() 189 199 static char hostname[HOST_NAME_MAX + 1]; 200 // Buffer for hostname. 190 201 char clevel=0; // letter-name for level 191 202 char head[HOST_NAME_MAX + 40]; // yes, this is long enough 192 203 char *head_ptr = head; // where we've got to in head 193 time_t clock = time(NULL); 194 struct tm *utc = gmtime(&clock); 195 196 if ((level > logLevel) || (logDest == PS_LOG_NONE)) { 204 time_t clock = time(NULL); // The current time. 205 struct tm *utc = gmtime(&clock); // The current gm time. 206 207 // If logging is off, or if the level is too high, return immediately. 208 if ((level > p_psGlobalLogLevel) || (p_psGlobalLogDest == PS_LOG_NONE)) { 197 209 return; 198 210 } 199 211 212 // If I have not been here yet, determine my hostname and save it. 200 213 if (first) { 201 214 first = 0; … … 236 249 } 237 250 238 239 if ( log_time) {251 // Create the various log fields... 252 if (p_psLogTime) { 240 253 sprintf(head_ptr, "%4d:%02d:%02d %02d:%02d:%02dZ", 241 254 utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday, … … 243 256 head_ptr += strlen(head_ptr); 244 257 } 245 if (log_host) { 258 // Hostname should be 20 characters. 259 if (p_psLogHost) { 246 260 if (head_ptr > head) { 247 261 *head_ptr++ = '|'; … … 250 264 head_ptr += strlen(head_ptr); 251 265 } 252 if ( log_level) {266 if (p_psLogLevel) { 253 267 if (head_ptr > head) { 254 268 *head_ptr++ = '|'; … … 257 271 head_ptr += strlen(head_ptr); 258 272 } 259 if (log_name) { 273 // The name field must be 15 characters. 274 if (p_psLogName) { 260 275 if (head_ptr > head) { 261 276 *head_ptr++ = '|'; 262 277 } 263 sprintf(head_ptr, "% -15s", name);278 sprintf(head_ptr, "%15.15s", name); 264 279 head_ptr += strlen(head_ptr); 265 280 } … … 270 285 *head_ptr = '\0'; 271 286 272 switch ( logDest) {287 switch (p_psGlobalLogDest) { 273 288 case PS_LOG_TO_STDOUT: 274 289 puts(head); 275 if ( log_msg) {290 if (p_psLogMsg) { 276 291 vprintf(fmt, ap); 277 292 if (fmt[strlen(fmt) - 1] != '\n') { … … 285 300 case PS_LOG_TO_STDERR: 286 301 fputs(head, stderr); 287 if ( log_msg) {302 if (p_psLogMsg) { 288 303 vfprintf(stderr, fmt, ap); 289 304 if (fmt[strlen(fmt) - 1] != '\n') { -
trunk/psLib/src/sysUtils/psLogMsg.h
r979 r1013 1 /** @file psLogMsg.h 2 * @brief Procedures for logging messages. 3 * \ingroup LogTrace 4 * 5 * This file will hold the prototypes for defining procedure which set 6 * message log levels, messahe log formats, message log destinations, and 7 * for generating the messages themselves. 8 * @ingroup LogTrace 9 * 10 * @author Robert Lupton, Princeton University 11 * @author George Gusciora, MHPCC 12 * 13 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-06-12 05:50:01 $ 15 * 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 17 */ 1 18 #if !defined(PS_LOG_MSG_H) 2 19 #define PS_LOG_MSG_H 3 4 /** \file psLogMsg.h5 * \brief log messaging facilities6 * \ingroup LogTrace7 */8 9 20 #include <stdarg.h> 10 21 … … 18 29 /// In future versions, this procedure will take a character string as an 19 30 /// argument which can specify more general log destinations. 20 int psLogSetDestination(int dest); 31 int psLogSetDestination(int dest ///< Specifies where to send messages. 32 ); 33 21 34 22 35 /// This procedure sets the message level for future log messages. Subsequent … … 25 38 /// Ie. higher values set by this procedure will cause more log messages to 26 39 /// be displayed. 27 int psLogSetLevel(int level); 40 int psLogSetLevel(int level ///< Specifies the system log level 41 ); 42 28 43 29 44 /// This procedure sets the log format for future log messages. The argument … … 32 47 /// Deleting a letter from the string will cause the associated information 33 48 /// to not be logged. 34 void psLogSetFormat(const char *fmt); 49 void psLogSetFormat(const char *fmt ///< Specifies the system log format 50 ); 35 51 36 52 … … 41 57 void psLogMsg(const char *name, ///< name of the log source 42 58 int myLevel, ///< severity level of this log message 43 const char *fmt, ...) ///< printf-style format command 44 ; 45 59 const char *fmt, ... ///< printf-style format command 60 ); 46 61 47 62 … … 51 66 int myLevel, ///< severity level of this log message 52 67 const char *fmt, ///< printf-style format command 53 va_list ap) ///< varargs argument list 54 ; 55 68 va_list ap ///< varargs argument list 69 ); 56 70 57 71 ///< Status codes for log messages -
trunk/psLib/src/sysUtils/psTrace.c
r928 r1013 1 /***************************************************************************** 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. 5 1 /** @file psTrace.c 2 * \brief basic run-time trace facilities 3 * \ingroup LogTrace 4 * 5 * This file will hold the code for procedures to insert 6 * trace messages into the code. 7 * 8 * @author Robert Lupton, Princeton University 9 * @author George Gusciora, MHPCC 10 * 11 * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-06-12 05:50:01 $ 13 * 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 */ 16 /***************************************************************************** 6 17 NOTES: 7 18 In the SRD, higher trace levels correspond to a numerically lower trace … … 16 27 trace level of that node. 17 28 18 This code is obfuscated by the fact that component names are of the form19 ".a.b.c.d" where "." is always the root of the tree, and for tree that have20 a depth of 3 or higher (including the root ".") the "." character is also21 the deliminator between different individual nodes in the full component22 name. So, for ".a.b.c.d", the first "." signifies the root of the tree,23 while the last three dots merely act as separators between the node names24 "b", "c", and "d".25 26 29 I added a function psTraceFree() which frees all nodes in the trace component 27 30 tree. Previously, there had been no function in the API which accomplished … … 32 35 #include <string.h> 33 36 #include <stdarg.h> 34 /* #include "pslib.h" */35 37 #include "psMemory.h" 36 38 #include "psTrace.h" … … 38 40 #include "psError.h" 39 41 40 static Component * croot = NULL;// The root of the trace component41 static FILE * traceFP = NULL;42 /***************************************************************************** 43 componentAlloc(): allocate memory for a new node, and initialize members.42 static Component *p_psCroot = NULL; // The root of the trace component 43 static FILE *p_psTraceFP = NULL; // File destination for messages. 44 /***************************************************************************** 45 componentAlloc(): allocate memory for a new node, and initialize members. 44 46 *****************************************************************************/ 45 47 Component *componentAlloc(const char *name, … … 56 58 57 59 /***************************************************************************** 58 componentFree(): free the current node in the root tree, and all 59 childrennodes as well.60 componentFree(): free the current node in the root tree, and all children 61 nodes as well. 60 62 *****************************************************************************/ 61 63 static void componentFree(Component *comp) … … 78 80 79 81 /***************************************************************************** 80 initTrace(): simply initialize the component root tree.82 initTrace(): simply initialize the component root tree. 81 83 *****************************************************************************/ 82 84 static void initTrace(void) 83 85 { 84 if ( croot == NULL) {85 croot = componentAlloc(".", DEFAULT_TRACE_LEVEL);86 } 87 } 88 89 90 /***************************************************************************** 91 Set all trace levels to zero.86 if (p_psCroot == NULL) { 87 p_psCroot = componentAlloc(".", DEFAULT_TRACE_LEVEL); 88 } 89 } 90 91 92 /***************************************************************************** 93 Set all trace levels to zero. 92 94 *****************************************************************************/ 93 95 void p_psTraceReset(Component *currentNode) … … 112 114 } 113 115 116 /***************************************************************************** 117 Set all trace levels to zero. 118 *****************************************************************************/ 114 119 void psTraceReset() 115 120 { 116 p_psTraceReset(croot); 117 } 118 119 121 p_psTraceReset(p_psCroot); 122 } 123 124 125 /***************************************************************************** 126 Free all nodes in the component tree. 127 *****************************************************************************/ 120 128 void psTraceFree() 121 129 { 122 componentFree( croot);123 } 124 125 126 /***************************************************************************** 127 componentAdd(): Adds the component named "addNodeName" to the root tree.130 componentFree(p_psCroot); 131 } 132 133 134 /***************************************************************************** 135 componentAdd(): Adds the component named "addNodeName" to the root tree. 128 136 129 NOTE: replace the call to strsep() with a call to strtok(), which conforms130 to ANSI-C.137 NOTE: replace the call to strsep() with a call to strtok(), which conforms 138 to ANSI-C. 131 139 *****************************************************************************/ 132 140 static void componentAdd(const char *addNodeName, 133 141 int level) 134 142 { 135 int i = 0; 136 char name[strlen(addNodeName) + 1]; 137 // buffer for writeable copy. 143 int i = 0; // Loop index variable. 144 char name[strlen(addNodeName) + 1]; // buffer for writeable copy. 138 145 char *pname=name; 139 146 char *firstComponent = NULL; // first component of name 140 Component *currentNode = croot;147 Component *currentNode = p_psCroot; 141 148 int nodeExists = 0; 142 149 143 150 // Is this the root node? If so, simply set level and return. 144 151 if (strcmp(".", addNodeName) == 0) { 145 croot->level = level;152 p_psCroot->level = level; 146 153 return; 147 154 } … … 199 206 { 200 207 // If the root component tree does not exist, then initialize it. 201 if ( croot == NULL) {208 if (p_psCroot == NULL) { 202 209 initTrace(); 203 210 } … … 230 237 char *pname=name; 231 238 char *firstComponent = NULL; // first component of name 232 Component *currentNode = croot;239 Component *currentNode = p_psCroot; 233 240 int i = 0; 234 241 … … 238 245 239 246 if (strcmp(".", aname) == 0) { 240 return( croot->level);247 return(p_psCroot->level); 241 248 } 242 249 … … 282 289 int psTraceGetLevel(const char *name) 283 290 { 284 if ( croot == NULL) {291 if (p_psCroot == NULL) { 285 292 return(UNKNOWN_TRACE_LEVEL); 286 293 } … … 328 335 329 336 /***************************************************************************** 330 psPrintTraceLevels() 331 Simply print all the trace levels in the trace levelcomponent tree.332 Inputs:333 none 334 Outputs:335 none 336 Returns:337 psPrintTraceLevels(): Simply print all the trace levels in the trace level 338 component tree. 339 Inputs: 340 none 341 Outputs: 342 none 343 Returns: 337 344 null 338 345 *****************************************************************************/ 339 346 void psTracePrintLevels(void) 340 347 { 341 if ( croot == NULL) {348 if (p_psCroot == NULL) { 342 349 return; 343 350 } 344 351 345 doPrintTraceLevels( croot, 0);346 } 347 348 349 /***************************************************************************** 350 p_psTrace(): we display the trace message to standard output if the trace351 level of that message, supplied by the parameter "level" is higher 352 than the trace level that is currently associated with the component 353 named by theparameter "comp".354 Input:352 doPrintTraceLevels(p_psCroot, 0); 353 } 354 355 356 /***************************************************************************** 357 p_psTrace(): we display the trace message to standard output if the trace 358 level of that message, supplied by the parameter "level" is higher than the 359 trace level that is currently associated with the component named by the 360 parameter "comp". 361 Input: 355 362 comp 356 363 level 357 364 ... a printf-style output string. 358 Output:359 none 360 Return:365 Output: 366 none 367 Return: 361 368 null 362 369 *****************************************************************************/ … … 384 391 fmt = va_arg(ap, char *); 385 392 386 if ( traceFP != NULL) {393 if (p_psTraceFP != NULL) { 387 394 // We indent each message one space for each level of the message. 388 395 for (i = 0; i < level; i++) { 389 fprintf( traceFP, " ");396 fprintf(p_psTraceFP, " "); 390 397 } 391 vfprintf( traceFP, fmt, ap);398 vfprintf(p_psTraceFP, fmt, ap); 392 399 } else { 393 400 // We indent each message one space for each level of the message. … … 405 412 void psTraceSetDestination(FILE *fp) 406 413 { 407 traceFP = fp;408 } 414 p_psTraceFP = fp; 415 } -
trunk/psLib/src/sysUtils/psTrace.h
r978 r1013 1 /** @file psTrace.h 2 * \brief basic run-time trace facilities 3 * \ingroup LogTrace 4 * 5 * This file will hold the prototypes for defining procedures to insert 6 * trace messages into the code. 7 * 8 * @author Robert Lupton, Princeton University 9 * @author George Gusciora, MHPCC 10 * 11 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-06-12 05:50:01 $ 13 * 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 */ 1 16 #if !defined(PS_TRACE_H) 2 17 #define PS_TRACE_H 1 … … 4 19 #define DEFAULT_TRACE_LEVEL 0 5 20 6 /** \file psTrace.h 7 * \brief basic run-time trace facilities 8 * \ingroup LogTrace 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 *****************************************************************************/ 21 /** Basic structure for the component tree. A component is a string of the 22 form aaa.bbb.ccc, and may itself contain further subcomponents. The 23 Component structure doesn't in fact contain it's full name, but only the 24 last part. */ 16 25 typedef struct Component 17 26 { … … 26 35 * \{ 27 36 */ 28 29 37 30 38 /** Functions **************************************************************/ … … 59 67 ; 60 68 69 /// Set the destination of future trace messages. 61 70 void psTraceSetDestination(FILE *fp); 62 71
Note:
See TracChangeset
for help on using the changeset viewer.
