Skip to content

Commit 61622c4

Browse files
committed
Fix LoggingOutputStream to work on windows (#51779)
LoggingOutputStream reads a stream and breaks on newlines. This commit fixes the behavior to account for windows newlines also containing `\r`. closes #51532
1 parent 4594a21 commit 61622c4

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

server/src/main/java/org/elasticsearch/common/logging/LoggingOutputStream.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ public void write(int b) throws IOException {
9191
public void flush() {
9292
Buffer buffer = threadLocal.get();
9393
if (buffer.used == 0) return;
94-
log(new String(buffer.bytes, 0, buffer.used, StandardCharsets.UTF_8));
94+
int used = buffer.used;
95+
if (buffer.bytes[used - 1] == '\r') {
96+
// windows case: remove the first part of newlines there too
97+
--used;
98+
}
99+
log(new String(buffer.bytes, 0, used, StandardCharsets.UTF_8));
95100
if (buffer.bytes.length != DEFAULT_BUFFER_LENGTH) {
96101
threadLocal.set(new Buffer()); // reset size
97102
} else {

server/src/test/java/org/elasticsearch/common/logging/LoggingOutputStreamTests.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,17 @@ public void testNull() {
7171
assertTrue(loggingStream.lines.isEmpty());
7272
}
7373

74-
public void testFlushOnNewline() {
75-
printStream.println("hello");
76-
printStream.println("world");
74+
// this test explicitly outputs the newlines instead of relying on println, to always test the unix behavior
75+
public void testFlushOnUnixNewline() {
76+
printStream.print("hello\n");
77+
printStream.print("world\n");
78+
assertThat(loggingStream.lines, contains("hello", "world"));
79+
}
80+
81+
// this test explicitly outputs the newlines instead of relying on println, to always test the windows behavior
82+
public void testFlushOnWindowsNewline() {
83+
printStream.print("hello\r\n");
84+
printStream.print("world\r\n");
7785
assertThat(loggingStream.lines, contains("hello", "world"));
7886
}
7987

0 commit comments

Comments
 (0)