-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from 1 commit
95a7887
fb7dd2d
503e0d8
1f544bf
656c80d
9a693e7
aec8bad
d5d8959
4da324f
489dd9b
2bacce8
6191e6e
25fec6c
64b1e0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
public abstract class BaseConsumer<SELF extends BaseConsumer<SELF>> implements Consumer<OutputFrame> { | ||
private StringBuilder logString = new StringBuilder(); | ||
private OutputFrame brokenFrame; | ||
private boolean removeColorCodes = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also add Lombok's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. plus |
||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid making it |
||
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 |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.