#4792 Calculated log message size rather than using fixed size

This commit is contained in:
Jerry (Xinyu Hou) 2016-07-14 07:55:13 -07:00
parent 18c2c90144
commit 83c0dea2e4
3 changed files with 19 additions and 14 deletions

View File

@ -172,22 +172,33 @@ Log::print(const char* file, int line, const char* fmt, ...)
// do not prefix time and file for kPRINT (CLOG_PRINT)
if (priority != kPRINT) {
char message[kLogMessageLength];
struct tm *tm;
char tmp[220];
char timestamp[50];
time_t t;
time(&t);
tm = localtime(&t);
sprintf(tmp, "%04i-%02i-%02iT%02i:%02i:%02i", tm->tm_year + 1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
sprintf(timestamp, "%04i-%02i-%02iT%02i:%02i:%02i", tm->tm_year + 1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
// square brackets, spaces, comma and null terminator take about 10
int size = 10;
size += strlen(timestamp);
size += strlen(g_priority[priority]);
size += strlen(buffer);
#ifndef NDEBUG
size += strlen(file);
// assume there is no file contains over 100k lines of code
size += 6;
#endif
char* message = new char[size];
#ifndef NDEBUG
sprintf(message, "[%s] %s: %s\n\t%s,%d", tmp, g_priority[priority], buffer, file, line);
sprintf(message, "[%s] %s: %s\n\t%s,%d", timestamp, g_priority[priority], buffer, file, line);
#else
sprintf(message, "[%s] %s: %s", tmp, g_priority[priority], buffer);
sprintf(message, "[%s] %s: %s", timestamp, g_priority[priority], buffer);
#endif
output(priority, message);
delete[] message;
} else {
output(priority, buffer);
}

View File

@ -139,8 +139,6 @@ private:
int m_maxPriority;
};
const UInt16 kLogMessageLength = 2048;
/*!
\def LOG(arg)
Write to the log. Because macros cannot accept variable arguments, this

View File

@ -230,11 +230,7 @@ ProtocolUtil::vreadf(synergy::IStream* stream, const char* fmt, va_list args)
throw;
}
// don't cause buffer overrun, using +100 chars in case
// someone modifies this log message in future.
if (len + 100 < kLogMessageLength) {
LOG((CLOG_DEBUG2 "readf: read %d byte string: %.*s", len, len, sBuffer));
}
// save the data
String* dst = va_arg(args, String*);