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 */
034public class SystemStreamLog
035    implements Log
036{
037    private static final String EOL = System.getProperty( "line.separator" );
038
039    private int currentLevel = LEVEL_INFO;
040
041    /** {@inheritDoc} */
042    public void setLogLevel( int level )
043    {
044        if ( level <= LEVEL_DEBUG )
045        {
046            currentLevel = LEVEL_DEBUG;
047        }
048        else if ( level <= LEVEL_INFO )
049        {
050            currentLevel = LEVEL_INFO;
051        }
052        else if ( level <= LEVEL_WARN )
053        {
054            currentLevel = LEVEL_WARN;
055        }
056        else if ( level <= LEVEL_ERROR )
057        {
058            currentLevel = LEVEL_ERROR;
059        }
060        else
061        {
062            currentLevel = LEVEL_DISABLED;
063        }
064    }
065
066    /**
067     * {@inheritDoc}
068     *
069     * @param content a {@link java.lang.CharSequence} object.
070     */
071    public void debug( CharSequence content )
072    {
073        if ( isDebugEnabled() )
074        {
075            print( "debug", content );
076        }
077    }
078
079    /** {@inheritDoc} */
080    public void debug( CharSequence content, Throwable error )
081    {
082        if ( isDebugEnabled() )
083        {
084            print( "debug", content, error );
085        }
086    }
087
088    /** {@inheritDoc} */
089    public void debug( Throwable error )
090    {
091        if ( isDebugEnabled() )
092        {
093            print( "debug", error );
094        }
095    }
096
097    /** {@inheritDoc} */
098    public void info( CharSequence content )
099    {
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}