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

More accurate log processing. #643

Merged
merged 14 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,76 @@
package org.testcontainers.containers.output;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

@SuppressWarnings("Duplicates")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what does it duplicate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh... It's just a refactoring artifact. Will remove this.

public abstract class BaseConsumer<SELF extends BaseConsumer<SELF>> implements Consumer<OutputFrame> {
private StringBuilder logString = new StringBuilder();
private OutputFrame brokenFrame;
private boolean removeColorCodes = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add Lombok's @Setter to this field

Copy link
Member

@bsideup bsideup Apr 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plus @Getter


public SELF withRemoveAnsiCodes(boolean removeAnsiCodes) {
this.removeColorCodes = removeAnsiCodes;
return (SELF) this;
}

public abstract void process(OutputFrame outputFrame);

@Override
public final synchronized void accept(OutputFrame outputFrame) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid making it final breaks the binary compatibility because there might be some strategies based on i.e. Slf4jLogConsumer but overriding accept (I think I even have one)

if (outputFrame != null) {
String utf8String = outputFrame.getUtf8String();
byte[] bytes = outputFrame.getBytes();

if (utf8String != null && !utf8String.isEmpty()) {
// Merging the strings by bytes to solve the problem breaking non-latin unicode symbols.
if (brokenFrame != null) {
bytes = merge(brokenFrame.getBytes(), bytes);
utf8String = new String(bytes);
brokenFrame = null;
}
// Logger chunks can break the string in middle of multibyte unicode character.
// Backup the bytes to reconstruct proper char sequence with bytes from next line.
if (Character.getType(utf8String.charAt(utf8String.length() - 1)) == Character.OTHER_SYMBOL) {
brokenFrame = new OutputFrame(outputFrame.getType(), bytes);
return;
}

if (removeColorCodes) {
utf8String = utf8String.replaceAll("\u001B\\[[0-9;]+m", "");
}

// Reformat strings to normalize enters.
List<String> lines = new ArrayList<>(Arrays.asList(utf8String.split("((\\r?\\n)|(\\r))")));
if (lines.isEmpty()) {
process(new OutputFrame(outputFrame.getType(), "".getBytes()));
return;
}
if (utf8String.startsWith("\n") || utf8String.startsWith("\r")) {
lines.add(0, "");
}
if (utf8String.endsWith("\n") || utf8String.endsWith("\r")) {
lines.add("");
}
for (int i = 0; i < lines.size() - 1; i++) {
String line = lines.get(i);
if (i == 0 && logString.length() > 0) {
line = logString.toString() + line;
logString.setLength(0);
}
process(new OutputFrame(outputFrame.getType(), line.getBytes()));
}
logString.append(lines.get(lines.size() - 1));
}
}
}

private byte[] merge(byte[] str1, byte[] str2) {
byte[] mergedString = new byte[str1.length + str2.length];
System.arraycopy(str1, 0, mergedString, 0, str1.length);
System.arraycopy(str2, 0, mergedString, str1.length, str2.length);
return mergedString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

import org.slf4j.Logger;

import java.util.function.Consumer;
import java.util.regex.Pattern;

/**
* A consumer for container output that logs output to an SLF4J logger.
*/
public class Slf4jLogConsumer implements Consumer<OutputFrame> {
public class Slf4jLogConsumer extends BaseConsumer<Slf4jLogConsumer> {
private final Logger logger;
private String prefix = "";

private static final Pattern ANSI_CODE_PATTERN = Pattern.compile("\\[\\d[ABCD]");

public Slf4jLogConsumer(Logger logger) {
this.logger = logger;
}
Expand All @@ -24,29 +19,19 @@ public Slf4jLogConsumer withPrefix(String prefix) {
}

@Override
public void accept(OutputFrame outputFrame) {
if (outputFrame != null) {
String utf8String = outputFrame.getUtf8String();

if (utf8String != null) {
OutputFrame.OutputType outputType = outputFrame.getType();
String message = utf8String.trim();

if (ANSI_CODE_PATTERN.matcher(message).matches()) {
return;
}

switch (outputType) {
case END:
break;
case STDOUT:
case STDERR:
logger.info("{}{}: {}", prefix, outputType, message);
break;
default:
throw new IllegalArgumentException("Unexpected outputType " + outputType);
}
}
public void process(OutputFrame outputFrame) {
OutputFrame.OutputType outputType = outputFrame.getType();

String utf8String = outputFrame.getUtf8String();
switch (outputType) {
case END:
break;
case STDOUT:
case STDERR:
logger.info("{}{}: {}", prefix, outputType, utf8String);
break;
default:
throw new IllegalArgumentException("Unexpected outputType " + outputType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.function.Consumer;

/**
* Created by rnorth on 26/03/2016.
*/
public class ToStringConsumer implements Consumer<OutputFrame> {
public class ToStringConsumer extends BaseConsumer<ToStringConsumer> {
private static final byte[] ENTER_BYTES = "\n".getBytes();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming: "NEW_LINE" or "LINE_BREAK"


private ByteArrayOutputStream stringBuffer = new ByteArrayOutputStream();

@Override
public void accept(OutputFrame outputFrame) {
public void process(OutputFrame outputFrame) {
try {
if (outputFrame.getBytes() != null) {
stringBuffer.write(outputFrame.getBytes());
stringBuffer.write(ENTER_BYTES);
stringBuffer.flush();
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
* A consumer for container output that buffers lines in a {@link java.util.concurrent.BlockingDeque} and enables tests
* to wait for a matching condition.
*/
public class WaitingConsumer implements Consumer<OutputFrame> {
public class WaitingConsumer extends BaseConsumer<WaitingConsumer> {

private static final Logger LOGGER = LoggerFactory.getLogger(WaitingConsumer.class);

private LinkedBlockingDeque<OutputFrame> frames = new LinkedBlockingDeque<>();

@Override
public void accept(OutputFrame frame) {
public void process(OutputFrame frame) {
frames.add(frame);
}

Expand Down