Top Description Inners Fields Constructors Methods
org.apache.tools.ant

public Class XmlLogger

extends Object
implements BuildLogger
Class Inheritance
All Implemented Interfaces
org.apache.tools.ant.BuildLogger, org.apache.tools.ant.BuildListener, java.util.EventListener
Imports
java.io.IOException, .OutputStream, .OutputStreamWriter, .PrintStream, .Writer, java.nio.charset.StandardCharsets, java.nio.file.Files, .Paths, java.util.HashSet, .Hashtable, .Map, .Set, .Stack, javax.xml.parsers.DocumentBuilder, .DocumentBuilderFactory, org.apache.tools.ant.util.DOMElementWriter, .StringUtils, org.w3c.dom.Document, .Element, .Node, .Text

Generates a file in the current directory with an XML description of what happened during a build. The default filename is "log.xml", but this can be overridden with the property XmlLogger.file. This implementation assumes in its sanity checking that only one thread runs a particular target/task at a time. This is enforced by the way that parallel builds and antcalls are done - and indeed all but the simplest of tasks could run into problems if executed in parallel.
See Also
Project#addBuildListener(BuildListener)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
XmlLogger.TimedElement

Utility class representing the time an element started.

Field Summary

Modifier and TypeField and Description
private static final String
BUILD_TAG

XML element name for a build.

private XmlLogger.TimedElement
buildElement

When the build started.

private static DocumentBuilder
builder

DocumentBuilder to use when creating the document to start with.

private Document
doc

The complete log document for this build.

private static final String
ERROR_ATTR

XML attribute name for an error description.

private static final String
LOCATION_ATTR

XML attribute name for a file location.

private static final String
MESSAGE_TAG

XML element name for a message.

private int
private static final String
NAME_ATTR

XML attribute name for a name.

private PrintStream
private static final String
PRIORITY_ATTR

XML attribute name for a message priority.

private static final String
STACKTRACE_TAG

XML element name for a stack trace.

private static final String
TARGET_TAG

XML element name for a target.

private Map<Target, XmlLogger.TimedElement>
targets

Mapping for when targets started (Target to TimedElement).

private static final String
TASK_TAG

XML element name for a task.

private Map<Task, XmlLogger.TimedElement>
tasks

Mapping for when tasks started (Task to TimedElement).

private Map<Thread, Stack<XmlLogger.TimedElement>>
threadStacks

Mapping of threads to stacks of elements (Thread to Stack of TimedElement).

private static final String
TIME_ATTR

XML attribute name for a time.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
buildFinished(BuildEvent
An event with any relevant extra information. Will not be null.
event
)

Implements org.apache.tools.ant.BuildListener.buildFinished.

Fired when the build finishes, this adds the time taken and any error stacktrace to the build element and writes the document to disk.
public void
buildStarted(BuildEvent
Ignored.
event
)

Implements org.apache.tools.ant.BuildListener.buildStarted.

Fired when the build starts, this builds the top-level element for the document and remembers the time of the start of the build.
private static DocumentBuilder

Returns:

a default DocumentBuilder instance.
getDocumentBuilder
()

Returns a default DocumentBuilder instance or throws an ExceptionInInitializerError if it can't be created.

public int
private String
getProperty(BuildEvent event, String propertyName, String defaultValue)

private Stack<XmlLogger.TimedElement>

Returns:

the stack of timed elements for the current thread
getStack
()

Returns the stack of timed elements for the current thread.

private XmlLogger.TimedElement
getTaskElement(Task task)

Get the TimedElement associated with a task.

public void
messageLogged(BuildEvent
An event with any relevant extra information. Will not be null.
event
)

Implements org.apache.tools.ant.BuildListener.messageLogged.

Fired when a message is logged, this adds a message element to the most appropriate parent element (task, target or build) and records the priority and text of the message.
public void
setEmacsMode(boolean
true if logger should produce emacs compatible output
emacsMode
)

Implements org.apache.tools.ant.BuildLogger.setEmacsMode.

Ignore emacs mode, as it has no meaning in XML format
public void
setErrorPrintStream(PrintStream
the stream we are going to ignore.
err
)

Implements org.apache.tools.ant.BuildLogger.setErrorPrintStream.

Ignore error print stream.
public void
setMessageOutputLevel(int
the logging level - see Project class for level definitions
level
)

