Skip to content

Commit

Permalink
Grab and redirect standard streams, fixes apache#100
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 19, 2020
1 parent ce00d16 commit 39064e7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Expand Up @@ -59,7 +59,7 @@ public static void main(String[] argv) throws Exception {
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);
Expand Down
Expand Up @@ -17,12 +17,20 @@

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;
}
Expand Down
@@ -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;
}
}
}

0 comments on commit 39064e7

Please sign in to comment.