Changeset 1013 for trunk/psLib/src/sys/psLogMsg.c
- Timestamp:
- Jun 11, 2004, 7:50:01 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psLogMsg.c (modified) (17 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') {
Note:
See TracChangeset
for help on using the changeset viewer.
