From 38688e7583ed218343cc26e35591cf25966f2aa7 Mon Sep 17 00:00:00 2001 From: Nicolas Lalevee Date: Wed, 15 Aug 2012 11:13:15 +0000 Subject: [PATCH] Add a log mode which logs nothing but ant task output and build failures; useful for using ant output in scripts, like for cacti for instance git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1373334 13f79535-47bb-0310-9956-ffa450edef68 --- manual/running.html | 7 ++- src/main/org/apache/tools/ant/Main.java | 15 ++++- .../tools/ant/listener/SilentLogger.java | 56 +++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/main/org/apache/tools/ant/listener/SilentLogger.java diff --git a/manual/running.html b/manual/running.html index 68d57940b..e0f0669a6 100644 --- a/manual/running.html +++ b/manual/running.html @@ -77,8 +77,10 @@ You can also access environment variables using the which instructs Ant to print less information to the console; -verbose, which causes Ant to print -additional information to the console; and -debug, -which causes Ant to print considerably more additional information. +additional information to the console; -debug, +which causes Ant to print considerably more additional information; and +-silent which makes Ant print nothing but task +output and build failures (useful to capture Ant output by scripts).

It is also possible to specify one or more targets that should be executed. @@ -104,6 +106,7 @@ Options: -diagnostics print information that might be helpful to diagnose or report problems. -quiet, -q be extra quiet + -silent, -S print nothing but task outputs and build failures -verbose, -v be extra verbose -debug, -d print debugging information -emacs, -e produce logging information without adornments diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index fcdd1eb3e..fa826006a 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -36,6 +36,7 @@ import java.util.Vector; import org.apache.tools.ant.input.DefaultInputHandler; import org.apache.tools.ant.input.InputHandler; import org.apache.tools.ant.launch.AntMain; +import org.apache.tools.ant.listener.SilentLogger; import org.apache.tools.ant.property.GetProperty; import org.apache.tools.ant.property.ResolvePropertyMap; import org.apache.tools.ant.util.ClasspathUtils; @@ -121,6 +122,11 @@ public class Main implements AntMain { */ private boolean emacsMode = false; + /** + * Whether or not log output should be reduced to the minimum + */ + private boolean silent = false; + /** * Whether or not this instance has successfully been * constructed and is ready to run. @@ -336,6 +342,8 @@ public class Main implements AntMain { msgOutputLevel = Project.MSG_VERBOSE; } else if (arg.equals("-debug") || arg.equals("-d")) { msgOutputLevel = Project.MSG_DEBUG; + } else if (arg.equals("-silent") || arg.equals("-S")) { + silent = true; } else if (arg.equals("-noinput")) { allowInput = false; } else if (arg.equals("-logfile") || arg.equals("-l")) { @@ -921,7 +929,11 @@ public class Main implements AntMain { */ private BuildLogger createLogger() { BuildLogger logger = null; - if (loggerClassname != null) { + if (silent) { + logger = new SilentLogger(); + msgOutputLevel = Project.MSG_WARN; + emacsMode = true; + } else if (loggerClassname != null) { try { logger = (BuildLogger) ClasspathUtils.newInstance( loggerClassname, Main.class.getClassLoader(), @@ -958,6 +970,7 @@ public class Main implements AntMain { msg.append(" -diagnostics print information that might be helpful to" + lSep); msg.append(" diagnose or report problems." + lSep); msg.append(" -quiet, -q be extra quiet" + lSep); + msg.append(" -silent, -S print nothing but task outputs and build failures" + lSep); msg.append(" -verbose, -v be extra verbose" + lSep); msg.append(" -debug, -d print debugging information" + lSep); msg.append(" -emacs, -e produce logging information without adornments" diff --git a/src/main/org/apache/tools/ant/listener/SilentLogger.java b/src/main/org/apache/tools/ant/listener/SilentLogger.java new file mode 100644 index 000000000..49da6e663 --- /dev/null +++ b/src/main/org/apache/tools/ant/listener/SilentLogger.java @@ -0,0 +1,56 @@ +/* + * 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. + * + */ +package org.apache.tools.ant.listener; + +import org.apache.tools.ant.BuildEvent; +import org.apache.tools.ant.DefaultLogger; + +/** + * A logger which logs nothing but build failure and what task might output + * + * @since 1.9.0 + */ +public class SilentLogger extends DefaultLogger { + + public void buildStarted(BuildEvent event) { + // log nothing + } + + public void buildFinished(BuildEvent event) { + if (event.getException() != null) { + super.buildFinished(event); + } + } + + public void targetStarted(BuildEvent event) { + // log nothing + } + + public void targetFinished(BuildEvent event) { + // log nothing + } + + public void taskStarted(BuildEvent event) { + // log nothing + } + + public void taskFinished(BuildEvent event) { + // log nothing + } + +}