diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2016-09-09 14:09:38 -0400 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2016-09-09 14:09:38 -0400 |
commit | 25c6eb9a0c87709ad80a896d737e8ab977eac883 (patch) | |
tree | 781d2208fcb3fe39aa900b8d3a9d0c438a6be43b /src/mongo | |
parent | 25ff17991ee95d996642eb6684ce37d927863ce7 (diff) | |
download | mongo-25c6eb9a0c87709ad80a896d737e8ab977eac883.tar.gz |
SERVER-25926 Windows audit bson output erroneously translates LF to CR LF
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/logger/message_event_utf8_encoder.cpp | 34 | ||||
-rw-r--r-- | src/mongo/logger/rotatable_file_writer.cpp | 31 |
2 files changed, 35 insertions, 30 deletions
diff --git a/src/mongo/logger/message_event_utf8_encoder.cpp b/src/mongo/logger/message_event_utf8_encoder.cpp index 827876d46f5..68fc6923f0f 100644 --- a/src/mongo/logger/message_event_utf8_encoder.cpp +++ b/src/mongo/logger/message_event_utf8_encoder.cpp @@ -46,6 +46,14 @@ MessageEventDetailsEncoder::DateFormatter MessageEventDetailsEncoder::getDateFor return _dateFormatter; } +namespace { +#ifdef _WIN32 +constexpr auto kEOL = "\r\n"_sd; +#else +constexpr auto kEOL = "\n"_sd; +#endif +} // namespace + MessageEventDetailsEncoder::~MessageEventDetailsEncoder() {} std::ostream& MessageEventDetailsEncoder::encode(const MessageEventEphemeral& event, std::ostream& os) { @@ -67,6 +75,26 @@ std::ostream& MessageEventDetailsEncoder::encode(const MessageEventEphemeral& ev } StringData msg = event.getMessage(); + +#ifdef _WIN32 + // We need to translate embedded Unix style line endings into Windows style endings. + std::string tempstr; + size_t embeddedNewLine = msg.find('\n'); + + if (embeddedNewLine != std::string::npos) { + tempstr = msg.toString().replace(embeddedNewLine, 1, "\r\n"); + + embeddedNewLine = tempstr.find('\n', embeddedNewLine + 2); + while (embeddedNewLine != std::string::npos) { + tempstr = tempstr.replace(embeddedNewLine, 1, "\r\n"); + + embeddedNewLine = tempstr.find('\n', embeddedNewLine + 2); + } + + msg = tempstr; + } +#endif + if (event.isTruncatable() && msg.size() > maxLogLine) { os << "warning: log line attempted (" << msg.size() / 1024 << "kB) over max size (" << maxLogLine / 1024 << "kB), printing beginning and end ... "; @@ -76,8 +104,10 @@ std::ostream& MessageEventDetailsEncoder::encode(const MessageEventEphemeral& ev } else { os << msg; } - if (!msg.endsWith(StringData("\n"_sd))) - os << '\n'; + + if (!msg.endsWith(kEOL)) + os << kEOL; + return os; } diff --git a/src/mongo/logger/rotatable_file_writer.cpp b/src/mongo/logger/rotatable_file_writer.cpp index 561869645ca..4c053f19615 100644 --- a/src/mongo/logger/rotatable_file_writer.cpp +++ b/src/mongo/logger/rotatable_file_writer.cpp @@ -97,8 +97,6 @@ private: virtual std::streamsize xsputn(const char* s, std::streamsize count); virtual int_type overflow(int_type ch = traits_type::eof()); - std::streamsize writeToFile(const char* s, std::streamsize count); - HANDLE _fileHandle; }; @@ -163,7 +161,8 @@ bool Win32FileStreambuf::open(StringData fileName, bool append) { return false; } -std::streamsize Win32FileStreambuf::writeToFile(const char* s, std::streamsize count) { +// Called when strings are written to ostream +std::streamsize Win32FileStreambuf::xsputn(const char* s, std::streamsize count) { DWORD totalBytesWritten = 0; while (count > totalBytesWritten) { @@ -171,32 +170,8 @@ std::streamsize Win32FileStreambuf::writeToFile(const char* s, std::streamsize c if (!WriteFile(_fileHandle, s, count - totalBytesWritten, &bytesWritten, NULL)) { break; } - totalBytesWritten += bytesWritten; - } - - return totalBytesWritten; -} - -// Called when strings are written to ostream -std::streamsize Win32FileStreambuf::xsputn(const char* s, std::streamsize count) { - DWORD totalBytesWritten = 0; - - // Scan for embedded newlines before end - // this should be rare since the newline should only be at the end - const char* startPos = s; - for (int i = 0; i < count; i++) { - if (s[i] == '\n') { - totalBytesWritten += writeToFile(startPos, i - (startPos - s)); - writeToFile("\r\n", 2); - totalBytesWritten += 1; // Caller expected we only wrote 1 char, so tell them so - startPos = &s[i + 1]; - } - } - // Did the string not end on "\n"? Write the remaining, no need for CRLF - // as upper layers are responsible for it - if ((startPos - s) != count) { - totalBytesWritten += writeToFile(startPos, count - (startPos - s)); + totalBytesWritten += bytesWritten; } return totalBytesWritten; |