diff --git a/src/it/order/verify.bsh b/src/it/order/verify.bsh index 1b78a91..1f4a434 100644 --- a/src/it/order/verify.bsh +++ b/src/it/order/verify.bsh @@ -7,7 +7,7 @@ import java.nio.file.Files; for(;;) { String line = reader.readLine(); if(line == null) { - throw new AssertionError(expected + " not found"); + throw new IllegalStateException(expected + " not found"); } if(line.equals(expected)) { return; @@ -28,9 +28,9 @@ import java.nio.file.Files; file = new File(basedir, "build.log" ); reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8); - findLine(reader, "[INFO] one started"); - findLine(reader, "[ERROR] one again"); - findLine(reader, "[INFO] two started"); - findLine(reader, "[ERROR] two again"); + findLine(reader, "[INFO] [order-one] one started"); + findLine(reader, "[ERROR] [order-one] one again"); + findLine(reader, "[INFO] [order-two] two started"); + findLine(reader, "[ERROR] [order-two] two again"); diff --git a/src/main/java/org/honton/chas/process/exec/maven/plugin/ExecProcess.java b/src/main/java/org/honton/chas/process/exec/maven/plugin/ExecProcess.java index 2c6f4ba..4a92108 100644 --- a/src/main/java/org/honton/chas/process/exec/maven/plugin/ExecProcess.java +++ b/src/main/java/org/honton/chas/process/exec/maven/plugin/ExecProcess.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; import org.apache.maven.plugin.logging.Log; +import org.honton.chas.process.exec.maven.plugin.StdoutRedirector.LineWriter; public class ExecProcess { private Process process; @@ -63,8 +64,18 @@ private void redirectToLogFile(ProcessBuilder pb) throws IOException { } private void redirectStream() throws IOException { - new StdoutRedirector(process.getInputStream(), log, false); - new StdoutRedirector(process.getErrorStream(), log, true); + new StdoutRedirector(name, process.getInputStream(), new LineWriter() { + @Override + public void writeLine(String line) { + log.info(line); + } + }); + new StdoutRedirector(name, process.getErrorStream(), new LineWriter() { + @Override + public void writeLine(String line) { + log.error(line); + } + }); } public void destroy() { diff --git a/src/main/java/org/honton/chas/process/exec/maven/plugin/StdoutRedirector.java b/src/main/java/org/honton/chas/process/exec/maven/plugin/StdoutRedirector.java index cbb8d6b..5a7bd20 100644 --- a/src/main/java/org/honton/chas/process/exec/maven/plugin/StdoutRedirector.java +++ b/src/main/java/org/honton/chas/process/exec/maven/plugin/StdoutRedirector.java @@ -8,14 +8,18 @@ public class StdoutRedirector extends Thread { + interface LineWriter { + void writeLine(String line); + } + + private final String streamName; private final BufferedReader in; - private final Log log; - private final boolean isErr; + private final LineWriter lineWriter; - StdoutRedirector(InputStream in, Log log, boolean isErr) { + StdoutRedirector(String streamName, InputStream in, LineWriter lineWriter) { + this.streamName = streamName; this.in = new BufferedReader(new InputStreamReader(in)); - this.log = log; - this.isErr = isErr; + this.lineWriter = lineWriter; setDaemon(true); start(); } @@ -23,7 +27,12 @@ public class StdoutRedirector extends Thread { @Override public void run() { try { - while (oneLine()) { + for(;;){ + String line = in.readLine(); + if (line == null) { + return; + } + lineWriter.writeLine('[' + streamName + "] " + line); } } catch (IOException ignore) { } finally { @@ -34,18 +43,4 @@ public void run() { } } } - - private boolean oneLine() throws IOException { - String line = in.readLine(); - if(line == null) { - return false; - } - if(isErr) { - log.error(line); - } - else { - log.info(line); - } - return true; - } }