--- os/log.c.old 2010-01-23 02:08:06.000000000 +0100 +++ os/log.c 2010-01-23 03:34:45.000000000 +0100 @@ -108,6 +108,8 @@ #endif static FILE *logFile = NULL; +static Bool logTimestamp = TRUE; +static Bool logFileTimestamp = TRUE; static Bool logFlush = FALSE; static Bool logSync = FALSE; static int logVerbosity = DEFAULT_LOG_VERBOSITY; @@ -257,7 +259,12 @@ LogVWrite(int verb, const char *f, va_list args) { static char tmpBuffer[1024]; + static char timestamp[21]; int len = 0; + size_t timestamp_len = 0; + Bool hasNewline = FALSE; + time_t t; + struct tm *tm; /* * Since a va_list can only be processed once, write the string to a @@ -267,12 +274,34 @@ if (verb < 0 || logFileVerbosity >= verb || logVerbosity >= verb) { vsnprintf(tmpBuffer, sizeof(tmpBuffer), f, args); len = strlen(tmpBuffer); + if (len == 0) + return; + } else { + return; } - if ((verb < 0 || logVerbosity >= verb) && len > 0) + if (logTimestamp || logFileTimestamp) { + t = time(NULL); + if (t != (time_t)-1) + tm = localtime(&t); + else + tm = NULL; + if (tm != NULL) + timestamp_len = strftime(timestamp, sizeof(timestamp), "%F %T ", tm); + } + hasNewline = (strchr(tmpBuffer, '\n') != NULL); + if ((verb < 0 || logVerbosity >= verb)) { + if (logTimestamp && timestamp_len) + fwrite(timestamp, timestamp_len, 1, stderr); + logTimestamp = hasNewline; fwrite(tmpBuffer, len, 1, stderr); - if ((verb < 0 || logFileVerbosity >= verb) && len > 0) { + } + if ((verb < 0 || logFileVerbosity >= verb)) { if (logFile) { + if (logFileTimestamp && timestamp_len) + fwrite(timestamp, timestamp_len, 1, logFile); + logFileTimestamp = hasNewline; fwrite(tmpBuffer, len, 1, logFile); + if (logFlush) { fflush(logFile); #ifndef WIN32 @@ -286,8 +315,8 @@ * xalloc() and friends can't be used. */ if (len > bufferUnused) { - bufferSize += 1024; - bufferUnused += 1024; + bufferSize += 1024 + timestamp_len; + bufferUnused += 1024 + timestamp_len; if (saveBuffer) saveBuffer = realloc(saveBuffer, bufferSize); else @@ -295,7 +324,9 @@ if (!saveBuffer) FatalError("realloc() failed while saving log messages\n"); } - bufferUnused -= len; + bufferUnused -= (timestamp_len + len); + memcpy(saveBuffer + bufferPos, timestamp, timestamp_len); + bufferPos += timestamp_len; memcpy(saveBuffer + bufferPos, tmpBuffer, len); bufferPos += len; }