From 8b2d7026b2216c6a9200d22a9adfa6ad12b1762e Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 6 Sep 2000 11:34:41 +0000 Subject: [PATCH] If the output has not been redirected (via -logfile) write error messages to stderr instead of stdout. Suggested by: Peter Nordlund git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267968 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/BuildLogger.java | 10 ++++++- .../org/apache/tools/ant/DefaultLogger.java | 26 ++++++++++++++----- src/main/org/apache/tools/ant/Main.java | 5 ++++ .../org/apache/tools/ant/taskdefs/Ant.java | 1 + 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/main/org/apache/tools/ant/BuildLogger.java b/src/main/org/apache/tools/ant/BuildLogger.java index 0ac485b76..068c71e56 100644 --- a/src/main/org/apache/tools/ant/BuildLogger.java +++ b/src/main/org/apache/tools/ant/BuildLogger.java @@ -89,4 +89,12 @@ public interface BuildLogger extends BuildListener { * editors can parse files names, etc. */ public void setEmacsMode(boolean emacsMode); -} \ No newline at end of file + + /** + * Set the output stream to which this logger is to send error messages. + * + * @param err the error stream for the logger. + */ + public void setErrorPrintStream(PrintStream err); + +} diff --git a/src/main/org/apache/tools/ant/DefaultLogger.java b/src/main/org/apache/tools/ant/DefaultLogger.java index 3da5f2d93..9b2100b50 100644 --- a/src/main/org/apache/tools/ant/DefaultLogger.java +++ b/src/main/org/apache/tools/ant/DefaultLogger.java @@ -65,6 +65,7 @@ public class DefaultLogger implements BuildLogger { private static int LEFT_COLUMN_SIZE = 12; private PrintStream out; + private PrintStream err; private int msgOutputLevel; private long startTime = System.currentTimeMillis(); @@ -94,6 +95,15 @@ public class DefaultLogger implements BuildLogger { this.out = output; } + /** + * Set the output stream to which this logger is to send error messages. + * + * @param err the error stream for the logger. + */ + public void setErrorPrintStream(PrintStream err) { + this.err = err; + } + /** * Set this logger to produce emacs (and other editor) friendly output. * @@ -120,18 +130,18 @@ public class DefaultLogger implements BuildLogger { out.println(lSep + "BUILD SUCCESSFUL"); } else { - out.println(lSep + "BUILD FAILED" + lSep); + err.println(lSep + "BUILD FAILED" + lSep); if (error instanceof BuildException) { - out.println(error.toString()); + err.println(error.toString()); Throwable nested = ((BuildException)error).getException(); if (nested != null) { - nested.printStackTrace(out); + nested.printStackTrace(err); } } else { - error.printStackTrace(out); + error.printStackTrace(err); } } @@ -152,6 +162,8 @@ public class DefaultLogger implements BuildLogger { public void messageLogged(BuildEvent event) { + PrintStream logTo = event.getPriority() == Project.MSG_ERR ? err : out; + // Filter out messages based on priority if (event.getPriority() <= msgOutputLevel) { @@ -162,14 +174,14 @@ public class DefaultLogger implements BuildLogger { if (!emacsMode) { String msg = "[" + name + "] "; for (int i = 0; i < (LEFT_COLUMN_SIZE - msg.length()); i++) { - out.print(" "); + logTo.print(" "); } - out.print(msg); + logTo.print(msg); } } // Print the message - out.println(event.getMessage()); + logTo.println(event.getMessage()); } } diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index 2f03f624d..03f4a070c 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -81,6 +81,9 @@ public class Main { /** Stream that we are using for logging */ private PrintStream out = System.out; + /** Stream that we are using for logging error messages */ + private PrintStream err = System.err; + /** The build targets */ private Vector targets = new Vector(5); @@ -152,6 +155,7 @@ public class Main { File logFile = new File(args[i+1]); i++; out = new PrintStream(new FileOutputStream(logFile)); + err = out; System.setOut(out); System.setErr(out); } catch (IOException ioe) { @@ -367,6 +371,7 @@ public class Main { logger.setMessageOutputLevel(msgOutputLevel); logger.setOutputPrintStream(out); + logger.setErrorPrintStream(err); logger.setEmacsMode(emacsMode); return logger; diff --git a/src/main/org/apache/tools/ant/taskdefs/Ant.java b/src/main/org/apache/tools/ant/taskdefs/Ant.java index 7f312c524..5993d09b1 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Ant.java +++ b/src/main/org/apache/tools/ant/taskdefs/Ant.java @@ -123,6 +123,7 @@ public class Ant extends Task { DefaultLogger logger = new DefaultLogger(); logger.setMessageOutputLevel(Project.MSG_INFO); logger.setOutputPrintStream(out); + logger.setErrorPrintStream(out); p1.addBuildListener(logger); } catch( IOException ex ) {