From 13bdae7c19c9af4d586622a10c4edf8a31fe8580 Mon Sep 17 00:00:00 2001 From: Jacobus Martinus Kruithof Date: Thu, 16 Nov 2006 16:08:45 +0000 Subject: [PATCH] Added logging of stacktraces of exceptions in logmessage when loglevel = debug. Made first use of this logging in the Delete task. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@475788 13f79535-47bb-0310-9956-ffa450edef68 --- src/main/org/apache/tools/ant/BuildEvent.java | 10 +- .../org/apache/tools/ant/BuildListener.java | 1 + .../org/apache/tools/ant/DefaultLogger.java | 4 + src/main/org/apache/tools/ant/Project.java | 94 +++++++++++++++++-- src/main/org/apache/tools/ant/Task.java | 32 +++++++ src/main/org/apache/tools/ant/XmlLogger.java | 7 ++ .../org/apache/tools/ant/taskdefs/Delete.java | 2 +- 7 files changed, 139 insertions(+), 11 deletions(-) diff --git a/src/main/org/apache/tools/ant/BuildEvent.java b/src/main/org/apache/tools/ant/BuildEvent.java index 92697bcea..7437394e5 100644 --- a/src/main/org/apache/tools/ant/BuildEvent.java +++ b/src/main/org/apache/tools/ant/BuildEvent.java @@ -46,7 +46,7 @@ public class BuildEvent extends EventObject { private int priority = Project.MSG_VERBOSE; /** * The exception associated with this event, if any. - * This is only used for "taskFinished", "targetFinished", + * This is only used for "messageLogged", "taskFinished", "targetFinished", * and "buildFinished" events. */ private Throwable exception; @@ -112,12 +112,13 @@ public class BuildEvent extends EventObject { /** * Sets the exception associated with this event. This is used - * for "taskFinished", "targetFinished", and "buildFinished" + * for "messageLogged", "taskFinished", "targetFinished", and "buildFinished" * events. * * @param exception The exception to be associated with this event. * May be null. * + * @see BuildListener#messageLogged(BuildEvent) * @see BuildListener#taskFinished(BuildEvent) * @see BuildListener#targetFinished(BuildEvent) * @see BuildListener#buildFinished(BuildEvent) @@ -183,12 +184,13 @@ public class BuildEvent extends EventObject { /** * Returns the exception that was thrown, if any. This field will only - * be set for "taskFinished", "targetFinished", and "buildFinished" + * be set for "messageLogged", "taskFinished", "targetFinished", and "buildFinished" * events. * * @return the exception associated with this exception, or * null if no exception has been set. - * + * + * @see BuildListener#messageLogged(BuildEvent) * @see BuildListener#taskFinished(BuildEvent) * @see BuildListener#targetFinished(BuildEvent) * @see BuildListener#buildFinished(BuildEvent) diff --git a/src/main/org/apache/tools/ant/BuildListener.java b/src/main/org/apache/tools/ant/BuildListener.java index 0732d9503..5801c980a 100644 --- a/src/main/org/apache/tools/ant/BuildListener.java +++ b/src/main/org/apache/tools/ant/BuildListener.java @@ -99,6 +99,7 @@ public interface BuildListener extends EventListener { * Must not be null. * * @see BuildEvent#getMessage() + * @see BuildEvent#getException() * @see BuildEvent#getPriority() */ void messageLogged(BuildEvent event); diff --git a/src/main/org/apache/tools/ant/DefaultLogger.java b/src/main/org/apache/tools/ant/DefaultLogger.java index 1ecf26f51..7a0571259 100644 --- a/src/main/org/apache/tools/ant/DefaultLogger.java +++ b/src/main/org/apache/tools/ant/DefaultLogger.java @@ -277,6 +277,10 @@ public class DefaultLogger implements BuildLogger { } else { message.append(event.getMessage()); } + Throwable ex = event.getException(); + if (Project.MSG_DEBUG <= msgOutputLevel && ex != null) { + message.append(StringUtils.getStackTrace(ex)); + } String msg = message.toString(); if (priority != Project.MSG_ERR) { diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index f86b8ae6c..c6d2f5895 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -425,9 +425,19 @@ public class Project implements ResourceFactory { * @param msgLevel The log priority level to use. */ public void log(String message, int msgLevel) { - fireMessageLogged(this, message, msgLevel); + log(message, null, msgLevel); } + /** + * Write a project level message to the log with the given log level. + * @param message The text to log. Should not be null. + * @param throwable The exception causing this log, may be null. + * @param msgLevel The log priority level to use. + */ + public void log(String message, Throwable throwable, int msgLevel) { + fireMessageLogged(this, message, throwable, msgLevel); + } + /** * Write a task level message to the log with the given log level. * @param task The task to use in the log. Must not be null. @@ -435,9 +445,20 @@ public class Project implements ResourceFactory { * @param msgLevel The log priority level to use. */ public void log(Task task, String message, int msgLevel) { - fireMessageLogged(task, message, msgLevel); + fireMessageLogged(task, message, null, msgLevel); } + /** + * Write a task level message to the log with the given log level. + * @param task The task to use in the log. Must not be null. + * @param message The text to log. Should not be null. + * @param throwable The exception causing this log, may be null. + * @param msgLevel The log priority level to use. + */ + public void log(Task task, String message, Throwable throwable, int msgLevel) { + fireMessageLogged(task, message, throwable, msgLevel); + } + /** * Write a target level message to the log with the given log level. * @param target The target to use in the log. @@ -446,9 +467,22 @@ public class Project implements ResourceFactory { * @param msgLevel The log priority level to use. */ public void log(Target target, String message, int msgLevel) { - fireMessageLogged(target, message, msgLevel); + log(target, message, null, msgLevel); } + /** + * Write a target level message to the log with the given log level. + * @param target The target to use in the log. + * Must not be null. + * @param message The text to log. Should not be null. + * @param throwable The exception causing this log, may be null. + * @param msgLevel The log priority level to use. + */ + public void log(Target target, String message, Throwable throwable, + int msgLevel) { + fireMessageLogged(target, message, throwable, msgLevel); + } + /** * Return the set of global filters. * @@ -2150,10 +2184,26 @@ public class Project implements ResourceFactory { */ protected void fireMessageLogged(Project project, String message, int priority) { + fireMessageLogged(project, message, priority); + } + + /** + * Send a "message logged" project level event + * to the build listeners for this project. + * + * @param project The project generating the event. + * Should not be null. + * @param message The message to send. Should not be null. + * @param throwable The exception that caused this message. May be null. + * @param priority The priority of the message. + */ + protected void fireMessageLogged(Project project, String message, + Throwable throwable, int priority) { BuildEvent event = new BuildEvent(project); + event.setException(throwable); fireMessageLoggedEvent(event, message, priority); } - + /** * Send a "message logged" target level event * to the build listeners for this project. @@ -2165,10 +2215,26 @@ public class Project implements ResourceFactory { */ protected void fireMessageLogged(Target target, String message, int priority) { + fireMessageLogged(target, message, null, priority); + } + + /** + * Send a "message logged" target level event + * to the build listeners for this project. + * + * @param target The target generating the event. + * Must not be null. + * @param message The message to send. Should not be null. + * @param throwable The exception that caused this message. May be null. + * @param priority The priority of the message. + */ + protected void fireMessageLogged(Target target, String message, + Throwable throwable, int priority) { BuildEvent event = new BuildEvent(target); + event.setException(throwable); fireMessageLoggedEvent(event, message, priority); } - + /** * Send a "message logged" task level event * to the build listeners for this project. @@ -2179,10 +2245,26 @@ public class Project implements ResourceFactory { * @param priority The priority of the message. */ protected void fireMessageLogged(Task task, String message, int priority) { + fireMessageLogged(task, message, null, priority); + } + + /** + * Send a "message logged" task level event + * to the build listeners for this project. + * + * @param task The task generating the event. + * Must not be null. + * @param message The message to send. Should not be null. + * @param throwable The exception that caused this message. May be null. + * @param priority The priority of the message. + */ + protected void fireMessageLogged(Task task, String message, + Throwable throwable, int priority) { BuildEvent event = new BuildEvent(task); + event.setException(throwable); fireMessageLoggedEvent(event, message, priority); } - + /** * Register a task as the current task for a thread. * If the task is null, the thread's entry is removed. diff --git a/src/main/org/apache/tools/ant/Task.java b/src/main/org/apache/tools/ant/Task.java index f455fcd46..4a666cfc1 100644 --- a/src/main/org/apache/tools/ant/Task.java +++ b/src/main/org/apache/tools/ant/Task.java @@ -327,6 +327,38 @@ public abstract class Task extends ProjectComponent { } } + /** + * Logs a message with the given priority. This delegates + * the actual logging to the project. + * + * @param t The exception to be logged. Should not be null. + * @param msgLevel The message priority at which this message is to + * be logged. + */ + public void log(Throwable t, int msgLevel) { + if(t != null) + { + log(t.getMessage(), t, msgLevel); + } + } + + /** + * Logs a message with the given priority. This delegates + * the actual logging to the project. + * + * @param msg The message to be logged. Should not be null. + * @param t The exception to be logged. May be null. + * @param msgLevel The message priority at which this message is to + * be logged. + */ + public void log(String msg, Throwable t, int msgLevel) { + if (getProject() != null) { + getProject().log(this, msg, t, msgLevel); + } else { + super.log(msg, msgLevel); + } + } + /** * Performs this task if it's still valid, or gets a replacement * version and performs that otherwise. diff --git a/src/main/org/apache/tools/ant/XmlLogger.java b/src/main/org/apache/tools/ant/XmlLogger.java index 68319c990..1381d4b1c 100644 --- a/src/main/org/apache/tools/ant/XmlLogger.java +++ b/src/main/org/apache/tools/ant/XmlLogger.java @@ -398,6 +398,13 @@ public class XmlLogger implements BuildLogger { } messageElement.setAttribute(PRIORITY_ATTR, name); + Throwable ex = event.getException(); + if (Project.MSG_DEBUG <= msgOutputLevel && ex != null) { + Text errText = doc.createCDATASection(StringUtils.getStackTrace(ex)); + Element stacktrace = doc.createElement(STACKTRACE_TAG); + stacktrace.appendChild(errText); + buildElement.element.appendChild(stacktrace); + } Text messageText = doc.createCDATASection(event.getMessage()); messageElement.appendChild(messageText); diff --git a/src/main/org/apache/tools/ant/taskdefs/Delete.java b/src/main/org/apache/tools/ant/taskdefs/Delete.java index 13f47be38..3286c2dee 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Delete.java +++ b/src/main/org/apache/tools/ant/taskdefs/Delete.java @@ -621,7 +621,7 @@ public class Delete extends MatchingTask { throw (e instanceof BuildException) ? (BuildException) e : new BuildException(e); } - log(e.getMessage(), quiet ? Project.MSG_VERBOSE : verbosity); + log(e, quiet ? Project.MSG_VERBOSE : verbosity); } /**