--- os/log.c.old 2010-01-23 02:08:06.000000000 +0100 +++ os/log.c 2010-01-23 05:09:15.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; @@ -251,13 +253,76 @@ } } +static void +_LogWriteFile(FILE *file, Bool *writePrefix, char *prefix, size_t prefixLen, char *message, size_t len) +{ + const char *ptr, *pos; + size_t sublen; + + 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); + *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 +332,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 @@ -286,8 +368,13 @@ * xalloc() and friends can't be used. */ if (len > bufferUnused) { - bufferSize += 1024; - bufferUnused += 1024; + Bool tmpLog = logFileTimestamp; + size_t more = _LogWriteBuffer(&tmpLog, + timestamp, timestampLen, tmpBuffer, len, FALSE); + if (more < 1024) + more = 1024; + bufferSize += more; + bufferUnused += more; if (saveBuffer) saveBuffer = realloc(saveBuffer, bufferSize); else @@ -295,9 +382,8 @@ 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); } } }