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 java.lang.System#out}.
28   *
29   * @since 2.0.9
30   */
31  public class PrintStreamLogger
32      implements InvokerLogger
33  {
34  
35      /**
36       * The print stream to write to, never <code>null</code>.
37       */
38      private PrintStream out;
39  
40      /**
41       * The threshold used to filter messages.
42       */
43      private int threshold;
44  
45      /**
46       * Creates a new logger that writes to {@link java.lang.System#out} and has a threshold of {@link #INFO}.
47       */
48      public PrintStreamLogger()
49      {
50          this( System.out, INFO );
51      }
52  
53      /**
54       * Creates a new logger that writes to the specified print stream.
55       *
56       * @param out The print stream to write to, must not be <code>null</code>.
57       * @param threshold The threshold for the logger.
58       */
59      public PrintStreamLogger( PrintStream out, int threshold )
60      {
61          if ( out == null )
62          {
63              throw new NullPointerException( "missing output stream" );
64          }
65          this.out = out;
66          setThreshold( threshold );
67      }
68  
69      /**
70       * Writes the specified message and exception to the print stream.
71       * 
72       * @param level The priority level of the message.
73       * @param message The message to log, may be <code>null</code>.
74       * @param error The exception to log, may be <code>null</code>.
75       */
76      private void log( int level, String message, Throwable error )
77      {
78          if ( level > threshold )
79          {
80              // don't log when it doesn't match your threshold.
81              return;
82          }
83  
84          if ( message == null && error == null )
85          {
86              // don't log when there's nothing to log.
87              return;
88          }
89  
90          StringBuilder buffer = new StringBuilder();
91  
92          switch ( level )
93          {
94              case ( DEBUG ):
95                  buffer.append( "[DEBUG]" );
96                  break;
97  
98              case ( INFO ):
99                  buffer.append( "[INFO]" );
100                 break;
101 
102             case ( WARN ):
103                 buffer.append( "[WARN]" );
104                 break;
105 
106             case ( ERROR ):
107                 buffer.append( "[ERROR]" );
108                 break;
109 
110             case ( FATAL ):
111                 buffer.append( "[FATAL]" );
112                 break;
113 
114             default:
115         }
116 
117         buffer.append( ' ' );
118 
119         if ( message != null )
120         {
121             buffer.append( message );
122         }
123 
124         if ( error != null )
125         {
126             StringWriter writer = new StringWriter();
127             PrintWriter pWriter = new PrintWriter( writer );
128 
129             error.printStackTrace( pWriter );
130 
131             if ( message != null )
132             {
133                 buffer.append( '\n' );
134             }
135 
136             buffer.append( "Error:\n" );
137             buffer.append( writer.toString() );
138         }
139 
140         out.println( buffer.toString() );
141     }
142 
143     /** {@inheritDoc} */
144     public void debug( String message )
145     {
146         log( DEBUG, message, null );
147     }
148 
149     /** {@inheritDoc} */
150     public void debug( String message, Throwable throwable )
151     {
152         log( DEBUG, message, throwable );
153     }
154 
155     /** {@inheritDoc} */
156     public void info( String message )
157     {
158         log( INFO, message, null );
159     }
160 
161     /** {@inheritDoc} */
162     public void info( String message, Throwable throwable )
163     {
164         log( INFO, message, throwable );
165     }
166 
167     /** {@inheritDoc} */
168     public void warn( String message )
169     {
170         log( WARN, message, null );
171     }
172 
173     /** {@inheritDoc} */
174     public void warn( String message, Throwable throwable )
175     {
176         log( WARN, message, throwable );
177     }
178 
179     /** {@inheritDoc} */
180     public void error( String message )
181     {
182         log( ERROR, message, null );
183     }
184 
185     /** {@inheritDoc} */
186     public void error( String message, Throwable throwable )
187     {
188         log( ERROR, message, throwable );
189     }
190 
191     /** {@inheritDoc} */
192     public void fatalError( String message )
193     {
194         log( FATAL, message, null );
195     }
196 
197     /** {@inheritDoc} */
198     public void fatalError( String message, Throwable throwable )
199     {
200         log( FATAL, message, throwable );
201     }
202 
203     /**
204      * <p>isDebugEnabled.</p>
205      *
206      * @return a boolean.
207      */
208     public boolean isDebugEnabled()
209     {
210         return threshold >= DEBUG;
211     }
212 
213     /**
214      * <p>isErrorEnabled.</p>
215      *
216      * @return a boolean.
217      */
218     public boolean isErrorEnabled()
219     {
220         return threshold >= ERROR;
221     }
222 
223     /**
224      * <p>isFatalErrorEnabled.</p>
225      *
226      * @return a boolean.
227      */
228     public boolean isFatalErrorEnabled()
229     {
230         return threshold >= FATAL;
231     }
232 
233     /**
234      * <p>isInfoEnabled.</p>
235      *
236      * @return a boolean.
237      */
238     public boolean isInfoEnabled()
239     {
240         return threshold >= INFO;
241     }
242 
243     /**
244      * <p>isWarnEnabled.</p>
245      *
246      * @return a boolean.
247      */
248     public boolean isWarnEnabled()
249     {
250         return threshold >= WARN;
251     }
252 
253     /**
254      * <p>Getter for the field <code>threshold</code>.</p>
255      *
256      * @return a int.
257      */
258     public int getThreshold()
259     {
260         return threshold;
261     }
262 
263     /** {@inheritDoc} */
264     public void setThreshold( int threshold )
265     {
266         this.threshold = threshold;
267     }
268 
269 }