Implements org.apache.tools.ant.BuildLogger.setMessageOutputLevel.

Set the logging level when using this as a Logger
public void
setOutputPrintStream(PrintStream
the output PrintStream.
output
)

Implements org.apache.tools.ant.BuildLogger.setOutputPrintStream.

Set the output stream to which logging output is sent when operating as a logger.
private void
synchronizedAppend(Node parent, Node child)

public void
targetFinished(BuildEvent
An event with any relevant extra information. Will not be null.
event
)

Implements org.apache.tools.ant.BuildListener.targetFinished.

Fired when a target finishes building, this adds the time taken and any error stacktrace to the appropriate target element in the log.
public void
targetStarted(BuildEvent
An event with any relevant extra information. Will not be null.
event
)

Implements org.apache.tools.ant.BuildListener.targetStarted.

Fired when a target starts building, this pushes a timed element for the target onto the stack of elements for the current thread, remembering the current time and the name of the target.
public void
taskFinished(BuildEvent
An event with any relevant extra information. Will not be null.
event
)

Implements org.apache.tools.ant.BuildListener.taskFinished.

Fired when a task finishes building, this adds the time taken and any error stacktrace to the appropriate task element in the log.
public void
taskStarted(BuildEvent
An event with any relevant extra information. Will not be null.
event
)

Implements org.apache.tools.ant.BuildListener.taskStarted.

Fired when a task starts building, this pushes a timed element for the task onto the stack of elements for the current thread, remembering the current time and the name of the task.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BUILD_TAGback to summary
private static final String BUILD_TAG

XML element name for a build.

buildElementback to summary
private XmlLogger.TimedElement buildElement

When the build started.

builderback to summary
private static DocumentBuilder builder

DocumentBuilder to use when creating the document to start with.

docback to summary
private Document doc

The complete log document for this build.

ERROR_ATTRback to summary
private static final String ERROR_ATTR

XML attribute name for an error description.

LOCATION_ATTRback to summary
private static final String LOCATION_ATTR

XML attribute name for a file location.

MESSAGE_TAGback to summary
private static final String MESSAGE_TAG

XML element name for a message.

msgOutputLevelback to summary
private int msgOutputLevel
NAME_ATTRback to summary
private static final String NAME_ATTR

XML attribute name for a name.

outStreamback to summary
private PrintStream outStream
PRIORITY_ATTRback to summary
private static final String PRIORITY_ATTR

XML attribute name for a message priority.

STACKTRACE_TAGback to summary
private static final String STACKTRACE_TAG

XML element name for a stack trace.

TARGET_TAGback to summary
private static final String TARGET_TAG

XML element name for a target.

targetsback to summary
private Map<Target, XmlLogger.TimedElement> targets

Mapping for when targets started (Target to TimedElement).

TASK_TAGback to summary
private static final String TASK_TAG

XML element name for a task.

tasksback to summary
private Map<Task, XmlLogger.TimedElement> tasks

Mapping for when tasks started (Task to TimedElement).

threadStacksback to summary
private Map<Thread, Stack<XmlLogger.TimedElement>> threadStacks

Mapping of threads to stacks of elements (Thread to Stack of TimedElement).

TIME_ATTRback to summary
private static final String TIME_ATTR

XML attribute name for a time.

Constructor Detail

XmlLoggerback to summary
public XmlLogger()

Method Detail

buildFinishedback to summary
public void buildFinished(BuildEvent event)

Implements org.apache.tools.ant.BuildListener.buildFinished.

Fired when the build finishes, this adds the time taken and any error stacktrace to the build element and writes the document to disk.

Parameters
event:BuildEvent

An event with any relevant extra information. Will not be null.

Annotations
@Override
buildStartedback to summary
public void buildStarted(BuildEvent event)

Implements org.apache.tools.ant.BuildListener.buildStarted.

Fired when the build starts, this builds the top-level element for the document and remembers the time of the start of the build.

Parameters
event:BuildEvent

Ignored.

Annotations
@Override
getDocumentBuilderback to summary
private static DocumentBuilder getDocumentBuilder()

Returns a default DocumentBuilder instance or throws an ExceptionInInitializerError if it can't be created.

Returns:DocumentBuilder

a default DocumentBuilder instance.

getMessageOutputLevelback to summary
public int getMessageOutputLevel()

Overrides default org.apache.tools.ant.BuildLogger.getMessageOutputLevel.

