Skip to content

Commit

Permalink
add name to lines redirected to maven log
Browse files Browse the repository at this point in the history
  • Loading branch information
chonton committed Apr 16, 2017
1 parent 7c19fb0 commit 84e1a0d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
10 changes: 5 additions & 5 deletions src/it/order/verify.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");


Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@

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();
}

@Override
public void run() {
try {
while (oneLine()) {
for(;;){
String line = in.readLine();
if (line == null) {
return;
}
lineWriter.writeLine('[' + streamName + "] " + line);
}
} catch (IOException ignore) {
} finally {
Expand All @@ -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;
}
}

0 comments on commit 84e1a0d

Please sign in to comment.