Changeset 7568
- Timestamp:
- Jun 14, 2006, 2:14:49 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psLogMsg.c (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psLogMsg.c
r7300 r7568 1 2 1 /** @file psLogMsg.c 3 2 * @brief Procedures for logging messages. … … 12 11 * @author GLG, MHPCC 13 12 * 14 * @version $Revision: 1.5 3$ $Name: not supported by cvs2svn $15 * @date $Date: 2006-06- 02 21:33:34$13 * @version $Revision: 1.54 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2006-06-15 00:14:49 $ 16 15 * 17 16 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii 18 17 */ 19 18 20 /*****************************************************************************21 NOTES: currently, the prototype code has the following global variables:22 static psS32 p_psGlobalLogDest;23 static psS32 p_psGlobalLogLevel;24 static psS32 p_psLogTime;25 static psS32 p_psLogHost;26 static psS32 p_psLogLevel;27 static psS32 p_psLogName;28 static psS32 p_psLogMsg;29 *****************************************************************************/30 19 #include "config.h" 31 20 … … 39 28 #include <fcntl.h> 40 29 30 #include "psMemory.h" 41 31 #include "psLogMsg.h" 42 32 #include "psError.h" 43 33 #include "psTrace.h" 34 #include "psList.h" 35 #include "psString.h" 44 36 45 37 #include "psErrorText.h" … … 50 42 #define MAX_LOG_LINE_LENGTH 256 51 43 52 static FILE *logDest = (FILE *) 1; // flag to initialize to stderr before using. 53 static int logFD = 0; 54 static psS32 globalLogLevel = PS_LOG_INFO; // log all messages at this or above 44 static int logFD = 2; // Log file descriptor 45 static psS32 globalLogLevel = PS_LOG_INFO; // log all messages at this or above 55 46 static psBool logTime = true; // Flag to include time info 56 47 static psBool logHost = true; // Flag to include host info … … 84 75 int psLogGetLevel() 85 76 { 86 return (globalLogLevel);77 return globalLogLevel; 87 78 } 88 79 … … 96 87 bool psLogSetDestination(int fd) 97 88 { 98 FILE *fp; 99 // if logDest has not been initialized, do so before using it 100 if (logDest == (FILE *) 1) { 101 logDest = stderr; 102 logFD = 2; 103 } 104 if (fd == -1) 89 if (fd <= -1) { 105 90 return false; 106 107 if ( fd == 1 ) { 108 // fp = stdout; 109 if (logDest != NULL && logDest != stderr && logDest != stdout) { 110 fclose(logDest); 111 } 112 logDest = stdout; 113 logFD = 1; 114 } else if (fd == 2) { 115 // fp = stderr; 116 if (logDest != NULL && logDest != stderr && logDest != stdout) { 117 fclose(logDest); 118 } 119 logDest = stderr; 120 logFD = 2; 121 } else if (fd == 0) { 122 // fp = NULL; 123 if (logDest != NULL && logDest != stderr && logDest != stdout) { 124 fclose(logDest); 125 } 126 logDest = NULL; 127 logFD = 0; 128 // return true; 129 } else if (fd > 2) { 130 if (logDest != NULL && logDest != stderr && logDest != stdout) { 131 fclose(logDest); 132 } 133 fp = fdopen(fd, "w"); 134 logDest = fp; 135 logFD = fd; 136 // fclose(fp); 137 // return true; 138 } 91 } 92 93 // Close the current FD if it's not stdout, stderr. 94 if (logFD > 2) { 95 close(logFD); 96 } 97 logFD = fd; 139 98 140 99 return true; 141 142 100 } 143 101 144 102 int psLogGetDestination(void) 145 103 { 146 if (logDest == (FILE *) 1) { 147 logFD = 1; 148 } 149 return (logFD); 104 return logFD; 150 105 } 151 106 … … 222 177 } 223 178 224 int psMessageDestination (const char *dest) 225 { 226 char protocol[5]; 227 char location[257]; 228 if (logDest == (FILE *) 1) { 229 logDest = stderr; 230 } 231 179 int psMessageDestination(const char *dest) 180 { 181 // No destination 232 182 if (dest == NULL || strcmp(dest, "none") == 0) { 233 if (logDest != NULL && logDest != stderr && logDest != stdout) {234 fclose(logDest);235 }236 logDest = NULL;237 183 return 0; 238 184 } 239 if (sscanf(dest, "%4s:%256s", protocol, location) < 2) { 240 psError(PS_ERR_LOCATION_INVALID, true, 241 PS_ERRORTEXT_psLogMsg_DESTINATION_MALFORMED, 242 dest); 185 186 // Special destinations: stdout, stderr 187 if (strcmp(dest, "stdout") == 0) { 188 return 1; 189 } 190 if (strcmp(dest, "stderr") == 0) { 191 return 2; 192 } 193 194 // Destination is protocol:location 195 psList *protocolLocation = psStringSplit(dest, ":", false); // A list containing the protocol and location 196 if (protocolLocation->n != 2) { 197 psFree(protocolLocation); 198 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unable to parse protocol:location from: %s\n", dest); 243 199 return -1; 244 200 } 245 if (strcmp(protocol, "dest") == 0) { 246 if (strcmp(location, "stderr") == 0) { 247 if (logDest != NULL && logDest != stderr && logDest != stdout) { 248 fclose(logDest); 249 } 250 logDest = stderr; 251 return 2; 252 } 253 if (strcmp(location, "stdout") == 0) { 254 if (logDest != NULL && logDest != stderr && logDest != stdout) { 255 fclose(logDest); 256 } 257 logDest = stdout; 258 return 1; 259 } 260 psError(PS_ERR_LOCATION_INVALID, true, PS_ERRORTEXT_psLogMsg_DEST_LOCATION_INVALID, 261 location); 262 return -1; 263 } else if (strcmp(protocol, "file") == 0) { 201 const char *protocol = psListGet(protocolLocation, PS_LIST_HEAD); // The protocol 202 const char *location = psListGet(protocolLocation, PS_LIST_TAIL); // The location 203 204 if (strcmp(protocol, "file") == 0) { 264 205 // FILE *file = fopen(location, "w"); 265 206 int fileD = creat(location, 0666); … … 268 209 psError(PS_ERR_IO, true, PS_ERRORTEXT_psLogMsg_OPEN_FILE_FAILED, 269 210 location); 211 psFree(protocolLocation); 270 212 return -1; 271 213 } 272 if (logDest != NULL && logDest != stderr && logDest != stdout) { 273 fclose(logDest); 274 } 275 logDest = fdopen(fileD, "w"); 214 psFree(protocolLocation); 276 215 return fileD; 277 216 } 217 218 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unrecognised protocol: %s\n", protocol); 219 psFree(protocolLocation); 278 220 return -1; 279 221 } … … 284 226 285 227 /***************************************************************************** 286 ps VLogMsg(): This routine sends the message, which is a printf style string228 psLogMsgV(): This routine sends the message, which is a printf style string 287 229 specified in the "..." argument, to the current message log destination with 288 230 the severity specified by the "level" argument. … … 306 248 struct tm *utc = gmtime(&clock); // The current gm time. 307 249 308 // if logDest has not been initialized, do so before using it309 if (logDest == (FILE *) 1) {310 logDest = stderr;311 }312 250 // If it's an abort, we always want to see the message 313 if (log Dest == NULL&& level == PS_LOG_ABORT) {314 log Dest = stderr;251 if (logFD == 0 && level == PS_LOG_ABORT) { 252 logFD = 2; 315 253 } 316 254 // If logging is off, or if the level is too high, return immediately. 317 if ((level > globalLogLevel) || (log Dest == NULL)) {255 if ((level > globalLogLevel) || (logFD == 0)) { 318 256 return; 319 257 } … … 395 333 *head_ptr = '\0'; 396 334 397 fputs(head, logDest);335 write(logFD, head, strlen(head)); 398 336 if (logMsg) { 399 337 char msg[1024]; … … 402 340 403 341 // detect multiple lines in message and indent each line by 4 spaces. 404 char* line = strtok_r(msg,"\n",&msgPtr); 405 while (line != NULL) { 406 fprintf(logDest," %s\n",line); 407 line = strtok_r(NULL,"\n",&msgPtr); 408 } 342 char *line = strtok_r(msg, "\n", &msgPtr); 343 do { 344 write(logFD, " ", 4); 345 write(logFD, line, strlen(line)); 346 write(logFD, "\n", 1); 347 } while ((line = strtok_r(NULL, "\n", &msgPtr))); 409 348 } else { 410 fputc('\n', logDest);349 write(logFD, "\n", 1); 411 350 } 412 351 413 352 if (level == PS_LOG_ABORT) { 414 fflush(logDest); 353 switch (logFD) { 354 case 1: 355 fflush(stdout); 356 break; 357 case 2: 358 fflush(stderr); 359 break; 360 // For the others, write() should send it unbuffered...? 361 } 362 415 363 } 416 364 }
Note:
See TracChangeset
for help on using the changeset viewer.