Returns:int

Doc from org.apache.tools.ant.BuildLogger.getMessageOutputLevel.

Returns the currently set message output level. The default implementation of this method returns MSG_INFO.

Annotations
@Override
getPropertyback to summary
private String getProperty(BuildEvent event, String propertyName, String defaultValue)
getStackback to summary
private Stack<XmlLogger.TimedElement> getStack()

Returns the stack of timed elements for the current thread.

Returns:Stack<XmlLogger.TimedElement>

the stack of timed elements for the current thread

getTaskElementback to summary
private XmlLogger.TimedElement getTaskElement(Task task)

Get the TimedElement associated with a task. Where the task is not found directly, search for unknown elements which may be hiding the real task

messageLoggedback to summary
public void messageLogged(BuildEvent event)

Implements org.apache.tools.ant.BuildListener.messageLogged.

Fired when a message is logged, this adds a message element to the most appropriate parent element (task, target or build) and records the priority and text of the message.

Parameters
event:BuildEvent

An event with any relevant extra information. Will not be null.

Annotations
@Override
setEmacsModeback to summary
public void setEmacsMode(boolean emacsMode)

Implements org.apache.tools.ant.BuildLogger.setEmacsMode.

Ignore emacs mode, as it has no meaning in XML format

Parameters
emacsMode:boolean

true if logger should produce emacs compatible output

Annotations
@Override
setErrorPrintStreamback to summary
public void setErrorPrintStream(PrintStream err)

Implements org.apache.tools.ant.BuildLogger.setErrorPrintStream.

Ignore error print stream. All output will be written to either the XML log file or the PrintStream provided to setOutputPrintStream

Parameters
err:PrintStream

the stream we are going to ignore.

Annotations
@Override
setMessageOutputLevelback to summary
public void setMessageOutputLevel(int level)

Implements org.apache.tools.ant.BuildLogger.setMessageOutputLevel.

Set the logging level when using this as a Logger

Parameters
level:int

the logging level - see Project class for level definitions

Annotations
@Override
setOutputPrintStreamback to summary
public void setOutputPrintStream(PrintStream output)

Implements org.apache.tools.ant.BuildLogger.setOutputPrintStream.

Set the output stream to which logging output is sent when operating as a logger.

Parameters
output:PrintStream

the output PrintStream.

Annotations
@Override
synchronizedAppendback to summary
private void synchronizedAppend(Node parent, Node child)
targetFinishedback to summary
public void targetFinished(BuildEvent event)

Implements org.apache.tools.ant.BuildListener.targetFinished.

Fired when a target finishes building, this adds the time taken and any error stacktrace to the appropriate target element in the log.

Parameters
event:BuildEvent

An event with any relevant extra information. Will not be null.

Annotations
@Override
targetStartedback to summary
public void targetStarted(BuildEvent event)

Implements org.apache.tools.ant.BuildListener.targetStarted.

Fired when a target starts building, this pushes a timed element for the target onto the stack of elements for the current thread, remembering the current time and the name of the target.

Parameters
event:BuildEvent

An event with any relevant extra information. Will not be null.

Annotations
@Override
taskFinishedback to summary
public void taskFinished(BuildEvent event)

Implements org.apache.tools.ant.BuildListener.taskFinished.

Fired when a task finishes building, this adds the time taken and any error stacktrace to the appropriate task element in the log.

Parameters
event:BuildEvent

An event with any relevant extra information. Will not be null.

Annotations
@Override
taskStartedback to summary
public void taskStarted(BuildEvent event)

Implements org.apache.tools.ant.BuildListener.taskStarted.

Fired when a task starts building, this pushes a timed element for the task onto the stack of elements for the current thread, remembering the current time and the name of the task.

Parameters
event:BuildEvent

An event with any relevant extra information. Will not be null.

Annotations
@Override
org.apache.tools.ant back to summary

private Class XmlLogger.TimedElement

extends Object
Class Inheritance

Utility class representing the time an element started.

Field Summary

Modifier and TypeField and Description
private Element
element

Element created at the start time.

private long
startTime

Start time in milliseconds (as returned by System.currentTimeMillis()).

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

elementback to summary
private Element element

Element created at the start time.

startTimeback to summary
private long startTime

Start time in milliseconds (as returned by System.currentTimeMillis()).

Constructor Detail

TimedElementback to summary
private TimedElement()

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override