Skip to content

Commit

Permalink
logsink: fix multiple issues with LogSink::ToString()
Browse files Browse the repository at this point in the history
1. Initializing std::ostringstream with a string makes no sense, as the
   string becomes an initial value of an underlying buffer; seek-to-end
   is not performed, so the initial value gets completely overwritten by
   subsequent writing.

2. Flag `log_year_in_prefix` should be considered, as if formatting a
   regular logging message.

3. Writing a buffer to std::ostream is better expressed with write(s,n).
  • Loading branch information
anpol committed Aug 13, 2022
1 parent 278ed96 commit 0642ff6
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2115,12 +2115,14 @@ void LogSink::WaitTillSent() {
string LogSink::ToString(LogSeverity severity, const char* file, int line,
const LogMessageTime& logmsgtime, const char* message,
size_t message_len) {
ostringstream stream(string(message, message_len));
ostringstream stream;
stream.fill('0');

stream << LogSeverityNames[severity][0]
<< setw(4) << 1900 + logmsgtime.year()
<< setw(2) << 1 + logmsgtime.month()
stream << LogSeverityNames[severity][0];
if (FLAGS_log_year_in_prefix) {
stream << setw(4) << 1900 + logmsgtime.year();
}
stream << setw(2) << 1 + logmsgtime.month()
<< setw(2) << logmsgtime.day()
<< ' '
<< setw(2) << logmsgtime.hour() << ':'
Expand All @@ -2132,7 +2134,14 @@ string LogSink::ToString(LogSeverity severity, const char* file, int line,
<< ' '
<< file << ':' << line << "] ";

stream << string(message, message_len);
#ifdef _MSC_VER
#pragma push_macro("_write")
#undef _write
#endif // _MSC_VER
stream.write(message, static_cast<std::streamsize>(message_len));
#ifdef _MSC_VER
#pragma pop_macro("_write")
#endif // _MSC_VER
return stream.str();
}

Expand Down

0 comments on commit 0642ff6

Please sign in to comment.