Coverage Report - org.apache.maven.shared.invoker.PrintStreamLogger
 
Classes in this File Line Coverage Branch Coverage Complexity
PrintStreamLogger
95%
61/64
93%
28/30
1,7
 
 1  
 package org.apache.maven.shared.invoker;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.PrintStream;
 23  
 import java.io.PrintWriter;
 24  
 import java.io.StringWriter;
 25  
 
 26  
 /**
 27  
  * Offers a logger that writes to a print stream like {@link System#out}.
 28  
  * 
 29  
  * @version $Id: PrintStreamLogger.java 661996 2008-05-31 10:50:38Z bentmann $
 30  
  * @since 2.0.9
 31  
  */
 32  
 public class PrintStreamLogger
 33  
     implements InvokerLogger
 34  
 {
 35  
 
 36  
     /**
 37  
      * The print stream to write to, never <code>null</code>.
 38  
      */
 39  
     private PrintStream out;
 40  
 
 41  
     /**
 42  
      * The threshold used to filter messages.
 43  
      */
 44  
     private int threshold;
 45  
 
 46  
     /**
 47  
      * Creates a new logger that writes to {@link System#out} and has a threshold of {@link #INFO}.
 48  
      */
 49  
     public PrintStreamLogger()
 50  
     {
 51  0
         this( System.out, INFO );
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Creates a new logger that writes to the specified print stream.
 56  
      * 
 57  
      * @param out The print stream to write to, must not be <code>null</code>.
 58  
      * @param threshold The threshold for the logger.
 59  
      */
 60  
     public PrintStreamLogger( PrintStream out, int threshold )
 61  39
     {
 62  39
         if ( out == null )
 63  
         {
 64  0
             throw new NullPointerException( "missing output stream" );
 65  
         }
 66  39
         this.out = out;
 67  39
         setThreshold( threshold );
 68  39
     }
 69  
 
 70  
     /**
 71  
      * Writes the specified message and exception to the print stream.
 72  
      * 
 73  
      * @param level The priority level of the message.
 74  
      * @param message The message to log, may be <code>null</code>.
 75  
      * @param error The exception to log, may be <code>null</code>.
 76  
      */
 77  
     private void log( int level, String message, Throwable error )
 78  
     {
 79  49
         if ( level > threshold )
 80  
         {
 81  
             // don't log when it doesn't match your threshold.
 82  12
             return;
 83  
         }
 84  
 
 85  37
         if ( message == null && error == null )
 86  
         {
 87  
             // don't log when there's nothing to log.
 88  4
             return;
 89  
         }
 90  
 
 91  33
         StringBuffer buffer = new StringBuffer();
 92  
 
 93  33
         switch ( level )
 94  
         {
 95  
             case ( DEBUG ):
 96  
             {
 97  13
                 buffer.append( "[DEBUG]" );
 98  13
                 break;
 99  
             }
 100  
             case ( INFO ):
 101  
             {
 102  4
                 buffer.append( "[INFO]" );
 103  4
                 break;
 104  
             }
 105  
             case ( WARN ):
 106  
             {
 107  8
                 buffer.append( "[WARN]" );
 108  8
                 break;
 109  
             }
 110  
             case ( ERROR ):
 111  
             {
 112  4
                 buffer.append( "[ERROR]" );
 113  4
                 break;
 114  
             }
 115  
             case ( FATAL ):
 116  
             {
 117  4
                 buffer.append( "[FATAL]" );
 118  
                 break;
 119  
             }
 120  
         }
 121  
 
 122  33
         buffer.append( ' ' );
 123  
 
 124  33
         if ( message != null )
 125  
         {
 126  29
             buffer.append( message );
 127  
         }
 128  
 
 129  33
         if ( error != null )
 130  
         {
 131  8
             StringWriter writer = new StringWriter();
 132  8
             PrintWriter pWriter = new PrintWriter( writer );
 133  
 
 134  8
             error.printStackTrace( pWriter );
 135  
 
 136  8
             if ( message != null )
 137  
             {
 138  4
                 buffer.append( '\n' );
 139  
             }
 140  
 
 141  8
             buffer.append( "Error:\n" );
 142  8
             buffer.append( writer.toString() );
 143  
         }
 144  
 
 145  33
         out.println( buffer.toString() );
 146  33
     }
 147  
 
 148  
     public void debug( String message )
 149  
     {
 150  22
         log( DEBUG, message, null );
 151  22
     }
 152  
 
 153  
     public void debug( String message, Throwable throwable )
 154  
     {
 155  3
         log( DEBUG, message, throwable );
 156  3
     }
 157  
 
 158  
     public void info( String message )
 159  
     {
 160  2
         log( INFO, message, null );
 161  2
     }
 162  
 
 163  
     public void info( String message, Throwable throwable )
 164  
     {
 165  3
         log( INFO, message, throwable );
 166  3
     }
 167  
 
 168  
     public void warn( String message )
 169  
     {
 170  6
         log( WARN, message, null );
 171  6
     }
 172  
 
 173  
     public void warn( String message, Throwable throwable )
 174  
     {
 175  3
         log( WARN, message, throwable );
 176  3
     }
 177  
 
 178  
     public void error( String message )
 179  
     {
 180  2
         log( ERROR, message, null );
 181  2
     }
 182  
 
 183  
     public void error( String message, Throwable throwable )
 184  
     {
 185  3
         log( ERROR, message, throwable );
 186  3
     }
 187  
 
 188  
     public void fatalError( String message )
 189  
     {
 190  2
         log( FATAL, message, null );
 191  2
     }
 192  
 
 193  
     public void fatalError( String message, Throwable throwable )
 194  
     {
 195  3
         log( FATAL, message, throwable );
 196  3
     }
 197  
 
 198  
     public boolean isDebugEnabled()
 199  
     {
 200  11
         return threshold >= DEBUG;
 201  
     }
 202  
 
 203  
     public boolean isErrorEnabled()
 204  
     {
 205  5
         return threshold >= ERROR;
 206  
     }
 207  
 
 208  
     public boolean isFatalErrorEnabled()
 209  
     {
 210  5
         return threshold >= FATAL;
 211  
     }
 212  
 
 213  
     public boolean isInfoEnabled()
 214  
     {
 215  5
         return threshold >= INFO;
 216  
     }
 217  
 
 218  
     public boolean isWarnEnabled()
 219  
     {
 220  5
         return threshold >= WARN;
 221  
     }
 222  
 
 223  
     public int getThreshold()
 224  
     {
 225  1
         return threshold;
 226  
     }
 227  
 
 228  
     public void setThreshold( int threshold )
 229  
     {
 230  50
         this.threshold = threshold;
 231  50
     }
 232  
 
 233  
 }