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