/*
 * $Id: AntTaskTarget.java,v 1.1 2003-05-26 03:49:51 tal Exp $
 */

/*

Copyright (c) 2000-2002 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.util;

import java.text.*;
import java.io.PrintStream;
import java.util.Date;

/** A <code>LogTarget</code> implementation that outputs to an Ant task's
    logger
 */
public class AntTaskTarget implements LogTarget {
  static final DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");

  protected org.lockss.ant.AntHelper helper;

  /** Create a log target that outputs to an AntTargetHelper */
  AntTaskTarget() {
    this.helper = new org.lockss.ant.AntHelper();
  }

  public void init() {
  }

  /** Write the message to the Ant task
   */
  public void handleMessage(Logger log, int msgLevel, String message) {
    StringBuffer sb = new StringBuffer();
    sb.append(df.format(new Date()));
    if (TimeBase.isSimulated()) {
      sb.append("(sim ");
      sb.append(TimeBase.nowMs());
      sb.append(")");
    }
    sb.append(": ");
    sb.append(log.nameOf(msgLevel));
    sb.append(": ");
    sb.append(message);
    helper.writeLog(sb.toString());
  }
}

@@ -27,17 +27,17 @@
 ava,v 1.
-1
+2
  2003-05
@@ -42,17 +42,17 @@
 05-2
-6 03:49:5
+7 21:03:3
 1 ta
@@ -1466,16 +1466,44 @@
 text.*;%0A
+import java.lang.reflect.*;%0A
 import j
@@ -1539,20 +1539,17 @@
 va.util.
-Date
+*
 ;%0A%0A/** A
@@ -1622,321 +1622,1201 @@
 's%0A 
-   logger%0A */%0Apublic class AntTaskTarget implements LogTarget %7B%0A  static final DateFormat df = new SimpleDateFormat(%22HH:mm:ss.SSS%22);%0A%0A  protected org.lockss.ant.AntHelper helper;%0A%0A  /** Create a log target that outputs to an AntTargetHelper */%0A  AntTaskTarget() %7B%0A    this.helper = new org.lockss.ant.AntHelper();
+* logger.%0A */%0A// It would be better if this class were in the ant hierarchy, as it needs%0A// access to Ant objects, and it isn't referenced from the main hierarchy.%0A// (It's invoked by putting its class name in org.lockss.defaultLogTarget.)%0A// But it needs LogTarget and Logger, which aren't available to the ant%0A// hierarchy because it's compiled first.%0A%0Apublic class AntTaskTarget implements LogTarget %7B%0A  static final DateFormat df = new SimpleDateFormat(%22HH:mm:ss.SSS%22);%0A%0A  // The helper is in the ant hierarchy, but we don't want compile-time%0A  // references into that hierarchy or the ant jars would be required to%0A  // compile even when not using ant.  So invoke the helper using reflection.%0A  //   protected AntHelper helper;%0A  protected Object helper;%0A  protected Method writeLogMethod;%0A  %0A  /** Create a log target that outputs to an AntTargetHelper */%0A  AntTaskTarget() %7B%0A    try %7B%0A      Class helperClass = Class.forName(%22org.lockss.ant.AntHelper%22);%0A      helper = helperClass.newInstance();%0A      Class argTypes%5B%5D = %7BString.class%7D;%0A      writeLogMethod = helperClass.getMethod(%22writeLog%22, argTypes);%0A    %7D catch (Exception e) %7B%0A      throw new RuntimeException(e.toString());%0A    %7D
 %0A  %7D
@@ -3277,42 +3277,299 @@
     
-helper.writeLog(sb.toString());%0A  %7D
+writeMsg(sb.toString());%0A  %7D%0A%0A  private void writeMsg(String msg) %7B%0A    Object args%5B%5D = %7Bmsg%7D;%0A    try %7B%0A      writeLogMethod.invoke(helper, args);%0A    %7D catch (Exception e) %7B%0A      System.err.println(%22Error invoking AntHelper: %22 + e.toString());%0A      System.err.println(msg);%0A    %7D%0A  %7D%0A    
 %0A%7D%0A

Use reflection to invoke AntHelper so don't need ant classes present to compile.