AntLog.java

  1. // $Id$
  2. /*
  3.  * ====================================================================
  4.  * Copyright (c) 2002-2004, Christophe Labouisse All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright notice,
  10.  * this list of conditions and the following disclaimer.
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  13.  * this list of conditions and the following disclaimer in the documentation
  14.  * and/or other materials provided with the distribution.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26.  * POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28. package net.ggtools.grand.log;

  29. import org.apache.commons.logging.Log;
  30. import org.apache.tools.ant.Project;
  31. import org.apache.tools.ant.Task;

  32. /**
  33.  * @author Christophe Labouisse
  34.  */
  35. public class AntLog extends SimpleLog implements Log {

  36.     /**
  37.      * Field currentProject.
  38.      */
  39.     private static Project currentProject;

  40.     /**
  41.      * Field currentTask.
  42.      */
  43.     private static Task currentTask;

  44.     /**
  45.      * Method setCurrentProject.
  46.      * @param newProject Project
  47.      */
  48.     public static void setCurrentProject(final Project newProject) {
  49.         currentProject = newProject;
  50.     }

  51.     /**
  52.      * Method setCurrentTask.
  53.      * @param newTask Task
  54.      */
  55.     public static void setCurrentTask(final Task newTask) {
  56.         currentTask = newTask;
  57.     }

  58.     /**
  59.      * Package only instantiation.
  60.      */
  61.     AntLog() {
  62.     }

  63.     /**
  64.      * Method log.
  65.      * @param message Object
  66.      * @param t Throwable
  67.      * @param level int
  68.      * @see net.ggtools.grand.log.SimpleLog#log(java.lang.Object, java.lang.Throwable, int)
  69.      */
  70.     @Override
  71.     protected final void log(final Object message, final Throwable t,
  72.             final int level) {
  73.         if (currentProject != null) {
  74.             // Translate into Ant log levels.
  75.             int antMsgLevel = level - LEVEL_ERROR + Project.MSG_ERR;
  76.             if (antMsgLevel < Project.MSG_ERR) {
  77.                 antMsgLevel = Project.MSG_ERR;
  78.             } else if (antMsgLevel > Project.MSG_DEBUG) {
  79.                 antMsgLevel = Project.MSG_DEBUG;
  80.             }

  81.             if (currentTask == null) {
  82.                 currentProject.log(message.toString(), antMsgLevel);
  83.             } else {
  84.                 currentProject.log(currentTask, message.toString(),
  85.                         antMsgLevel);
  86.             }
  87.         } else {
  88.             super.log(message, t, level);
  89.         }
  90.     }

  91. }