예제 #1
0
 @Test
 public void testCtorWithTwoParameters() {
   final OutputStream infoStream = new ByteArrayOutputStream();
   final DefaultLogger dl = new DefaultLogger(infoStream, true);
   dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
   dl.auditFinished(new AuditEvent(6000, "myfile"));
 }
예제 #2
0
 /**
  * Fired when a task finishes building, this adds the time taken and any error stacktrace to the
  * appropriate task element in the log.
  *
  * @param event An event with any relevant extra information. Will not be <code>null</code>.
  */
 public void taskFinished(BuildEvent event) {
   Task task = event.getTask();
   TimedElement taskElement = (TimedElement) tasks.get(task);
   if (taskElement != null) {
     long totalTime = System.currentTimeMillis() - taskElement.startTime;
     taskElement.element.setAttribute(TIME_ATTR, DefaultLogger.formatTime(totalTime));
     Target target = task.getOwningTarget();
     TimedElement targetElement = null;
     if (target != null) {
       targetElement = (TimedElement) targets.get(target);
     }
     if (targetElement == null) {
       buildElement.element.appendChild(taskElement.element);
     } else {
       targetElement.element.appendChild(taskElement.element);
     }
     Stack threadStack = getStack();
     if (!threadStack.empty()) {
       TimedElement poppedStack = (TimedElement) threadStack.pop();
       if (poppedStack != taskElement) {
         throw new RuntimeException(
             "Mismatch - popped element = "
                 + poppedStack.element
                 + " finished task element = "
                 + taskElement.element);
       }
     }
   }
 }
예제 #3
0
  /**
   * Fired when the build finishes, this adds the time taken and any error stacktrace to the build
   * element and writes the document to disk.
   *
   * @param event An event with any relevant extra information. Will not be <code>null</code>.
   */
  public void buildFinished(BuildEvent event) {
    long totalTime = System.currentTimeMillis() - buildElement.startTime;
    buildElement.element.setAttribute(TIME_ATTR, DefaultLogger.formatTime(totalTime));

    if (event.getException() != null) {
      buildElement.element.setAttribute(ERROR_ATTR, event.getException().toString());
      // print the stacktrace in the build file it is always useful...
      // better have too much info than not enough.
      Throwable t = event.getException();
      Text errText = doc.createCDATASection(StringUtils.getStackTrace(t));
      Element stacktrace = doc.createElement(STACKTRACE_TAG);
      stacktrace.appendChild(errText);
      buildElement.element.appendChild(stacktrace);
    }

    String outFilename = event.getProject().getProperty("XmlLogger.file");
    if (outFilename == null) {
      outFilename = "log.xml";
    }
    String xslUri = event.getProject().getProperty("ant.XmlLogger.stylesheet.uri");
    if (xslUri == null) {
      xslUri = "log.xsl";
    }
    Writer out = null;
    try {
      // specify output in UTF8 otherwise accented characters will blow
      // up everything
      OutputStream stream = outStream;
      if (stream == null) {
        stream = new FileOutputStream(outFilename);
      }
      out = new OutputStreamWriter(stream, "UTF8");
      out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
      if (xslUri.length() > 0) {
        out.write("<?xml-stylesheet type=\"text/xsl\" href=\"" + xslUri + "\"?>\n\n");
      }
      (new DOMElementWriter()).write(buildElement.element, out, 0, "\t");
      out.flush();
    } catch (IOException exc) {
      throw new BuildException("Unable to write log file", exc);
    } finally {
      if (out != null) {
        try {
          out.close();
        } catch (IOException e) {
          // ignore
        }
      }
    }
    buildElement = null;
  }