Add customized listener that is aware of ansi colorization and can, if needed, strip out ansi sequences when stream is not a tty Minor hack when -l option is set to force jansi to strip sequences.
Project: http://git-wip-us.apache.org/repos/asf/maven/repo Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/c279b0ab Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/c279b0ab Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/c279b0ab Branch: refs/heads/slf4j-gossip Commit: c279b0ab49cad8d37b1976e16ae498fd735b1813 Parents: 700ed37 Author: Jason Dillon <ja...@planet57.com> Authored: Fri May 20 16:58:13 2016 -0700 Committer: Hervé Boutemy <hbout...@apache.org> Committed: Thu Jun 2 23:41:45 2016 +0200 ---------------------------------------------------------------------- .../java/org/apache/maven/cli/MavenCli.java | 3 + .../impl/gossip/ColorConsoleListener.java | 67 ++++++++++++++++++++ .../org.sonatype.gossip/config.properties | 2 +- 3 files changed, 71 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven/blob/c279b0ab/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java ---------------------------------------------------------------------- diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 6ac77de..a72c254 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -466,6 +466,9 @@ public class MavenCli File logFile = new File( cliRequest.commandLine.getOptionValue( CLIManager.LOG_FILE ) ); logFile = resolveFile( logFile, cliRequest.workingDirectory ); + // HACK: disable any colorization when writing log file + System.setProperty( "jansi.strip", Boolean.TRUE.toString() ); + // redirect stdout and stderr to file try { http://git-wip-us.apache.org/repos/asf/maven/blob/c279b0ab/maven-embedder/src/main/java/org/apache/maven/cli/logging/impl/gossip/ColorConsoleListener.java ---------------------------------------------------------------------- diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/logging/impl/gossip/ColorConsoleListener.java b/maven-embedder/src/main/java/org/apache/maven/cli/logging/impl/gossip/ColorConsoleListener.java new file mode 100644 index 0000000..4f80738 --- /dev/null +++ b/maven-embedder/src/main/java/org/apache/maven/cli/logging/impl/gossip/ColorConsoleListener.java @@ -0,0 +1,67 @@ +package org.apache.maven.cli.logging.impl.gossip; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import java.io.PrintStream; + +import org.fusesource.jansi.AnsiConsole; + +import org.sonatype.gossip.Event; +import org.sonatype.gossip.listener.ListenerSupport; + +/** + * Specialized {@link org.sonatype.gossip.listener.Listener} which is aware of ANSI streams. + * + * @author <a href="mailto:ja...@planet57.com">Jason Dillon</a> + * @since 3.4.0 + */ +public class ColorConsoleListener + extends ListenerSupport +{ + private PrintStream out; + + private PrintStream getOut() + { + if ( out == null ) + { + // wrapping has logic which can detect, to some limited degree, if ansi is supported and strip if needed + out = new PrintStream( AnsiConsole.wrapOutputStream( System.out ) ); + } + return out; + } + + @Override + public void onEvent( final Event event ) throws Exception + { + assert event != null; + + if ( !isLoggable( event ) ) + { + return; + } + + PrintStream out = getOut(); + synchronized ( out ) + { + out.print( render( event ) ); + out.flush(); + } + } +} http://git-wip-us.apache.org/repos/asf/maven/blob/c279b0ab/maven-embedder/src/main/resources/META-INF/org.sonatype.gossip/config.properties ---------------------------------------------------------------------- diff --git a/maven-embedder/src/main/resources/META-INF/org.sonatype.gossip/config.properties b/maven-embedder/src/main/resources/META-INF/org.sonatype.gossip/config.properties index acec235..587b2be 100644 --- a/maven-embedder/src/main/resources/META-INF/org.sonatype.gossip/config.properties +++ b/maven-embedder/src/main/resources/META-INF/org.sonatype.gossip/config.properties @@ -56,7 +56,7 @@ profile.maven-plain.trigger.default.ignoreCase=true profile.maven-color.includes=maven-common profile.maven-color.listeners=console -profile.maven-color.listener.console=org.sonatype.gossip.listener.ConsoleListener +profile.maven-color.listener.console=org.apache.maven.cli.logging.impl.gossip.ColorConsoleListener profile.maven-color.listener.console.renderer=org.apache.maven.cli.logging.impl.gossip.ColorRenderer profile.maven-color.listener.console.renderer.pattern=[%l] %m%n%x profile.maven-color.triggers=default