Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 262   Methods: 9
NCLOC: 99   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
Recorder.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 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   
 package org.apache.tools.ant.taskdefs;
 55   
 
 56   
 import java.io.FileOutputStream;
 57   
 import java.io.IOException;
 58   
 import java.io.PrintStream;
 59   
 import java.util.Hashtable;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 import org.apache.tools.ant.Project;
 62   
 import org.apache.tools.ant.Task;
 63   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 64   
 
 65   
 /**
 66   
  * Adds a listener to the current build process that records the
 67   
  * output to a file.
 68   
  * <p>Several recorders can exist at the same time.  Each recorder is
 69   
  * associated with a file.  The filename is used as a unique identifier for
 70   
  * the recorders.  The first call to the recorder task with an unused filename
 71   
  * will create a recorder (using the parameters provided) and add it to the
 72   
  * listeners of the build.  All subsequent calls to the recorder task using
 73   
  * this filename will modify that recorders state (recording or not) or other
 74   
  * properties (like logging level).</p>
 75   
  * <p>Some technical issues: the file's print stream is flushed for &quot;finished&quot;
 76   
  * events (buildFinished, targetFinished and taskFinished), and is closed on
 77   
  * a buildFinished event.</p>
 78   
  * @author <a href="mailto:jayglanville@home.com">J D Glanville</a>
 79   
  * @see RecorderEntry
 80   
  * @version 0.5
 81   
  * @since Ant 1.4
 82   
  * @ant.task name="record" category="utility"
 83   
  */
 84   
 public class Recorder extends Task {
 85   
 
 86   
     //////////////////////////////////////////////////////////////////////
 87   
     // ATTRIBUTES
 88   
 
 89   
     /** The name of the file to record to. */
 90   
     private String filename = null;
 91   
     /**
 92   
      * Whether or not to append. Need Boolean to record an unset state (null).
 93   
      */
 94   
     private Boolean append = null;
 95   
     /**
 96   
      * Whether to start or stop recording. Need Boolean to record an unset
 97   
      * state (null).
 98   
      */
 99   
     private Boolean start = null;
 100   
     /** The level to log at. A level of -1 means not initialized yet. */
 101   
     private int loglevel = -1;
 102   
     /** Strip task banners if true.  */
 103   
     private boolean emacsMode = false;
 104   
     /** The list of recorder entries. */
 105   
     private static Hashtable recorderEntries = new Hashtable();
 106   
 
 107   
     //////////////////////////////////////////////////////////////////////
 108   
     // CONSTRUCTORS / INITIALIZERS
 109   
 
 110   
     //////////////////////////////////////////////////////////////////////
 111   
     // ACCESSOR METHODS
 112   
 
 113   
     /**
 114   
      * Sets the name of the file to log to, and the name of the recorder
 115   
      * entry.
 116   
      *
 117   
      * @param fname File name of logfile.
 118   
      */
 119  0
     public void setName(String fname) {
 120  0
         filename = fname;
 121   
     }
 122   
 
 123   
 
 124   
     /**
 125   
      * Sets the action for the associated recorder entry.
 126   
      *
 127   
      * @param action The action for the entry to take: start or stop.
 128   
      */
 129  0
     public void setAction(ActionChoices action) {
 130  0
         if (action.getValue().equalsIgnoreCase("start")) {
 131  0
             start = Boolean.TRUE;
 132   
         } else {
 133  0
             start = Boolean.FALSE;
 134   
         }
 135   
     }
 136   
 
 137   
 
 138   
     /** Whether or not the logger should append to a previous file.  */
 139  0
     public void setAppend(boolean append) {
 140  0
         this.append = new Boolean(append);
 141   
     }
 142   
 
 143   
 
 144  0
     public void setEmacsMode(boolean emacsMode) {
 145  0
         this.emacsMode = emacsMode;
 146   
     }
 147   
 
 148   
 
 149   
     /**
 150   
      * Sets the level to which this recorder entry should log to.
 151   
      *
 152   
      * @see VerbosityLevelChoices
 153   
      */
 154  0
     public void setLoglevel(VerbosityLevelChoices level) {
 155   
         //I hate cascading if/elseif clauses !!!
 156  0
         String lev = level.getValue();
 157   
 
 158  0
         if (lev.equalsIgnoreCase("error")) {
 159  0
             loglevel = Project.MSG_ERR;
 160  0
         } else if (lev.equalsIgnoreCase("warn")) {
 161  0
             loglevel = Project.MSG_WARN;
 162  0
         } else if (lev.equalsIgnoreCase("info")) {
 163  0
             loglevel = Project.MSG_INFO;
 164  0
         } else if (lev.equalsIgnoreCase("verbose")) {
 165  0
             loglevel = Project.MSG_VERBOSE;
 166  0
         } else if (lev.equalsIgnoreCase("debug")) {
 167  0
             loglevel = Project.MSG_DEBUG;
 168   
         }
 169   
     }
 170   
 
 171   
     //////////////////////////////////////////////////////////////////////
 172   
     // CORE / MAIN BODY
 173   
 
 174   
     /** The main execution.  */
 175  0
     public void execute() throws BuildException {
 176  0
         if (filename == null) {
 177  0
             throw new BuildException("No filename specified");
 178   
         }
 179   
 
 180  0
         getProject().log("setting a recorder for name " + filename,
 181   
             Project.MSG_DEBUG);
 182   
 
 183   
         // get the recorder entry
 184  0
         RecorderEntry recorder = getRecorder(filename, getProject());
 185   
         // set the values on the recorder
 186  0
         recorder.setMessageOutputLevel(loglevel);
 187  0
         recorder.setRecordState(start);
 188  0
         recorder.setEmacsMode(emacsMode);
 189   
     }
 190   
 
 191   
     //////////////////////////////////////////////////////////////////////
 192   
     // INNER CLASSES
 193   
 
 194   
     /**
 195   
      * A list of possible values for the <code>setAction()</code> method.
 196   
      * Possible values include: start and stop.
 197   
      */
 198   
     public static class ActionChoices extends EnumeratedAttribute {
 199   
         private static final String[] values = {"start", "stop"};
 200   
 
 201   
 
 202  0
         public String[] getValues() {
 203  0
             return values;
 204   
         }
 205   
     }
 206   
 
 207   
 
 208   
     /**
 209   
      * A list of possible values for the <code>setLoglevel()</code> method.
 210   
      * Possible values include: error, warn, info, verbose, debug.
 211   
      */
 212   
     public static class VerbosityLevelChoices extends EnumeratedAttribute {
 213   
         private static final String[] values = {"error", "warn", "info",
 214   
             "verbose", "debug"};
 215   
 
 216   
 
 217  0
         public String[] getValues() {
 218  0
             return values;
 219   
         }
 220   
     }
 221   
 
 222   
 
 223   
     /**
 224   
      * Gets the recorder that's associated with the passed in name. If the
 225   
      * recorder doesn't exist, then a new one is created.
 226   
      */
 227  0
     protected RecorderEntry getRecorder(String name, Project proj)
 228   
          throws BuildException {
 229  0
         Object o = recorderEntries.get(name);
 230  0
         RecorderEntry entry;
 231   
 
 232  0
         if (o == null) {
 233   
             // create a recorder entry
 234  0
             try {
 235  0
                 entry = new RecorderEntry(name);
 236   
 
 237  0
                 PrintStream out = null;
 238   
 
 239  0
                 if (append == null) {
 240  0
                     out = new PrintStream(
 241   
                         new FileOutputStream(name));
 242   
                 } else {
 243  0
                     out = new PrintStream(
 244   
                         new FileOutputStream(name, append.booleanValue()));
 245   
                 }
 246  0
                 entry.setErrorPrintStream(out);
 247  0
                 entry.setOutputPrintStream(out);
 248   
             } catch (IOException ioe) {
 249  0
                 throw new BuildException("Problems creating a recorder entry",
 250   
                     ioe);
 251   
             }
 252  0
             proj.addBuildListener(entry);
 253  0
             recorderEntries.put(name, entry);
 254   
         } else {
 255  0
             entry = (RecorderEntry) o;
 256   
         }
 257  0
         return entry;
 258   
     }
 259   
 
 260   
 }
 261   
 
 262