View Javadoc

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          this( System.out, INFO );
52      }
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      {
62          if ( out == null )
63          {
64              throw new NullPointerException( "missing output stream" );
65          }
66          this.out = out;
67          setThreshold( threshold );
68      }
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          if ( level > threshold )
80          {
81              // don't log when it doesn't match your threshold.
82              return;
83          }
84  
85          if ( message == null && error == null )
86          {
87              // don't log when there's nothing to log.
88              return;
89          }
90  
91          StringBuffer buffer = new StringBuffer();
92  
93          switch ( level )
94          {
95              case ( DEBUG ):
96              {
97                  buffer.append( "[DEBUG]" );
98                  break;
99              }
100             case ( INFO ):
101             {
102                 buffer.append( "[INFO]" );
103                 break;
104             }
105             case ( WARN ):
106             {
107                 buffer.append( "[WARN]" );
108                 break;
109             }
110             case ( ERROR ):
111             {
112                 buffer.append( "[ERROR]" );
113                 break;
114             }
115             case ( FATAL ):
116             {
117                 buffer.append( "[FATAL]" );
118                 break;
119             }
120         }
121 
122         buffer.append( ' ' );
123 
124         if ( message != null )
125         {
126             buffer.append( message );
127         }
128 
129         if ( error != null )
130         {
131             StringWriter writer = new StringWriter();
132             PrintWriter pWriter = new PrintWriter( writer );
133 
134             error.printStackTrace( pWriter );
135 
136             if ( message != null )
137             {
138                 buffer.append( '\n' );
139             }
140 
141             buffer.append( "Error:\n" );
142             buffer.append( writer.toString() );
143         }
144 
145         out.println( buffer.toString() );
146     }
147 
148     public void debug( String message )
149     {
150         log( DEBUG, message, null );
151     }
152 
153     public void debug( String message, Throwable throwable )
154     {
155         log( DEBUG, message, throwable );
156     }
157 
158     public void info( String message )
159     {
160         log( INFO, message, null );
161     }
162 
163     public void info( String message, Throwable throwable )
164     {
165         log( INFO, message, throwable );
166     }
167 
168     public void warn( String message )
169     {
170         log( WARN, message, null );
171     }
172 
173     public void warn( String message, Throwable throwable )
174     {
175         log( WARN, message, throwable );
176     }
177 
178     public void error( String message )
179     {
180         log( ERROR, message, null );
181     }
182 
183     public void error( String message, Throwable throwable )
184     {
185         log( ERROR, message, throwable );
186     }
187 
188     public void fatalError( String message )
189     {
190         log( FATAL, message, null );
191     }
192 
193     public void fatalError( String message, Throwable throwable )
194     {
195         log( FATAL, message, throwable );
196     }
197 
198     public boolean isDebugEnabled()
199     {
200         return threshold >= DEBUG;
201     }
202 
203     public boolean isErrorEnabled()
204     {
205         return threshold >= ERROR;
206     }
207 
208     public boolean isFatalErrorEnabled()
209     {
210         return threshold >= FATAL;
211     }
212 
213     public boolean isInfoEnabled()
214     {
215         return threshold >= INFO;
216     }
217 
218     public boolean isWarnEnabled()
219     {
220         return threshold >= WARN;
221     }
222 
223     public int getThreshold()
224     {
225         return threshold;
226     }
227 
228     public void setThreshold( int threshold )
229     {
230         this.threshold = threshold;
231     }
232 
233 }