--- os/log.c.old 2010-01-23 02:08:06.000000000 +0100 +++ os/log.c 2010-01-26 02:17:56.000000000 +0100 @@ -108,6 +108,9 @@ #endif static FILE *logFile = NULL; +static Bool logUseTimestamp = TRUE; +static Bool logTimestamp = TRUE; +static Bool logFileTimestamp = TRUE; static Bool logFlush = FALSE; static Bool logSync = FALSE; static int logVerbosity = DEFAULT_LOG_VERBOSITY; @@ -251,13 +254,79 @@ } } +static void +_LogWriteFile(FILE *file, Bool *writePrefix, char *prefix, size_t prefixLen, char *message, size_t len) +{ + const char *ptr, *pos; + size_t sublen; + + if (!logUseTimestamp) + *writePrefix = FALSE; + ptr = message; + do { + pos = strchr(ptr, '\n'); + if (pos != NULL) + sublen = pos-ptr+1; + else + sublen = len; + if (*writePrefix && prefixLen) + fwrite(prefix, prefixLen, 1, file); + if (logUseTimestamp) + *writePrefix = (pos != NULL); + fwrite(ptr, sublen, 1, file); + ptr += sublen; + len -= sublen; + } while (len); +} + +static size_t +_LogWriteBuffer(Bool *writePrefix, char *prefix, size_t prefixLen, char *message, size_t len, Bool write) +{ + const char *ptr, *pos; + size_t sublen; + size_t total; + + total = 0; + ptr = message; + do { + pos = strchr(ptr, '\n'); + if (pos != NULL) + sublen = pos-ptr+1; + else + sublen = len; + if (*writePrefix && prefixLen) { + total += prefixLen; + if (write) { + bufferUnused -= prefixLen; + memcpy(saveBuffer + bufferPos, prefix, prefixLen); + bufferPos += prefixLen; + } + } + *writePrefix = (pos != NULL); + total += sublen; + if (write) { + bufferUnused -= sublen; + memcpy(saveBuffer + bufferPos, ptr, sublen); + bufferPos += sublen; + } + ptr += sublen; + len -= sublen; + } while (len); + return total; +} + /* This function does the actual log message writes. */ void LogVWrite(int verb, const char *f, va_list args) { static char tmpBuffer[1024]; + static char timestamp[21]; + const char *pos; int len = 0; + size_t timestampLen = 0; + time_t t; + struct tm *tm; /* * Since a va_list can only be processed once, write the string to a @@ -267,12 +336,29 @@ if (verb < 0 || logFileVerbosity >= verb || logVerbosity >= verb) { vsnprintf(tmpBuffer, sizeof(tmpBuffer), f, args); len = strlen(tmpBuffer); + if (len == 0) + return; + } else { + return; + } + pos = strchr(tmpBuffer, '\n'); + if (logTimestamp || logFileTimestamp || (pos != NULL && *(pos+1) != '\0')) { + t = time(NULL); + if (t != (time_t)-1) + tm = localtime(&t); + else + tm = NULL; + if (tm != NULL) + timestampLen = strftime(timestamp, sizeof(timestamp), "%F %T ", tm); + } + if ((verb < 0 || logVerbosity >= verb)) { + _LogWriteFile(stderr, &logTimestamp, + timestamp, timestampLen, tmpBuffer, len); } - if ((verb < 0 || logVerbosity >= verb) && len > 0) - fwrite(tmpBuffer, len, 1, stderr); - if ((verb < 0 || logFileVerbosity >= verb) && len > 0) { + if ((verb < 0 || logFileVerbosity >= verb)) { if (logFile) { - fwrite(tmpBuffer, len, 1, logFile); + _LogWriteFile(logFile, &logFileTimestamp, + timestamp, timestampLen, tmpBuffer, len); if (logFlush) { fflush(logFile); #ifndef WIN32 @@ -281,13 +367,18 @@ #endif } } else if (needBuffer) { + Bool tmpLog = logFileTimestamp; + size_t more = _LogWriteBuffer(&tmpLog, + timestamp, timestampLen, tmpBuffer, len, FALSE); /* * Note, this code is used before OsInit() has been called, so * xalloc() and friends can't be used. */ - if (len > bufferUnused) { - bufferSize += 1024; - bufferUnused += 1024; + if (more > (size_t)bufferUnused) { + if (more < 1024) + more = 1024; + bufferSize += more; + bufferUnused += more; if (saveBuffer) saveBuffer = realloc(saveBuffer, bufferSize); else @@ -295,11 +386,18 @@ if (!saveBuffer) FatalError("realloc() failed while saving log messages\n"); } - bufferUnused -= len; - memcpy(saveBuffer + bufferPos, tmpBuffer, len); - bufferPos += len; + _LogWriteBuffer(&logFileTimestamp, + timestamp, timestampLen, tmpBuffer, len, TRUE); + } } } + +void +LogDisableTimestamp(void) +{ + logUseTimestamp = FALSE; + logTimestamp = FALSE; + logFileTimestamp = FALSE; } void --- os/utils.c.old 2010-01-26 02:27:53.000000000 +0100 +++ os/utils.c 2010-01-26 02:08:13.000000000 +0100 @@ -715,6 +715,7 @@ } else if ( strcmp( argv[i], "-help") == 0) { + LogDisableTimestamp(); UseMsg(); exit(0); } @@ -962,6 +963,7 @@ } else { + LogDisableTimestamp(); ErrorF("Unrecognized option: %s\n", argv[i]); UseMsg(); FatalError("Unrecognized option: %s\n", argv[i]);