587
|
1 |
/*
|
628
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
587
|
18 |
package com.nokia.ant;
|
|
19 |
|
|
20 |
import java.io.FileOutputStream;
|
|
21 |
import java.io.IOException;
|
|
22 |
import java.io.OutputStream;
|
|
23 |
import java.io.OutputStreamWriter;
|
|
24 |
import java.io.PrintStream;
|
|
25 |
import java.io.Writer;
|
628
|
26 |
import java.util.Enumeration;
|
587
|
27 |
import java.util.Hashtable;
|
|
28 |
import java.util.Stack;
|
628
|
29 |
|
587
|
30 |
import javax.xml.parsers.DocumentBuilder;
|
|
31 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
32 |
import javax.xml.parsers.ParserConfigurationException;
|
628
|
33 |
|
|
34 |
import org.apache.tools.ant.BuildEvent;
|
|
35 |
import org.apache.tools.ant.BuildException;
|
|
36 |
import org.apache.tools.ant.BuildLogger;
|
|
37 |
import org.apache.tools.ant.Project;
|
|
38 |
import org.apache.tools.ant.Target;
|
|
39 |
import org.apache.tools.ant.Task;
|
|
40 |
import org.apache.tools.ant.UnknownElement;
|
587
|
41 |
import org.apache.tools.ant.util.DOMElementWriter;
|
628
|
42 |
import org.apache.tools.ant.util.DateUtils;
|
587
|
43 |
import org.apache.tools.ant.util.StringUtils;
|
|
44 |
import org.w3c.dom.Document;
|
|
45 |
import org.w3c.dom.Element;
|
|
46 |
import org.w3c.dom.Text;
|
|
47 |
|
|
48 |
/**
|
628
|
49 |
* Generates a file in the current directory with an XML description of what happened during a
|
|
50 |
* build. The default filename is "log.xml", but this can be overridden with the property
|
|
51 |
* <code>XmlLogger.file</code>.
|
|
52 |
*
|
|
53 |
* This implementation assumes in its sanity checking that only one thread runs a particular
|
|
54 |
* target/task at a time. This is enforced by the way that parallel builds and antcalls are done -
|
|
55 |
* and indeed all but the simplest of tasks could run into problems if executed in parallel.
|
587
|
56 |
*/
|
|
57 |
public class XmlLogger implements BuildLogger {
|
|
58 |
|
|
59 |
/** XML element name for a build. */
|
|
60 |
private static final String BUILD_TAG = "build";
|
|
61 |
/** XML element name for a message. */
|
|
62 |
private static final String MESSAGE_TAG = "message";
|
|
63 |
/** XML attribute name for a time. */
|
|
64 |
private static final String TIME_ATTR = "time";
|
|
65 |
/** XML attribute name for a message priority. */
|
|
66 |
private static final String PRIORITY_ATTR = "priority";
|
|
67 |
/** XML attribute name for an error description. */
|
|
68 |
private static final String ERROR_ATTR = "error";
|
|
69 |
/** XML element name for a stack trace. */
|
|
70 |
private static final String STACKTRACE_TAG = "stacktrace";
|
628
|
71 |
|
587
|
72 |
/** DocumentBuilder to use when creating the document to start with. */
|
|
73 |
private static DocumentBuilder builder = getDocumentBuilder();
|
628
|
74 |
|
587
|
75 |
private int msgOutputLevel = Project.MSG_ERR;
|
|
76 |
private PrintStream outStream;
|
628
|
77 |
|
587
|
78 |
/** The complete log document for this build. */
|
|
79 |
private Document doc = builder.newDocument();
|
|
80 |
/** Mapping for when tasks started (Task to TimedElement). */
|
|
81 |
private Hashtable tasks = new Hashtable();
|
|
82 |
/** Mapping for when targets started (Task to TimedElement). */
|
|
83 |
private Hashtable targets = new Hashtable();
|
|
84 |
/**
|
628
|
85 |
* Mapping of threads to stacks of elements (Thread to Stack of TimedElement).
|
587
|
86 |
*/
|
|
87 |
private Hashtable<Thread, Stack> threadStacks = new Hashtable<Thread, Stack>();
|
|
88 |
/**
|
|
89 |
* When the build started.
|
|
90 |
*/
|
|
91 |
private TimedElement buildElement;
|
628
|
92 |
|
587
|
93 |
/**
|
628
|
94 |
* Returns a default DocumentBuilder instance or throws an ExceptionInInitializerError if it
|
|
95 |
* can't be created.
|
|
96 |
*
|
587
|
97 |
* @return a default DocumentBuilder instance.
|
|
98 |
*/
|
|
99 |
private static DocumentBuilder getDocumentBuilder() {
|
|
100 |
try {
|
|
101 |
return DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
628
|
102 |
}
|
|
103 |
catch (ParserConfigurationException exc) {
|
587
|
104 |
throw new ExceptionInInitializerError(exc.getMessage());
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
/** Utility class representing the time an element started. */
|
|
109 |
private static class TimedElement {
|
|
110 |
/**
|
628
|
111 |
* Start time in milliseconds (as returned by <code>System.currentTimeMillis()</code>).
|
587
|
112 |
*/
|
|
113 |
private long startTime;
|
|
114 |
/** Element created at the start time. */
|
|
115 |
private Element element;
|
628
|
116 |
|
587
|
117 |
public String toString() {
|
|
118 |
return element.getTagName() + ":" + element.getAttribute("name");
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
/**
|
628
|
123 |
* Fired when the build starts, this builds the top-level element for the document and remembers
|
|
124 |
* the time of the start of the build.
|
|
125 |
*
|
587
|
126 |
* @param event Ignored.
|
|
127 |
*/
|
|
128 |
public void buildStarted(BuildEvent event) {
|
|
129 |
buildElement = new TimedElement();
|
|
130 |
buildElement.startTime = System.currentTimeMillis();
|
|
131 |
buildElement.element = doc.createElement(BUILD_TAG);
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
628
|
135 |
* Fired when the build finishes, this adds the time taken and any error stacktrace to the build
|
|
136 |
* element and writes the document to disk.
|
|
137 |
*
|
|
138 |
* @param event An event with any relevant extra information. Will not be <code>null</code>.
|
587
|
139 |
*/
|
|
140 |
public void buildFinished(BuildEvent event) {
|
|
141 |
long totalTime = System.currentTimeMillis() - buildElement.startTime;
|
628
|
142 |
buildElement.element.setAttribute(TIME_ATTR, DateUtils.formatElapsedTime(totalTime));
|
587
|
143 |
|
|
144 |
if (event.getException() != null) {
|
628
|
145 |
buildElement.element.setAttribute(ERROR_ATTR, event.getException().toString());
|
587
|
146 |
// print the stacktrace in the build file it is always useful...
|
|
147 |
// better have too much info than not enough.
|
588
|
148 |
Throwable exception = event.getException();
|
|
149 |
Text errText = doc.createCDATASection(StringUtils.getStackTrace(exception));
|
587
|
150 |
Element stacktrace = doc.createElement(STACKTRACE_TAG);
|
|
151 |
stacktrace.appendChild(errText);
|
|
152 |
buildElement.element.appendChild(stacktrace);
|
|
153 |
}
|
|
154 |
|
|
155 |
String outFilename = event.getProject().getProperty("XmlLogger.file");
|
|
156 |
if (outFilename == null) {
|
|
157 |
outFilename = "log.xml";
|
|
158 |
}
|
628
|
159 |
String xslUri = event.getProject().getProperty("ant.XmlLogger.stylesheet.uri");
|
587
|
160 |
if (xslUri == null) {
|
|
161 |
xslUri = "log.xsl";
|
|
162 |
}
|
|
163 |
Writer out = null;
|
|
164 |
try {
|
|
165 |
// specify output in UTF8 otherwise accented characters will blow
|
|
166 |
// up everything
|
|
167 |
OutputStream stream = outStream;
|
|
168 |
if (stream == null) {
|
|
169 |
stream = new FileOutputStream(outFilename);
|
|
170 |
}
|
|
171 |
out = new OutputStreamWriter(stream, "UTF8");
|
|
172 |
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
|
173 |
if (xslUri.length() > 0) {
|
628
|
174 |
out.write("<?xml-stylesheet type=\"text/xsl\" href=\"" + xslUri + "\"?>\n\n");
|
587
|
175 |
}
|
|
176 |
(new DOMElementWriter()).write(buildElement.element, out, 0, "\t");
|
|
177 |
out.flush();
|
628
|
178 |
}
|
|
179 |
catch (IOException exc) {
|
587
|
180 |
throw new BuildException("Unable to write log file" + exc.getMessage());
|
628
|
181 |
}
|
|
182 |
finally {
|
587
|
183 |
if (out != null) {
|
|
184 |
try {
|
|
185 |
out.close();
|
628
|
186 |
}
|
|
187 |
catch (IOException e) {
|
587
|
188 |
// We are Ignoring the errors as no need to fail the build.
|
|
189 |
event.getProject().log("Not able to close the file handler " + e.getMessage(), Project.MSG_WARN);
|
|
190 |
e = null; // ignore
|
|
191 |
}
|
|
192 |
}
|
|
193 |
}
|
|
194 |
buildElement = null;
|
|
195 |
}
|
|
196 |
|
|
197 |
/**
|
628
|
198 |
* Fired when a target starts building, this pushes a timed element for the target onto the
|
|
199 |
* stack of elements for the current thread, remembering the current time and the name of the
|
|
200 |
* target.
|
|
201 |
*
|
|
202 |
* @param event An event with any relevant extra information. Will not be <code>null</code>.
|
587
|
203 |
*/
|
|
204 |
public void targetStarted(BuildEvent event) {
|
|
205 |
}
|
|
206 |
|
|
207 |
/**
|
628
|
208 |
* Fired when a target finishes building, this adds the time taken and any error stacktrace to
|
|
209 |
* the appropriate target element in the log.
|
|
210 |
*
|
|
211 |
* @param event An event with any relevant extra information. Will not be <code>null</code>.
|
587
|
212 |
*/
|
|
213 |
public void targetFinished(BuildEvent event) {
|
|
214 |
}
|
|
215 |
|
|
216 |
/**
|
628
|
217 |
* Fired when a task starts building, this pushes a timed element for the task onto the stack of
|
|
218 |
* elements for the current thread, remembering the current time and the name of the task.
|
|
219 |
*
|
|
220 |
* @param event An event with any relevant extra information. Will not be <code>null</code>.
|
587
|
221 |
*/
|
|
222 |
public void taskStarted(BuildEvent event) {
|
|
223 |
}
|
|
224 |
|
|
225 |
/**
|
628
|
226 |
* Fired when a task finishes building, this adds the time taken and any error stacktrace to the
|
|
227 |
* appropriate task element in the log.
|
|
228 |
*
|
|
229 |
* @param event An event with any relevant extra information. Will not be <code>null</code>.
|
587
|
230 |
*/
|
|
231 |
public void taskFinished(BuildEvent event) {
|
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Get the TimedElement associated with a task.
|
628
|
236 |
*
|
|
237 |
* Where the task is not found directly, search for unknown elements which may be hiding the
|
|
238 |
* real task
|
587
|
239 |
*/
|
|
240 |
private TimedElement getTaskElement(Task task) {
|
|
241 |
TimedElement element = (TimedElement) tasks.get(task);
|
|
242 |
if (element != null) {
|
|
243 |
return element;
|
|
244 |
}
|
|
245 |
|
588
|
246 |
for (Enumeration taskEnum = tasks.keys(); taskEnum.hasMoreElements();) {
|
|
247 |
Task key = (Task) taskEnum.nextElement();
|
587
|
248 |
if (key instanceof UnknownElement) {
|
|
249 |
if (((UnknownElement) key).getTask() == task) {
|
|
250 |
return (TimedElement) tasks.get(key);
|
|
251 |
}
|
|
252 |
}
|
|
253 |
}
|
|
254 |
|
|
255 |
return null;
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
628
|
259 |
* Fired when a message is logged, this adds a message element to the most appropriate parent
|
|
260 |
* element (task, target or build) and records the priority and text of the message.
|
|
261 |
*
|
|
262 |
* @param event An event with any relevant extra information. Will not be <code>null</code>.
|
587
|
263 |
*/
|
|
264 |
public void messageLogged(BuildEvent event) {
|
|
265 |
int priority = event.getPriority();
|
|
266 |
if (priority > msgOutputLevel) {
|
|
267 |
return;
|
|
268 |
}
|
|
269 |
Element messageElement = doc.createElement(MESSAGE_TAG);
|
|
270 |
|
|
271 |
String name = "debug";
|
|
272 |
switch (event.getPriority()) {
|
|
273 |
case Project.MSG_ERR:
|
|
274 |
name = "error";
|
|
275 |
break;
|
|
276 |
case Project.MSG_WARN:
|
|
277 |
name = "warn";
|
|
278 |
break;
|
|
279 |
case Project.MSG_INFO:
|
|
280 |
name = "info";
|
|
281 |
break;
|
|
282 |
default:
|
|
283 |
name = "debug";
|
|
284 |
break;
|
|
285 |
}
|
|
286 |
messageElement.setAttribute(PRIORITY_ATTR, name);
|
|
287 |
|
|
288 |
Throwable ex = event.getException();
|
|
289 |
if (Project.MSG_DEBUG <= msgOutputLevel && ex != null) {
|
|
290 |
Text errText = doc.createCDATASection(StringUtils.getStackTrace(ex));
|
|
291 |
Element stacktrace = doc.createElement(STACKTRACE_TAG);
|
|
292 |
stacktrace.appendChild(errText);
|
|
293 |
buildElement.element.appendChild(stacktrace);
|
|
294 |
}
|
|
295 |
Text messageText = doc.createCDATASection(event.getMessage());
|
|
296 |
messageElement.appendChild(messageText);
|
|
297 |
|
|
298 |
TimedElement parentElement = null;
|
|
299 |
|
|
300 |
Task task = event.getTask();
|
|
301 |
|
|
302 |
Target target = event.getTarget();
|
|
303 |
if (task != null) {
|
|
304 |
parentElement = getTaskElement(task);
|
|
305 |
}
|
|
306 |
if (parentElement == null && target != null) {
|
|
307 |
parentElement = (TimedElement) targets.get(target);
|
|
308 |
}
|
|
309 |
|
|
310 |
/*
|
628
|
311 |
* if (parentElement == null) { Stack threadStack = (Stack)
|
|
312 |
* threadStacks.get(Thread.currentThread()); if (threadStack != null) { if
|
|
313 |
* (!threadStack.empty()) { parentElement = (TimedElement) threadStack.peek(); } } }
|
|
314 |
*/
|
587
|
315 |
|
|
316 |
if (parentElement != null) {
|
|
317 |
parentElement.element.appendChild(messageElement);
|
628
|
318 |
}
|
|
319 |
else {
|
587
|
320 |
buildElement.element.appendChild(messageElement);
|
|
321 |
}
|
|
322 |
}
|
|
323 |
|
|
324 |
// -------------------------------------------------- BuildLogger interface
|
|
325 |
|
|
326 |
/**
|
|
327 |
* Set the logging level when using this as a Logger
|
628
|
328 |
*
|
|
329 |
* @param level the logging level - see {@link org.apache.tools.ant.Project#MSG_ERR Project}
|
587
|
330 |
* class for level definitions
|
|
331 |
*/
|
|
332 |
public void setMessageOutputLevel(int level) {
|
|
333 |
msgOutputLevel = level;
|
|
334 |
}
|
|
335 |
|
|
336 |
/**
|
628
|
337 |
* Set the output stream to which logging output is sent when operating as a logger.
|
|
338 |
*
|
587
|
339 |
* @param output the output PrintStream.
|
|
340 |
*/
|
|
341 |
public void setOutputPrintStream(PrintStream output) {
|
|
342 |
this.outStream = new PrintStream(output, true);
|
|
343 |
}
|
|
344 |
|
|
345 |
/**
|
|
346 |
* Ignore emacs mode, as it has no meaning in XML format
|
628
|
347 |
*
|
|
348 |
* @param emacsMode true if logger should produce emacs compatible output
|
587
|
349 |
*/
|
|
350 |
public void setEmacsMode(boolean emacsMode) {
|
|
351 |
}
|
|
352 |
|
|
353 |
/**
|
628
|
354 |
* Ignore error print stream. All output will be written to either the XML log file or the
|
|
355 |
* PrintStream provided to setOutputPrintStream
|
|
356 |
*
|
587
|
357 |
* @param err the stream we are going to ignore.
|
|
358 |
*/
|
|
359 |
public void setErrorPrintStream(PrintStream err) {
|
|
360 |
}
|
|
361 |
|
|
362 |
}
|