Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 335   Methods: 14
NCLOC: 199   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CommonsLoggingListener.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002-2003 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 "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   
 
 55   
 package org.apache.tools.ant.listener;
 56   
 
 57   
 import org.apache.commons.logging.Log;
 58   
 import org.apache.commons.logging.LogConfigurationException;
 59   
 import org.apache.commons.logging.LogFactory;
 60   
 import org.apache.tools.ant.*;
 61   
 
 62   
 import java.io.PrintStream;
 63   
 
 64   
 /**
 65   
  * Jakarta Commons Logging listener.
 66   
  * Note: do not use the SimpleLog as your logger implementation as it
 67   
  * causes an infinite loop since it writes to System.err, which Ant traps
 68   
  * and reroutes to the logger/listener layer.
 69   
  *
 70   
  * The following names are used for the log:
 71   
  *  org.apache.tools.ant.Project.PROJECT_NAME  - for project events
 72   
  *  org.apache.tools.ant.Target.TARGET_NAME - for target events
 73   
  *  TASK_CLASS_NAME.TARGET_NAME - for events in individual targets.
 74   
  *
 75   
  * In all target and project names we replace "." and " " with "-".
 76   
  *
 77   
  * TODO: we should use the advanced context logging features ( and expose them
 78   
  * in c-l first :-)
 79   
  * TODO: this is _very_ inefficient. Switching the out and tracking the logs
 80   
  * can be optimized a lot - but may require few more changes to the core.
 81   
  *
 82   
  * @author Erik Hatcher
 83   
  * @since Ant 1.5
 84   
  */
 85   
 public class CommonsLoggingListener implements BuildListener, BuildLogger {
 86   
 
 87   
     /** Indicates if the listener was initialized. */
 88   
     private boolean initialized = false;
 89   
 
 90   
     private LogFactory logFactory;
 91   
 
 92   
     /**
 93   
      * Construct the listener and make sure that a LogFactory
 94   
      * can be obtained.
 95   
      */
 96  0
     public CommonsLoggingListener() {
 97   
     }
 98   
 
 99  0
     private Log getLog( String cat, String suffix ) {
 100  0
         if( suffix != null ) {
 101  0
             suffix=suffix.replace('.', '-');
 102  0
             suffix=suffix.replace(' ', '-');
 103  0
             cat=cat + "." + suffix;
 104   
         }
 105  0
         PrintStream tmpOut=System.out;
 106  0
         PrintStream tmpErr=System.err;
 107  0
         System.setOut( out );
 108  0
         System.setErr( err );
 109   
 
 110  0
         if( ! initialized ) {
 111  0
             try {
 112  0
                 logFactory = LogFactory.getFactory();
 113   
             } catch (LogConfigurationException e) {
 114  0
                 e.printStackTrace(System.err);
 115  0
                 return null;
 116   
             }
 117   
         }
 118   
 
 119  0
         initialized = true;
 120  0
         Log log=logFactory.getInstance(cat);
 121  0
         System.setOut( tmpOut );
 122  0
         System.setErr( tmpErr );
 123  0
         return log;
 124   
     }
 125   
 
 126   
     /**
 127   
      * @see BuildListener#buildStarted
 128   
      */
 129  0
     public void buildStarted(BuildEvent event) {
 130  0
         String categoryString= "org.apache.tools.ant.Project";
 131  0
         Log log=getLog(categoryString, null);
 132   
 
 133  0
         if (initialized) {
 134  0
             realLog( log, "Build started.", Project.MSG_INFO, null);
 135   
         }
 136   
     }
 137   
 
 138   
     /**
 139   
      * @see BuildListener#buildFinished
 140   
      */
 141  0
     public void buildFinished(BuildEvent event) {
 142  0
         if (initialized) {
 143  0
             String categoryString= "org.apache.tools.ant.Project";
 144  0
             Log log=getLog(categoryString, event.getProject().getName());
 145   
 
 146  0
             if (event.getException() == null) {
 147  0
                 realLog( log, "Build finished.", Project.MSG_INFO, null);
 148   
             } else {
 149  0
                 realLog( log, "Build finished with error.", Project.MSG_ERR,
 150   
                         event.getException());
 151   
             }
 152   
         }
 153   
     }
 154   
 
 155   
     /**
 156   
      * @see BuildListener#targetStarted
 157   
      */
 158  0
     public void targetStarted(BuildEvent event) {
 159  0
         if (initialized) {
 160  0
             Log log = getLog("org.apache.tools.ant.Target",
 161   
                     event.getTarget().getName() );
 162   
             // Since task log category includes target, we don't really
 163   
             // need this message
 164  0
             realLog( log, "Start: " + event.getTarget().getName(),
 165   
                     Project.MSG_DEBUG, null);
 166   
         }
 167   
     }
 168   
 
 169   
     /**
 170   
      * @see BuildListener#targetFinished
 171   
      */
 172  0
     public void targetFinished(BuildEvent event) {
 173  0
         if (initialized) {
 174  0
             String targetName = event.getTarget().getName();
 175  0
             Log log = getLog("org.apache.tools.ant.Target",
 176   
                     event.getTarget().getName() );
 177  0
             if (event.getException() == null) {
 178  0
                 realLog(log, "Target end: " + targetName, Project.MSG_DEBUG, null);
 179   
             } else {
 180  0
                 realLog(log, "Target \"" + targetName
 181   
                         + "\" finished with error.", Project.MSG_ERR,
 182   
                         event.getException());
 183   
             }
 184   
         }
 185   
     }
 186   
 
 187   
     /**
 188   
      * @see BuildListener#taskStarted
 189   
      */
 190  0
     public void taskStarted(BuildEvent event) {
 191  0
         if (initialized) {
 192  0
             Task task = event.getTask();
 193  0
             Object real=task;
 194  0
             if( task instanceof UnknownElement ) {
 195  0
                 Object realObj=((UnknownElement)task).getTask();
 196  0
                 if( realObj!=null ) {
 197  0
                     real=realObj;
 198   
                 }
 199   
             }
 200  0
             Log log = getLog(real.getClass().getName(), null);
 201  0
             if( log.isTraceEnabled()) {
 202  0
                 realLog( log, "Task \"" + task.getTaskName() + "\" started ",
 203   
                         Project.MSG_VERBOSE, null);
 204   
             }
 205   
         }
 206   
     }
 207   
 
 208   
     /**
 209   
      * @see BuildListener#taskFinished
 210   
      */
 211  0
     public void taskFinished(BuildEvent event) {
 212  0
         if (initialized) {
 213  0
             Task task = event.getTask();
 214  0
             Object real=task;
 215  0
             if( task instanceof UnknownElement ) {
 216  0
                 Object realObj=((UnknownElement)task).getTask();
 217  0
                 if( realObj!=null ) {
 218  0
                     real=realObj;
 219   
                 }
 220   
             }
 221  0
             Log log = getLog(real.getClass().getName(), null);
 222  0
             if (event.getException() == null) {
 223  0
                 if( log.isTraceEnabled() ) {
 224  0
                     realLog( log, "Task \"" + task.getTaskName() + "\" finished.",
 225   
                             Project.MSG_VERBOSE, null);
 226   
                 }
 227   
             } else {
 228  0
                 realLog( log, "Task \"" + task.getTaskName()
 229   
                         + "\" finished with error.", Project.MSG_ERR,
 230   
                         event.getException());
 231   
             }
 232   
         }
 233   
     }
 234   
 
 235   
 
 236   
     /**
 237   
      * @see BuildListener#messageLogged
 238   
      */
 239  0
     public void messageLogged(BuildEvent event) {
 240  0
         if (initialized) {
 241  0
             Object categoryObject = event.getTask();
 242  0
             String categoryString=null;
 243  0
             String categoryDetail=null;
 244   
 
 245  0
             if (categoryObject == null) {
 246  0
                 categoryObject = event.getTarget();
 247  0
                 if (categoryObject == null) {
 248  0
                     categoryObject = event.getProject();
 249  0
                     categoryString="org.apache.tools.ant.Project";
 250  0
                     categoryDetail=event.getProject().getName();
 251   
                 } else {
 252  0
                     categoryString= "org.apache.tools.ant.Target";
 253  0
                     categoryDetail=event.getTarget().getName();
 254   
                 }
 255   
             } else {
 256   
                 // It's a task - append the target
 257  0
                 if( event.getTarget() != null ) {
 258  0
                     categoryString=categoryObject.getClass().getName();
 259  0
                     categoryDetail=event.getTarget().getName();
 260   
                 } else {
 261  0
                     categoryString=categoryObject.getClass().getName();
 262   
                 }
 263   
 
 264   
             }
 265   
 
 266  0
             Log log = getLog(categoryString, categoryDetail);
 267  0
             int priority=event.getPriority();
 268  0
             String message=event.getMessage();
 269  0
             realLog( log, message, priority , null);
 270   
         }
 271   
     }
 272   
 
 273  0
     private void realLog( Log log, String message, int priority, Throwable t )
 274   
     {
 275  0
         PrintStream tmpOut=System.out;
 276  0
         PrintStream tmpErr=System.err;
 277  0
         System.setOut( out );
 278  0
         System.setErr( err );
 279  0
         switch (priority) {
 280   
             case Project.MSG_ERR:
 281  0
                 if( t==null ) {
 282  0
                     log.error(message);
 283   
                 } else {
 284  0
                     log.error( message,t );
 285   
                 }
 286  0
                 break;
 287   
             case Project.MSG_WARN:
 288  0
                 if( t==null ) {
 289  0
                     log.warn(message);
 290   
                 } else {
 291  0
                     log.warn( message,t );
 292   
                 }
 293  0
                 break;
 294   
             case Project.MSG_INFO:
 295  0
                 if( t==null ) {
 296  0
                     log.info(message);
 297   
                 } else {
 298  0
                     log.info( message,t );
 299   
                 }
 300  0
                 break;
 301   
             case Project.MSG_VERBOSE:
 302  0
                 log.debug(message);
 303  0
                 break;
 304   
             case Project.MSG_DEBUG:
 305  0
                 log.debug(message);
 306  0
                 break;
 307   
             default:
 308  0
                 log.error(message);
 309  0
                 break;
 310   
         }
 311  0
         System.setOut( tmpOut );
 312  0
         System.setErr( tmpErr );
 313   
     }
 314   
 
 315   
     PrintStream out;
 316   
     PrintStream err;
 317   
 
 318  0
     public void setMessageOutputLevel(int level) {
 319   
         // Use the logger config
 320   
     }
 321   
 
 322  0
     public void setOutputPrintStream(PrintStream output) {
 323  0
         this.out = output;
 324   
     }
 325   
 
 326  0
     public void setEmacsMode(boolean emacsMode) {
 327   
         // Doesn't make sense for c-l. Use the logger config
 328   
     }
 329   
 
 330  0
     public void setErrorPrintStream(PrintStream err) {
 331  0
         this.err=err;
 332   
     }
 333   
 
 334   
 }
 335