Fix windows empty line in logging capture (#52162)

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
This commit is contained in:
Ryan Ernst 2020-02-10 16:29:29 -05:00 committed by Ryan Ernst
parent 6b600855a9
commit 88cf8ac0a8
2 changed files with 13 additions and 4 deletions

View File

@ -96,6 +96,10 @@ class LoggingOutputStream extends OutputStream {
// windows case: remove the first part of newlines there too // windows case: remove the first part of newlines there too
--used; --used;
} }
if (used == 0) {
// only windows \r was in the buffer
return;
}
log(new String(buffer.bytes, 0, used, StandardCharsets.UTF_8)); log(new String(buffer.bytes, 0, used, StandardCharsets.UTF_8));
if (buffer.bytes.length != DEFAULT_BUFFER_LENGTH) { if (buffer.bytes.length != DEFAULT_BUFFER_LENGTH) {
threadLocal.set(new Buffer()); // reset size threadLocal.set(new Buffer()); // reset size

View File

@ -58,9 +58,15 @@ public class LoggingOutputStreamTests extends ESTestCase {
printStream = new PrintStream(loggingStream, false, StandardCharsets.UTF_8.name()); printStream = new PrintStream(loggingStream, false, StandardCharsets.UTF_8.name());
} }
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/51838") public void testEmptyLineUnix() {
public void testEmptyLine() { printStream.print("\n");
printStream.println(""); assertTrue(loggingStream.lines.isEmpty());
printStream.flush();
assertTrue(loggingStream.lines.isEmpty());
}
public void testEmptyLineWindows() {
printStream.print("\r\n");
assertTrue(loggingStream.lines.isEmpty()); assertTrue(loggingStream.lines.isEmpty());
printStream.flush(); printStream.flush();
assertTrue(loggingStream.lines.isEmpty()); assertTrue(loggingStream.lines.isEmpty());
@ -96,7 +102,6 @@ public class LoggingOutputStreamTests extends ESTestCase {
assertThat(loggingStream.threadLocal.get().bytes.length, equalTo(DEFAULT_BUFFER_LENGTH)); assertThat(loggingStream.threadLocal.get().bytes.length, equalTo(DEFAULT_BUFFER_LENGTH));
} }
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/51838")
public void testMaxBuffer() { public void testMaxBuffer() {
String longStr = randomAlphaOfLength(MAX_BUFFER_LENGTH); String longStr = randomAlphaOfLength(MAX_BUFFER_LENGTH);
String extraLongStr = longStr + "OVERFLOW"; String extraLongStr = longStr + "OVERFLOW";