Skip to content

Commit

Permalink
Fix windows empty line in logging capture (#52162)
Browse files Browse the repository at this point in the history
This commit fixes another edge case in handling windows newlines in our
capture of stdout/stderr to log4j. The case is that the \r appears at
the beginning of the buffer when flushing, which would unintentionally
be emitted as an empty string. This commit skips the flush if only a \r
was found.

closes #51838
  • Loading branch information
rjernst committed Feb 10, 2020
1 parent 6b60085 commit 88cf8ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public void flush() {
// windows case: remove the first part of newlines there too
--used;
}
if (used == 0) {
// only windows \r was in the buffer
return;
}
log(new String(buffer.bytes, 0, used, StandardCharsets.UTF_8));
if (buffer.bytes.length != DEFAULT_BUFFER_LENGTH) {
threadLocal.set(new Buffer()); // reset size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ public void createStream() throws Exception {
printStream = new PrintStream(loggingStream, false, StandardCharsets.UTF_8.name());
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/51838")
public void testEmptyLine() {
printStream.println("");
public void testEmptyLineUnix() {
printStream.print("\n");
assertTrue(loggingStream.lines.isEmpty());
printStream.flush();
assertTrue(loggingStream.lines.isEmpty());
}

public void testEmptyLineWindows() {
printStream.print("\r\n");
assertTrue(loggingStream.lines.isEmpty());
printStream.flush();
assertTrue(loggingStream.lines.isEmpty());
Expand Down Expand Up @@ -96,7 +102,6 @@ public void testBufferExtension() {
assertThat(loggingStream.threadLocal.get().bytes.length, equalTo(DEFAULT_BUFFER_LENGTH));
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/51838")
public void testMaxBuffer() {
String longStr = randomAlphaOfLength(MAX_BUFFER_LENGTH);
String extraLongStr = longStr + "OVERFLOW";
Expand Down

0 comments on commit 88cf8ac

Please sign in to comment.