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      private PrintStream out;
38  
39      private static final String FATAL_ERROR = "[FATAL] ";
40  
41      private static final String ERROR = "[ERROR] ";
42  
43      private static final String WARNING = "[WARNING] ";
44  
45      private static final String INFO = "[INFO] ";
46  
47      private static final String DEBUG = "[DEBUG] ";
48  
49      public PrintStreamLogger( PrintStream out )
50      {
51          super( Logger.LEVEL_INFO, Maven.class.getName() );
52  
53          setStream( out );
54      }
55  
56      public void setStream( PrintStream out )
57      {
58          if ( out == null )
59          {
60              throw new IllegalArgumentException( "output stream missing" );
61          }
62  
63          this.out = out;
64      }
65  
66      public void debug( String message, Throwable throwable )
67      {
68          if ( isDebugEnabled() )
69          {
70              out.print( DEBUG );
71              out.println( message );
72  
73              if ( null != throwable )
74              {
75                  throwable.printStackTrace( out );
76              }
77          }
78      }
79  
80      public void info( String message, Throwable throwable )
81      {
82          if ( isInfoEnabled() )
83          {
84              out.print( INFO );
85              out.println( message );
86  
87              if ( null != throwable )
88              {
89                  throwable.printStackTrace( out );
90              }
91          }
92      }
93  
94      public void warn( String message, Throwable throwable )
95      {
96          if ( isWarnEnabled() )
97          {
98              out.print( WARNING );
99              out.println( message );
100 
101             if ( null != throwable )
102             {
103                 throwable.printStackTrace( out );
104             }
105         }
106     }
107 
108     public void error( String message, Throwable throwable )
109     {
110         if ( isErrorEnabled() )
111         {
112             out.print( ERROR );
113             out.println( message );
114 
115             if ( null != throwable )
116             {
117                 throwable.printStackTrace( out );
118             }
119         }
120     }
121 
122     public void fatalError( String message, Throwable throwable )
123     {
124         if ( isFatalErrorEnabled() )
125         {
126             out.print( FATAL_ERROR );
127             out.println( message );
128 
129             if ( null != throwable )
130             {
131                 throwable.printStackTrace( out );
132             }
133         }
134     }
135 
136     public void close()
137     {
138         if ( out == System.out || out == System.err )
139         {
140             out.flush();
141         }
142         else
143         {
144             out.close();
145         }
146     }
147 
148     public Logger getChildLogger( String arg0 )
149     {
150         return this;
151     }
152 
153 }