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   * @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 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          StringBuffer buffer = new StringBuffer();
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     public void debug( String message )
144     {
145         log( DEBUG, message, null );
146     }
147 
148     public void debug( String message, Throwable throwable )
149     {
150         log( DEBUG, message, throwable );
151     }
152 
153     public void info( String message )
154     {
155         log( INFO, message, null );
156     }
157 
158     public void info( String message, Throwable throwable )
159     {
160         log( INFO, message, throwable );
161     }
162 
163     public void warn( String message )
164     {
165         log( WARN, message, null );
166     }
167 
168     public void warn( String message, Throwable throwable )
169     {
170         log( WARN, message, throwable );
171     }
172 
173     public void error( String message )
174     {
175         log( ERROR, message, null );
176     }
177 
178     public void error( String message, Throwable throwable )
179     {
180         log( ERROR, message, throwable );
181     }
182 
183     public void fatalError( String message )
184     {
185         log( FATAL, message, null );
186     }
187 
188     public void fatalError( String message, Throwable throwable )
189     {
190         log( FATAL, message, throwable );
191     }
192 
193     public boolean isDebugEnabled()
194     {
195         return threshold >= DEBUG;
196     }
197 
198     public boolean isErrorEnabled()
199     {
200         return threshold >= ERROR;
201     }
202 
203     public boolean isFatalErrorEnabled()
204     {
205         return threshold >= FATAL;
206     }
207 
208     public boolean isInfoEnabled()
209     {
210         return threshold >= INFO;
211     }
212 
213     public boolean isWarnEnabled()
214     {
215         return threshold >= WARN;
216     }
217 
218     public int getThreshold()
219     {
220         return threshold;
221     }
222 
223     public void setThreshold( int threshold )
224     {
225         this.threshold = threshold;
226     }
227 
228 }