diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2014-03-12 11:36:23 -0400 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2014-05-23 18:26:55 -0400 |
commit | 57d2c1fd5e7a43b33947b1c96aca2128cc41c5a7 (patch) | |
tree | e273290b085ea31037994dbb8852de98a8cadbe0 /src/mongo/logger/rotatable_file_writer.cpp | |
parent | 99fa4e6058a24d6c4d7fde19ce940719c5bbc210 (diff) | |
download | mongo-57d2c1fd5e7a43b33947b1c96aca2128cc41c5a7.tar.gz |
SERVER-12803: Use CR/LF in log files on Windows
Diffstat (limited to 'src/mongo/logger/rotatable_file_writer.cpp')
-rw-r--r-- | src/mongo/logger/rotatable_file_writer.cpp | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/src/mongo/logger/rotatable_file_writer.cpp b/src/mongo/logger/rotatable_file_writer.cpp index f84747d18f9..8b456e3f9fc 100644 --- a/src/mongo/logger/rotatable_file_writer.cpp +++ b/src/mongo/logger/rotatable_file_writer.cpp @@ -96,6 +96,8 @@ namespace { 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; }; @@ -165,8 +167,9 @@ namespace { return false; } - std::streamsize Win32FileStreambuf::xsputn(const char* s, std::streamsize count) { + std::streamsize Win32FileStreambuf::writeToFile(const char* s, std::streamsize count) { DWORD totalBytesWritten = 0; + while (count > totalBytesWritten) { DWORD bytesWritten; if (!WriteFile( @@ -179,9 +182,36 @@ namespace { } 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)); + } + return totalBytesWritten; } + // Overflow is called for single character writes to the ostream Win32FileStreambuf::int_type Win32FileStreambuf::overflow(int_type ch) { if (ch == traits_type::eof()) return ~ch; // Returning traits_type::eof() => failure, anything else => success. |