You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

XmlLogger.java 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant;
  55. import java.io.*;
  56. import java.util.*;
  57. import javax.xml.parsers.*;
  58. import org.w3c.dom.*;
  59. import org.apache.tools.ant.util.DOMElementWriter;
  60. /**
  61. * Generates a "log.xml" file in the current directory with
  62. * an XML description of what happened during a build.
  63. *
  64. * @see Project#addBuildListener(BuildListener)
  65. */
  66. public class XmlLogger implements BuildListener {
  67. private static final DocumentBuilder builder = getDocumentBuilder();
  68. private static DocumentBuilder getDocumentBuilder() {
  69. try {
  70. return DocumentBuilderFactory.newInstance().newDocumentBuilder();
  71. }
  72. catch(Exception exc) {
  73. throw new ExceptionInInitializerError(exc);
  74. }
  75. }
  76. // XML constants for tag names and attribute names
  77. private static final String BUILD_TAG = "build";
  78. private static final String TARGET_TAG = "target";
  79. private static final String TASK_TAG = "task";
  80. private static final String MESSAGE_TAG = "message";
  81. private static final String NAME_ATTR = "name";
  82. private static final String TIME_ATTR = "time";
  83. private static final String PRIORITY_ATTR = "priority";
  84. private static final String LOCATION_ATTR = "location";
  85. private static final String ERROR_ATTR = "error";
  86. private Document doc;
  87. private Hashtable tasks = new Hashtable();
  88. private Hashtable targets = new Hashtable();
  89. private Hashtable threadStacks = new Hashtable();
  90. private TimedElement buildElement = null;
  91. static private class TimedElement {
  92. long startTime;
  93. Element element;
  94. }
  95. /**
  96. * Constructs a new BuildListener that logs build events to an XML file.
  97. */
  98. public XmlLogger() {
  99. }
  100. public void buildStarted(BuildEvent event) {
  101. buildElement = new TimedElement();
  102. buildElement.startTime = System.currentTimeMillis();
  103. doc = builder.newDocument();
  104. buildElement.element = doc.createElement(BUILD_TAG);
  105. }
  106. public void buildFinished(BuildEvent event) {
  107. long totalTime = System.currentTimeMillis() - buildElement.startTime;
  108. buildElement.element.setAttribute(TIME_ATTR, DefaultLogger.formatTime(totalTime));
  109. if (event.getException() != null) {
  110. buildElement.element.setAttribute(ERROR_ATTR, event.getException().toString());
  111. }
  112. try {
  113. String outFilename =
  114. event.getProject().getProperty("XmlLogger.file");
  115. if (outFilename == null) {
  116. outFilename = "log.xml";
  117. }
  118. // specify output in UTF8 otherwise accented characters will blow
  119. // up everything
  120. Writer out =
  121. new OutputStreamWriter(new FileOutputStream(outFilename),
  122. "UTF8");
  123. out.write("<?xml:stylesheet type=\"text/xsl\" href=\"log.xsl\"?>\n\n");
  124. (new DOMElementWriter()).write(buildElement.element, out, 0, "\t");
  125. out.flush();
  126. out.close();
  127. } catch(IOException exc) {
  128. throw new BuildException("Unable to close log file", exc);
  129. }
  130. buildElement = null;
  131. }
  132. private Stack getStack() {
  133. Stack threadStack = (Stack)threadStacks.get(Thread.currentThread());
  134. if (threadStack == null) {
  135. threadStack = new Stack();
  136. threadStacks.put(Thread.currentThread(), threadStack);
  137. }
  138. return threadStack;
  139. }
  140. public void targetStarted(BuildEvent event) {
  141. Target target = event.getTarget();
  142. TimedElement targetElement = new TimedElement();
  143. targetElement.startTime = System.currentTimeMillis();
  144. targetElement.element = doc.createElement(TARGET_TAG);
  145. targetElement.element.setAttribute(NAME_ATTR, target.getName());
  146. targets.put(target, targetElement);
  147. getStack().push(targetElement);
  148. }
  149. public void targetFinished(BuildEvent event) {
  150. Target target = event.getTarget();
  151. TimedElement targetElement = (TimedElement)targets.get(target);
  152. if (targetElement != null) {
  153. long totalTime = System.currentTimeMillis() - targetElement.startTime;
  154. targetElement.element.setAttribute(TIME_ATTR, DefaultLogger.formatTime(totalTime));
  155. TimedElement parentElement = null;
  156. Stack threadStack = getStack();
  157. if (!threadStack.empty()) {
  158. TimedElement poppedStack = (TimedElement)threadStack.pop();
  159. if (poppedStack != targetElement) {
  160. throw new RuntimeException("Mismatch - popped element = " + poppedStack.element +
  161. " finished task element = " + targetElement.element);
  162. }
  163. if (!threadStack.empty()) {
  164. parentElement = (TimedElement)threadStack.peek();
  165. }
  166. }
  167. if (parentElement == null) {
  168. buildElement.element.appendChild(targetElement.element);
  169. }
  170. else {
  171. parentElement.element.appendChild(targetElement.element);
  172. }
  173. }
  174. }
  175. public void taskStarted(BuildEvent event) {
  176. Task task = event.getTask();
  177. TimedElement taskElement = new TimedElement();
  178. taskElement.startTime = System.currentTimeMillis();
  179. taskElement.element = doc.createElement(TASK_TAG);
  180. String name = task.getClass().getName();
  181. int pos = name.lastIndexOf(".");
  182. if (pos != -1) {
  183. name = name.substring(pos + 1);
  184. }
  185. taskElement.element.setAttribute(NAME_ATTR, name);
  186. taskElement.element.setAttribute(LOCATION_ATTR, event.getTask().getLocation().toString());
  187. tasks.put(task, taskElement);
  188. getStack().push(taskElement);
  189. }
  190. public void taskFinished(BuildEvent event) {
  191. Task task = event.getTask();
  192. TimedElement taskElement = (TimedElement)tasks.get(task);
  193. if (taskElement != null) {
  194. long totalTime = System.currentTimeMillis() - taskElement.startTime;
  195. taskElement.element.setAttribute(TIME_ATTR, DefaultLogger.formatTime(totalTime));
  196. Target target = task.getOwningTarget();
  197. TimedElement targetElement = (TimedElement)targets.get(target);
  198. if (targetElement == null) {
  199. buildElement.element.appendChild(taskElement.element);
  200. }
  201. else {
  202. targetElement.element.appendChild(taskElement.element);
  203. }
  204. Stack threadStack = getStack();
  205. if (!threadStack.empty()) {
  206. TimedElement poppedStack = (TimedElement)threadStack.pop();
  207. if (poppedStack != taskElement) {
  208. throw new RuntimeException("Mismatch - popped element = " + poppedStack.element +
  209. " finished task element = " + taskElement.element);
  210. }
  211. }
  212. }
  213. }
  214. public void messageLogged(BuildEvent event) {
  215. Element messageElement = doc.createElement(MESSAGE_TAG);
  216. String name = "debug";
  217. switch(event.getPriority()) {
  218. case Project.MSG_ERR: name = "error"; break;
  219. case Project.MSG_WARN: name = "warn"; break;
  220. case Project.MSG_INFO: name = "info"; break;
  221. default: name = "debug"; break;
  222. }
  223. messageElement.setAttribute(PRIORITY_ATTR, name);
  224. Text messageText = doc.createCDATASection(event.getMessage());
  225. messageElement.appendChild(messageText);
  226. TimedElement parentElement = null;
  227. Task task = event.getTask();
  228. Target target = event.getTarget();
  229. if (task != null) {
  230. parentElement = (TimedElement)tasks.get(task);
  231. }
  232. if (parentElement == null && target != null) {
  233. parentElement = (TimedElement)targets.get(target);
  234. }
  235. if (parentElement == null) {
  236. Stack threadStack = (Stack)threadStacks.get(Thread.currentThread());
  237. if (threadStack != null) {
  238. if (!threadStack.empty()) {
  239. parentElement = (TimedElement)threadStack.peek();
  240. }
  241. }
  242. }
  243. if (parentElement != null) {
  244. parentElement.element.appendChild(messageElement);
  245. }
  246. else {
  247. buildElement.element.appendChild(messageElement);
  248. }
  249. }
  250. }