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