Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LoggingOutputStream to work on windows #51779

Merged
merged 1 commit into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ public void write(int b) throws IOException {
public void flush() {
Buffer buffer = threadLocal.get();
if (buffer.used == 0) return;
log(new String(buffer.bytes, 0, buffer.used, StandardCharsets.UTF_8));
int used = buffer.used;
if (buffer.bytes[used - 1] == '\r') {
mark-vieira marked this conversation as resolved.
Show resolved Hide resolved
// windows case: remove the first part of newlines there too
--used;
}
log(new String(buffer.bytes, 0, used, StandardCharsets.UTF_8));
if (buffer.bytes.length != DEFAULT_BUFFER_LENGTH) {
threadLocal.set(new Buffer()); // reset size
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,17 @@ public void testNull() {
assertTrue(loggingStream.lines.isEmpty());
}

public void testFlushOnNewline() {
printStream.println("hello");
printStream.println("world");
// this test explicitly outputs the newlines instead of relying on println, to always test the unix behavior
public void testFlushOnUnixNewline() {
printStream.print("hello\n");
printStream.print("world\n");
assertThat(loggingStream.lines, contains("hello", "world"));
}

// this test explicitly outputs the newlines instead of relying on println, to always test the windows behavior
public void testFlushOnWindowsNewline() {
printStream.print("hello\r\n");
printStream.print("world\r\n");
assertThat(loggingStream.lines, contains("hello", "world"));
}

Expand Down