Grab and redirect standard streams, fixes #100

This commit is contained in:
Guillaume Nodet
2020-10-19 11:27:07 +02:00
parent 9834f77a41
commit d773bd378a
3 changed files with 77 additions and 1 deletions

View File

@@ -59,7 +59,7 @@ public class DefaultClient implements Client {
if (i < argv.length) {
logFile = Paths.get(argv[i++]);
} else {
throw new IllegalArgumentException("-l and --log-file need to befollowed by a path");
throw new IllegalArgumentException("-l and --log-file need to be followed by a path");
}
} else {
args.add(arg);

View File

@@ -17,12 +17,20 @@ package org.jboss.fuse.mvnd.logging.smart;
import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.ExecutionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
public class LoggingExecutionListener implements ExecutionListener {
private final ExecutionListener delegate;
static {
Logger logger = LoggerFactory.getLogger("org.jboss.fuse.mvnd");
System.setOut(new LoggingOutputStream(s -> logger.info("[stdout] " + s)).printStream());
System.setErr(new LoggingOutputStream(s -> logger.info("[stderr] " + s)).printStream());
}
public LoggingExecutionListener(ExecutionListener delegate) {
this.delegate = delegate;
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.fuse.mvnd.logging.smart;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.function.Consumer;
public class LoggingOutputStream extends FilterOutputStream {
static final byte[] LINE_SEP = System.lineSeparator().getBytes();
final EolBaos buf;
final Consumer<String> consumer;
public LoggingOutputStream(Consumer<String> consumer) {
this(new EolBaos(), consumer);
}
LoggingOutputStream(EolBaos out, Consumer<String> consumer) {
super(out);
this.buf = out;
this.consumer = consumer;
}
public PrintStream printStream() {
return new PrintStream(this);
}
@Override
public void write(int b) throws IOException {
super.write(b);
if (buf.isEol()) {
String line = new String(buf.toByteArray(), 0, buf.size() - LINE_SEP.length);
consumer.accept(line);
buf.reset();
}
}
static class EolBaos extends ByteArrayOutputStream {
boolean isEol() {
if (count >= LINE_SEP.length) {
for (int i = 0; i < LINE_SEP.length; i++) {
if (buf[count - LINE_SEP.length + i] != LINE_SEP[i]) {
return false;
}
}
return true;
}
return false;
}
}
}