From d773bd378a0c33a716400e6dfd44dfbee3658e63 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 19 Oct 2020 11:27:07 +0200 Subject: [PATCH] Grab and redirect standard streams, fixes #100 --- .../jboss/fuse/mvnd/client/DefaultClient.java | 2 +- .../smart/LoggingExecutionListener.java | 8 +++ .../logging/smart/LoggingOutputStream.java | 68 +++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 daemon/src/main/java/org/jboss/fuse/mvnd/logging/smart/LoggingOutputStream.java diff --git a/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java b/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java index 8ee21cf2..98aaca36 100644 --- a/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java +++ b/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java @@ -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); diff --git a/daemon/src/main/java/org/jboss/fuse/mvnd/logging/smart/LoggingExecutionListener.java b/daemon/src/main/java/org/jboss/fuse/mvnd/logging/smart/LoggingExecutionListener.java index 3d4171c3..3c6a5fcf 100644 --- a/daemon/src/main/java/org/jboss/fuse/mvnd/logging/smart/LoggingExecutionListener.java +++ b/daemon/src/main/java/org/jboss/fuse/mvnd/logging/smart/LoggingExecutionListener.java @@ -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; } diff --git a/daemon/src/main/java/org/jboss/fuse/mvnd/logging/smart/LoggingOutputStream.java b/daemon/src/main/java/org/jboss/fuse/mvnd/logging/smart/LoggingOutputStream.java new file mode 100644 index 00000000..8ce4e938 --- /dev/null +++ b/daemon/src/main/java/org/jboss/fuse/mvnd/logging/smart/LoggingOutputStream.java @@ -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 consumer; + + public LoggingOutputStream(Consumer consumer) { + this(new EolBaos(), consumer); + } + + LoggingOutputStream(EolBaos out, Consumer 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; + } + } +}