001package org.apache.maven.doxia.logging;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.PrintWriter;
023import java.io.StringWriter;
024
025/**
026 * Logger with "standard" output and error output stream. The log prefix is voluntarily in lower case.
027 * <br>
028 * Based on <code>org.apache.maven.plugin.logging.SystemStreamLog</code>.
029 *
030 * @author jdcasey
031 * @author ltheussl
032 * @since 1.1
033 */
034@Deprecated
035public class SystemStreamLog
036    implements Log
037{
038    private static final String EOL = System.getProperty( "line.separator" );
039
040    private int currentLevel = LEVEL_INFO;
041
042    /** {@inheritDoc} */
043    public void setLogLevel( int level )
044    {
045        if ( level <= LEVEL_DEBUG )
046        {
047            currentLevel = LEVEL_DEBUG;
048        }
049        else if ( level <= LEVEL_INFO )
050        {
051            currentLevel = LEVEL_INFO;
052        }
053        else if ( level <= LEVEL_WARN )
054        {
055            currentLevel = LEVEL_WARN;
056        }
057        else if ( level <= LEVEL_ERROR )
058        {
059            currentLevel = LEVEL_ERROR;
060        }
061        else
062        {
063            currentLevel = LEVEL_DISABLED;
064        }
065    }
066
067    /**
068     * {@inheritDoc}
069     *
070     * @param content a {@link java.lang.CharSequence} object.
071     */
072    public void debug( CharSequence content )
073    {
074        if ( isDebugEnabled() )
075        {
076            print( "debug", content );
077        }
078    }
079
080    /** {@inheritDoc} */
081    public void debug( CharSequence content, Throwable error )
082    {
083        if ( isDebugEnabled() )
084        {
085            print( "debug", content, error );
086        }
087    }
088
089    /** {@inheritDoc} */
090    public void debug( Throwable error )
091    {
092        if ( isDebugEnabled() )
093        {
094            print( "debug", error );
095        }
096    }
097
098    /** {@inheritDoc} */
099    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}