View Javadoc

1   package org.apache.maven.cli;
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  
24  import org.apache.maven.Maven;
25  import org.codehaus.plexus.logging.AbstractLogger;
26  import org.codehaus.plexus.logging.Logger;
27  
28  /**
29   * Logs to a user-supplied {@link PrintStream}.
30   * 
31   * @author Benjamin Bentmann
32   */
33  public class PrintStreamLogger
34      extends AbstractLogger
35  {
36  
37      static interface Provider
38      {
39          PrintStream getStream();
40      }
41  
42      private Provider provider;
43  
44      private static final String FATAL_ERROR = "[FATAL] ";
45  
46      private static final String ERROR = "[ERROR] ";
47  
48      private static final String WARNING = "[WARNING] ";
49  
50      private static final String INFO = "[INFO] ";
51  
52      private static final String DEBUG = "[DEBUG] ";
53  
54      public PrintStreamLogger( Provider provider )
55      {
56          super( Logger.LEVEL_INFO, Maven.class.getName() );
57  
58          if ( provider == null )
59          {
60              throw new IllegalArgumentException( "output stream provider missing" );
61          }
62          this.provider = provider;
63      }
64  
65      public PrintStreamLogger( PrintStream out )
66      {
67          super( Logger.LEVEL_INFO, Maven.class.getName() );
68  
69          setStream( out );
70      }
71  
72      public void setStream( final PrintStream out )
73      {
74          if ( out == null )
75          {
76              throw new IllegalArgumentException( "output stream missing" );
77          }
78  
79          this.provider = new Provider()
80          {
81              public PrintStream getStream()
82              {
83                  return out;
84              }
85          };
86      }
87  
88      public void debug( String message, Throwable throwable )
89      {
90          if ( isDebugEnabled() )
91          {
92              PrintStream out = provider.getStream();
93  
94              out.print( DEBUG );
95              out.println( message );
96  
97              if ( null != throwable )
98              {
99                  throwable.printStackTrace( out );
100             }
101         }
102     }
103 
104     public void info( String message, Throwable throwable )
105     {
106         if ( isInfoEnabled() )
107         {
108             PrintStream out = provider.getStream();
109 
110             out.print( INFO );
111             out.println( message );
112 
113             if ( null != throwable )
114             {
115                 throwable.printStackTrace( out );
116             }
117         }
118     }
119 
120     public void warn( String message, Throwable throwable )
121     {
122         if ( isWarnEnabled() )
123         {
124             PrintStream out = provider.getStream();
125 
126             out.print( WARNING );
127             out.println( message );
128 
129             if ( null != throwable )
130             {
131                 throwable.printStackTrace( out );
132             }
133         }
134     }
135 
136     public void error( String message, Throwable throwable )
137     {
138         if ( isErrorEnabled() )
139         {
140             PrintStream out = provider.getStream();
141 
142             out.print( ERROR );
143             out.println( message );
144 
145             if ( null != throwable )
146             {
147                 throwable.printStackTrace( out );
148             }
149         }
150     }
151 
152     public void fatalError( String message, Throwable throwable )
153     {
154         if ( isFatalErrorEnabled() )
155         {
156             PrintStream out = provider.getStream();
157 
158             out.print( FATAL_ERROR );
159             out.println( message );
160 
161             if ( null != throwable )
162             {
163                 throwable.printStackTrace( out );
164             }
165         }
166     }
167 
168     public void close()
169     {
170         PrintStream out = provider.getStream();
171 
172         if ( out == System.out || out == System.err )
173         {
174             out.flush();
175         }
176         else
177         {
178             out.close();
179         }
180     }
181 
182     public Logger getChildLogger( String arg0 )
183     {
184         return this;
185     }
186 
187 }