View Javadoc
1   package org.apache.maven.doxia.logging;
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.PrintWriter;
23  import java.io.StringWriter;
24  
25  /**
26   * Logger with "standard" output and error output stream. The log prefix is voluntarily in lower case.
27   * <br>
28   * Based on <code>org.apache.maven.plugin.logging.SystemStreamLog</code>.
29   *
30   * @author jdcasey
31   * @author ltheussl
32   * @since 1.1
33   */
34  public class SystemStreamLog
35      implements Log
36  {
37      private static final String EOL = System.getProperty( "line.separator" );
38  
39      private int currentLevel = LEVEL_INFO;
40  
41      /** {@inheritDoc} */
42      public void setLogLevel( int level )
43      {
44          if ( level <= LEVEL_DEBUG )
45          {
46              currentLevel = LEVEL_DEBUG;
47          }
48          else if ( level <= LEVEL_INFO )
49          {
50              currentLevel = LEVEL_INFO;
51          }
52          else if ( level <= LEVEL_WARN )
53          {
54              currentLevel = LEVEL_WARN;
55          }
56          else if ( level <= LEVEL_ERROR )
57          {
58              currentLevel = LEVEL_ERROR;
59          }
60          else
61          {
62              currentLevel = LEVEL_DISABLED;
63          }
64      }
65  
66      /**
67       * {@inheritDoc}
68       *
69       * @param content a {@link java.lang.CharSequence} object.
70       */
71      public void debug( CharSequence content )
72      {
73          if ( isDebugEnabled() )
74          {
75              print( "debug", content );
76          }
77      }
78  
79      /** {@inheritDoc} */
80      public void debug( CharSequence content, Throwable error )
81      {
82          if ( isDebugEnabled() )
83          {
84              print( "debug", content, error );
85          }
86      }
87  
88      /** {@inheritDoc} */
89      public void debug( Throwable error )
90      {
91          if ( isDebugEnabled() )
92          {
93              print( "debug", error );
94          }
95      }
96  
97      /** {@inheritDoc} */
98      public void info( CharSequence content )
99      {
100         if ( isInfoEnabled() )
101         {
102             print( "info", content );
103         }
104     }
105 
106     /** {@inheritDoc} */
107     public void info( CharSequence content, Throwable error )
108     {
109         if ( isInfoEnabled() )
110         {
111             print( "info", content, error );
112         }
113     }
114 
115     /**
116      * {@inheritDoc}
117      *
118      * @param error a {@link java.lang.Throwable} object.
119      */
120     public void info( Throwable error )
121     {
122         if ( isInfoEnabled() )
123         {
124             print( "info", error );
125         }
126     }
127 
128     /**
129      * {@inheritDoc}
130      *
131      * @param content a {@link java.lang.CharSequence} object.
132      */
133     public void warn( CharSequence content )
134     {
135         if ( isWarnEnabled() )
136         {
137             print( "warn", content );
138         }
139     }
140 
141     /** {@inheritDoc} */
142     public void warn( CharSequence content, Throwable error )
143     {
144         if ( isWarnEnabled() )
145         {
146             print( "warn", content, error );
147         }
148     }
149 
150     /** {@inheritDoc} */
151     public void warn( Throwable error )
152     {
153         if ( isWarnEnabled() )
154         {
155             print( "warn", error );
156         }
157     }
158 
159     /** {@inheritDoc} */
160     public void error( CharSequence content )
161     {
162         if ( isErrorEnabled() )
163         {
164             System.err.println( "[error] " + content.toString() );
165         }
166     }
167 
168     /** {@inheritDoc} */
169     public void error( CharSequence content, Throwable error )
170     {
171         if ( isErrorEnabled() )
172         {
173             StringWriter sWriter = new StringWriter();
174             PrintWriter pWriter = new PrintWriter( sWriter );
175 
176             error.printStackTrace( pWriter );
177 
178             System.err.println( "[error] " + content.toString()
179                 + EOL + EOL + sWriter.toString() );
180         }
181     }
182 
183     /**
184      * {@inheritDoc}
185      *
186      * @param error a {@link java.lang.Throwable} object.
187      */
188     public void error( Throwable error )
189     {
190         if ( isErrorEnabled() )
191         {
192             StringWriter sWriter = new StringWriter();
193             PrintWriter pWriter = new PrintWriter( sWriter );
194 
195             error.printStackTrace( pWriter );
196 
197             System.err.println( "[error] " + sWriter.toString() );
198         }
199     }
200 
201     /**
202      * {@inheritDoc}
203      *
204      * @return a boolean.
205      */
206     public boolean isDebugEnabled()
207     {
208         return ( currentLevel <= LEVEL_DEBUG );
209     }
210 
211     /**
212      * {@inheritDoc}
213      *
214      * @return a boolean.
215      */
216     public boolean isInfoEnabled()
217     {
218         return ( currentLevel <= LEVEL_INFO );
219     }
220 
221     /**
222      * {@inheritDoc}
223      *
224      * @return a boolean.
225      */
226     public boolean isWarnEnabled()
227     {
228         return ( currentLevel <= LEVEL_WARN );
229     }
230 
231     /**
232      * {@inheritDoc}
233      *
234      * @return a boolean.
235      */
236     public boolean isErrorEnabled()
237     {
238         return ( currentLevel <= LEVEL_ERROR );
239     }
240 
241       //
242      // private
243     //
244 
245     private void print( String prefix, CharSequence content )
246     {
247         System.out.println( "[" + prefix + "] " + content.toString() );
248     }
249 
250     private void print( String prefix, Throwable error )
251     {
252         StringWriter sWriter = new StringWriter();
253         PrintWriter pWriter = new PrintWriter( sWriter );
254 
255         error.printStackTrace( pWriter );
256 
257         System.out.println( "[" + prefix + "] " + sWriter.toString() );
258     }
259 
260     private void print( String prefix, CharSequence content, Throwable error )
261     {
262         StringWriter sWriter = new StringWriter();
263         PrintWriter pWriter = new PrintWriter( sWriter );
264 
265         error.printStackTrace( pWriter );
266 
267         System.out.println( "[" + prefix + "] " + content.toString()
268             + EOL + EOL + sWriter.toString() );
269     }
270 }