Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 278   Methods: 2
NCLOC: 111   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AnsiColorLogger.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 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.listener;
 55   
 
 56   
 import java.io.FileInputStream;
 57   
 import java.io.IOException;
 58   
 import java.io.InputStream;
 59   
 import java.io.PrintStream;
 60   
 import java.util.Properties;
 61   
 import org.apache.tools.ant.DefaultLogger;
 62   
 import org.apache.tools.ant.Project;
 63   
 
 64   
 /**
 65   
  * Uses ANSI Color Code Sequences to colorize messages
 66   
  * sent to the console.
 67   
  *
 68   
  * If used with the -logfile option, the output file
 69   
  * will contain all the necessary escape codes to
 70   
  * display the text in colorized mode when displayed
 71   
  * in the console using applications like cat, more,
 72   
  * etc.
 73   
  *
 74   
  * This is designed to work on terminals that support ANSI
 75   
  * color codes.  It works on XTerm, ETerm, Mindterm, etc.
 76   
  * It also works on Win9x (with ANSI.SYS loaded.)
 77   
  *
 78   
  * NOTE:
 79   
  * It doesn't work on WinNT's COMMAND.COM even with
 80   
  * ANSI.SYS loaded.
 81   
  *
 82   
  * The default colors used for differentiating
 83   
  * the message levels can be changed by editing the
 84   
  * /org/apache/tools/ant/listener/defaults.properties
 85   
  * file.
 86   
  * This file contains 5 key/value pairs:
 87   
  * AnsiColorLogger.ERROR_COLOR=2;31
 88   
  * AnsiColorLogger.WARNING_COLOR=2;35
 89   
  * AnsiColorLogger.INFO_COLOR=2;36
 90   
  * AnsiColorLogger.VERBOSE_COLOR=2;32
 91   
  * AnsiColorLogger.DEBUG_COLOR=2;34
 92   
  *
 93   
  * Another option is to pass a system variable named
 94   
  * ant.logger.defaults, with value set to the path of
 95   
  * the file that contains user defined Ansi Color
 96   
  * Codes, to the <B>java</B> command using -D option.
 97   
  *
 98   
  * To change these colors use the following chart:
 99   
  *
 100   
  *      <B>ANSI COLOR LOGGER CONFIGURATION</B>
 101   
  *
 102   
  * Format for AnsiColorLogger.*=
 103   
  *  Attribute;Foreground;Background
 104   
  *
 105   
  *  Attribute is one of the following:
 106   
  *  0 -> Reset All Attributes (return to normal mode)
 107   
  *  1 -> Bright (Usually turns on BOLD)
 108   
  *  2 -> Dim
 109   
  *  3 -> Underline
 110   
  *  5 -> link
 111   
  *  7 -> Reverse
 112   
  *  8 -> Hidden
 113   
  *
 114   
  *  Foreground is one of the following:
 115   
  *  30 -> Black
 116   
  *  31 -> Red
 117   
  *  32 -> Green
 118   
  *  33 -> Yellow
 119   
  *  34 -> Blue
 120   
  *  35 -> Magenta
 121   
  *  36 -> Cyan
 122   
  *  37 -> White
 123   
  *
 124   
  *  Background is one of the following:
 125   
  *  40 -> Black
 126   
  *  41 -> Red
 127   
  *  42 -> Green
 128   
  *  43 -> Yellow
 129   
  *  44 -> Blue
 130   
  *  45 -> Magenta
 131   
  *  46 -> Cyan
 132   
  *  47 -> White
 133   
  *
 134   
  * @author Magesh Umasankar
 135   
  */
 136   
 public final class AnsiColorLogger extends DefaultLogger {
 137   
     // private static final int ATTR_NORMAL = 0;
 138   
     // private static final int ATTR_BRIGHT = 1;
 139   
     private static final int ATTR_DIM = 2;
 140   
     // private static final int ATTR_UNDERLINE = 3;
 141   
     // private static final int ATTR_BLINK = 5;
 142   
     // private static final int ATTR_REVERSE = 7;
 143   
     // private static final int ATTR_HIDDEN = 8;
 144   
 
 145   
     // private static final int FG_BLACK = 30;
 146   
     private static final int FG_RED = 31;
 147   
     private static final int FG_GREEN = 32;
 148   
     // private static final int FG_YELLOW = 33;
 149   
     private static final int FG_BLUE = 34;
 150   
     private static final int FG_MAGENTA = 35;
 151   
     private static final int FG_CYAN = 36;
 152   
     // private static final int FG_WHITE = 37;
 153   
 
 154   
     // private static final int BG_BLACK = 40;
 155   
     // private static final int BG_RED = 41;
 156   
     // private static final int BG_GREEN = 42;
 157   
     // private static final int BG_YELLOW = 44;
 158   
     // private static final int BG_BLUE = 44;
 159   
     // private static final int BG_MAGENTA = 45;
 160   
     // private static final int BG_CYAN = 46;
 161   
     // private static final int BG_WHITE = 47;
 162   
 
 163   
     private static final String PREFIX = "\u001b[";
 164   
     private static final String SUFFIX = "m";
 165   
     private static final char SEPARATOR = ';';
 166   
     private static final String END_COLOR = PREFIX + SUFFIX;
 167   
 
 168   
     private String errColor 
 169   
         = PREFIX + ATTR_DIM + SEPARATOR + FG_RED + SUFFIX;
 170   
     private String warnColor 
 171   
         = PREFIX + ATTR_DIM + SEPARATOR + FG_MAGENTA + SUFFIX;
 172   
     private String infoColor 
 173   
         = PREFIX + ATTR_DIM + SEPARATOR + FG_CYAN + SUFFIX;
 174   
     private String verboseColor 
 175   
         = PREFIX + ATTR_DIM + SEPARATOR + FG_GREEN + SUFFIX;
 176   
     private String debugColor 
 177   
         = PREFIX + ATTR_DIM + SEPARATOR + FG_BLUE + SUFFIX;
 178   
 
 179   
     private boolean colorsSet = false;
 180   
 
 181   
     /**
 182   
      * Set the colors to use from a property file specified by the
 183   
      * special ant property ant.logger.defaults
 184   
      */
 185  0
     private final void setColors() {
 186  0
         String userColorFile = System.getProperty("ant.logger.defaults");
 187  0
         String systemColorFile =
 188   
             "/org/apache/tools/ant/listener/defaults.properties";
 189   
 
 190  0
         InputStream in = null;
 191   
 
 192  0
         try {
 193  0
             Properties prop = new Properties();
 194   
 
 195  0
             if (userColorFile != null) {
 196  0
                 in = new FileInputStream(userColorFile);
 197   
             } else {
 198  0
                 in = getClass().getResourceAsStream(systemColorFile);
 199   
             }
 200   
 
 201  0
             if (in != null) {
 202  0
                 prop.load(in);
 203   
             }
 204   
 
 205  0
             String err = prop.getProperty("AnsiColorLogger.ERROR_COLOR");
 206  0
             String warn = prop.getProperty("AnsiColorLogger.WARNING_COLOR");
 207  0
             String info = prop.getProperty("AnsiColorLogger.INFO_COLOR");
 208  0
             String verbose = prop.getProperty("AnsiColorLogger.VERBOSE_COLOR");
 209  0
             String debug = prop.getProperty("AnsiColorLogger.DEBUG_COLOR");
 210  0
             if (err != null) {
 211  0
                 errColor = PREFIX + err + SUFFIX;
 212   
             }
 213  0
             if (warn != null) {
 214  0
                 warnColor = PREFIX + warn + SUFFIX;
 215   
             }
 216  0
             if (info != null) {
 217  0
                 infoColor = PREFIX + info + SUFFIX;
 218   
             }
 219  0
             if (verbose != null) {
 220  0
                 verboseColor = PREFIX + verbose + SUFFIX;
 221   
             }
 222  0
             if (debug != null) {
 223  0
                 debugColor = PREFIX + debug + SUFFIX;
 224   
             }
 225   
         } catch (IOException ioe) {
 226   
             //Ignore - we will use the defaults.
 227   
         } finally {
 228  0
             if (in != null) {
 229  0
                 try {
 230  0
                     in.close();
 231   
                 } catch (IOException e) {
 232   
                     //Ignore - We do not want this to stop the build.
 233   
                 }
 234   
             }
 235   
         }
 236   
     }
 237   
 
 238   
     /**
 239   
      * @see DefaultLogger#printMessage
 240   
      */
 241  0
     protected final void printMessage(final String message,
 242   
                                       final PrintStream stream,
 243   
                                       final int priority) {
 244  0
         if (message != null && stream != null) {
 245  0
             if (!colorsSet) {
 246  0
                 setColors();
 247  0
                 colorsSet = true;
 248   
             }
 249   
 
 250  0
             final StringBuffer msg = new StringBuffer(message);
 251  0
             switch (priority) {
 252   
                 case Project.MSG_ERR:
 253  0
                     msg.insert(0, errColor);
 254  0
                     msg.append(END_COLOR);
 255  0
                     break;
 256   
                 case Project.MSG_WARN:
 257  0
                     msg.insert(0, warnColor);
 258  0
                     msg.append(END_COLOR);
 259  0
                     break;
 260   
                 case Project.MSG_INFO:
 261  0
                     msg.insert(0, infoColor);
 262  0
                     msg.append(END_COLOR);
 263  0
                     break;
 264   
                 case Project.MSG_VERBOSE:
 265  0
                     msg.insert(0, verboseColor);
 266  0
                     msg.append(END_COLOR);
 267  0
                     break;
 268   
                 case Project.MSG_DEBUG:
 269  0
                     msg.insert(0, debugColor);
 270  0
                     msg.append(END_COLOR);
 271  0
                     break;
 272   
             }
 273  0
             final String strmessage = msg.toString();
 274  0
             stream.println(strmessage);
 275   
         }
 276   
     }
 277   
 }
 278