Changeset 1407 for trunk/psLib/src/sysUtils/psLogMsg.c
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sysUtils/psLogMsg.c (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sysUtils/psLogMsg.c
r1406 r1407 1 1 2 /** @file psLogMsg.c 2 3 * @brief Procedures for logging messages. … … 11 12 * @author George Gusciora, MHPCC 12 13 * 13 * @version $Revision: 1.2 3$ $Name: not supported by cvs2svn $14 * @date $Date: 2004-08-0 6 22:34:05$14 * @version $Revision: 1.24 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-08-07 00:06:06 $ 15 16 * 16 17 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 44 45 45 46 #define MAX_LOG_LINE_LENGTH 128 46 static FILE * logDest = (FILE*)1;// flag to initialize to stderr before using.47 static int globalLogLevel = PS_LOG_INFO; // log all messages at this or above48 static bool logTime = true; // Flag to include time info49 static bool logHost = true; // Flag to include host info50 static bool logLevel = true; // Flag to include level info51 static bool logName = true; // Flag to include name info52 static bool logMsg = true; // Flag to include message info47 static FILE *logDest = (FILE *) 1; // flag to initialize to stderr before using. 48 static int globalLogLevel = PS_LOG_INFO; // log all messages at this or above 49 static bool logTime = true; // Flag to include time info 50 static bool logHost = true; // Flag to include host info 51 static bool logLevel = true; // Flag to include level info 52 static bool logName = true; // Flag to include name info 53 static bool logMsg = true; // Flag to include message info 53 54 54 55 /***************************************************************************** … … 67 68 68 69 if ((level < MIN_LOG_LEVEL) || (level > MAX_LOG_LEVEL)) { 69 psLogMsg("logmsg", PS_LOG_WARN, 70 "Attempt to set invalid logMsg level: %d", level); 70 psLogMsg("logmsg", PS_LOG_WARN, "Attempt to set invalid logMsg level: %d", level); 71 71 level = (level < MIN_LOG_LEVEL) ? MIN_LOG_LEVEL : MAX_LOG_LEVEL; 72 72 } 73 74 73 // Set new global log level 75 74 globalLogLevel = level; … … 78 77 return oldLevel; 79 78 } 80 81 79 82 80 /***************************************************************************** … … 91 89 An integer specifying the old log destination. 92 90 *****************************************************************************/ 93 int psLogSetDestination(const char *dest)91 int psLogSetDestination(const char *dest) 94 92 { 95 93 char protocol[5]; … … 97 95 98 96 // if logDest has not been initialized, do so before using it 99 if (logDest ==(FILE*)1) {97 if (logDest == (FILE *) 1) { 100 98 logDest = stderr; 101 99 } 102 100 103 if (dest == NULL || strcmp(dest, "none")==0) {101 if (dest == NULL || strcmp(dest, "none") == 0) { 104 102 if (logDest != NULL && logDest != stderr && logDest != stdout) { 105 103 fclose(logDest); … … 109 107 } 110 108 111 if (sscanf(dest, "%4s:%256s",protocol,location) < 2) {112 psError(__func__, "The specified destination, %s, is malformed.", dest);109 if (sscanf(dest, "%4s:%256s", protocol, location) < 2) { 110 psError(__func__, "The specified destination, %s, is malformed.", dest); 113 111 return 1; 114 112 } 115 113 116 if (strcmp(protocol, "dest") == 0) {117 if (strcmp(location, "stderr") == 0) {114 if (strcmp(protocol, "dest") == 0) { 115 if (strcmp(location, "stderr") == 0) { 118 116 if (logDest != NULL && logDest != stderr && logDest != stdout) { 119 117 fclose(logDest); … … 122 120 return 0; 123 121 } 124 if (strcmp(location, "stdout") == 0) {122 if (strcmp(location, "stdout") == 0) { 125 123 if (logDest != NULL && logDest != stderr && logDest != stdout) { 126 124 fclose(logDest); … … 129 127 return 0; 130 128 } 131 psError(__func__, "The location, %s, for protocol 'dest' is invalid.",location);129 psError(__func__, "The location, %s, for protocol 'dest' is invalid.", location); 132 130 return 1; 133 } else 134 if (strcmp(protocol,"file") == 0) {135 FILE* file = fopen(location,"w"); 136 if (file == NULL) {137 psError(__func__,"Could not open file '%s' for output.",location);138 return 1;139 }140 if (logDest != NULL && logDest != stderr && logDest != stdout) {141 fclose(logDest);142 }143 logDest = file;144 return 0;145 }146 147 psError(__func__, "Do not know how to handle the protocol '%s'.",protocol);131 } else if (strcmp(protocol, "file") == 0) { 132 FILE *file = fopen(location, "w"); 133 134 if (file == NULL) { 135 psError(__func__, "Could not open file '%s' for output.", location); 136 return 1; 137 } 138 if (logDest != NULL && logDest != stderr && logDest != stdout) { 139 fclose(logDest); 140 } 141 logDest = file; 142 return 0; 143 } 144 145 psError(__func__, "Do not know how to handle the protocol '%s'.", protocol); 148 146 return 1; 149 147 } … … 181 179 fmt = "THLNM"; 182 180 } 183 184 181 // Step through each character in the format string. For each letter 185 182 // in that string, set/unset the appropriate logging. … … 214 211 215 212 if (!logMsg) { 216 psTrace("utils.logMsg", 1, 217 "You must at least log error messages (You chose \"%s\")", 218 fmt); 219 } 220 } 221 222 223 224 #if !defined(HOST_NAME_MAX) // should be in limits.h 225 # define HOST_NAME_MAX 256 213 psTrace("utils.logMsg", 1, "You must at least log error messages (You chose \"%s\")", fmt); 214 } 215 } 216 217 #if !defined(HOST_NAME_MAX) // should be in limits.h 218 # define HOST_NAME_MAX 256 226 219 #endif 220 227 221 /***************************************************************************** 228 222 psVLogMsg(): This routine sends the message, which is a printf style … … 239 233 NULL. 240 234 *****************************************************************************/ 241 void psLogMsgV(const char *name, 242 int level, 243 const char *fmt, 244 va_list ap) 245 { 246 static int first = 1; // Flag for calling gethostname() 235 void psLogMsgV(const char *name, int level, const char *fmt, va_list ap) 236 { 237 static int first = 1; // Flag for calling gethostname() 247 238 static char hostname[HOST_NAME_MAX + 1]; 239 248 240 // Buffer for hostname. 249 char clevel =0;// letter-name for level250 char head[MAX_LOG_LINE_LENGTH +2];// the added two are for the ending | and \0251 char *head_ptr = head; // where we've got to in head241 char clevel = 0; // letter-name for level 242 char head[MAX_LOG_LINE_LENGTH + 2]; // the added two are for the ending | and \0 243 char *head_ptr = head; // where we've got to in head 252 244 int maxLength = MAX_LOG_LINE_LENGTH; 253 time_t clock = time(NULL); // The current time.245 time_t clock = time(NULL); // The current time. 254 246 struct tm *utc = gmtime(&clock); // The current gm time. 255 247 256 248 // if logDest has not been initialized, do so before using it 257 if (logDest ==(FILE*)1) {249 if (logDest == (FILE *) 1) { 258 250 logDest = stderr; 259 251 } 260 261 252 // If logging is off, or if the level is too high, return immediately. 262 253 if ((level > globalLogLevel) || (logDest == NULL)) { 263 254 return; 264 255 } 265 266 256 // If I have not been here yet, determine my hostname and save it. 267 257 if (first) { … … 297 287 298 288 default: 299 psTrace("utils.logMsg", 2, "Invalid logMsg level: %d (%s)\n", 300 level, fmt); 289 psTrace("utils.logMsg", 2, "Invalid logMsg level: %d (%s)\n", level, fmt); 301 290 level = (level < 0) ? 0 : 9; 302 291 break; … … 307 296 maxLength -= snprintf(head_ptr, maxLength, "%4d:%02d:%02d %02d:%02d:%02dZ", 308 297 utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday, 309 utc->tm_hour, utc->tm_min, utc->tm_sec) -1;298 utc->tm_hour, utc->tm_min, utc->tm_sec) - 1; 310 299 head_ptr += strlen(head_ptr); 311 300 } … … 336 325 if (head_ptr > head) { 337 326 *head_ptr++ = '|'; 338 } else 339 if (!logMsg) { // no output desired 340 return; 341 } 327 } else if (!logMsg) { // no output desired 328 return; 329 } 342 330 *head_ptr = '\0'; 343 331 … … 352 340 } 353 341 } 354 355 342 356 343 /***************************************************************************** … … 369 356 NULL 370 357 *****************************************************************************/ 371 void psLogMsg(const char *name, 372 int level, 373 const char *fmt, 374 ...) 358 void psLogMsg(const char *name, int level, const char *fmt, ...) 375 359 { 376 360 va_list ap;
Note:
See TracChangeset
for help on using the changeset viewer.
