/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 */
package org.apache.avalon.framework.logger;


/**
 * Logger sending everything to the standard output streams.
 * This is mainly for the cases when you have a utility that 
 * does not have a logger to supply.
 *
 * @author <a href="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
 */
public final class ConsoleLogger implements Logger
{
    private final int logLevel;
    public final static int LEVEL_DEBUG = 0;
    public final static int LEVEL_INFO = 1;
    public final static int LEVEL_WARN = 2;
    public final static int LEVEL_ERROR = 3;
    public final static int LEVEL_FATAL = 4;
    public final static int LEVEL_DISABLED = 5;
    
    /**
     * Creates a new ConsoleLogger with the priority set to DEBUG.
     */
    public ConsoleLogger()
    {        
        this.logLevel = LEVEL_DEBUG;
    }
    
    /**
     * Creates a new ConsoleLogger.
     */
    public ConsoleLogger(final int logLevel)
    {        
        this.logLevel = logLevel;
    }
    
    public void debug(String message)
    {
        if ( this.logLevel <= LEVEL_DEBUG ) 
        {
            System.out.println (message);
        }        
    }
    
    public void debug(String message, Throwable throwable)
    {
        if ( this.logLevel <= LEVEL_DEBUG ) 
        {
            System.out.println( message );
            throwable.printStackTrace( System.out );
        }
    }
    
    public boolean isDebugEnabled()
    {
        return this.logLevel <= LEVEL_DEBUG;
    }
    
    public void info(String message)
    {
        if ( this.logLevel <= LEVEL_INFO ) 
        {
            System.out.println( message );
        }        
    }
    
    public void info(String message, Throwable throwable)
    {
        if ( this.logLevel <= LEVEL_INFO ) 
        {
            System.out.println( message );
            throwable.printStackTrace( System.out );
        }
    }
    
    public boolean isInfoEnabled()
    {
        return this.logLevel <= LEVEL_INFO;
    }
    
    public void warn(String message)
    {
        if ( this.logLevel <= LEVEL_WARN ) 
        {
            System.out.println( message );
        }
    }
    
    public void warn(String message, Throwable throwable)
    {
        if ( this.logLevel <= LEVEL_WARN ) 
        {
            System.out.println( message );
            throwable.printStackTrace( System.out );
        }
    }
    
    public boolean isWarnEnabled()
    {
        return this.logLevel <= LEVEL_WARN;
    }
    
    public void error(String message)
    {
        if ( this.logLevel <= LEVEL_ERROR ) 
        {
            System.out.println( message );
        }
    }
    
    public void error(String message, Throwable throwable)
    {
        if ( this.logLevel <= LEVEL_ERROR ) 
        {
            System.out.println( message );
            throwable.printStackTrace( System.out );
        }
    }
    
    public boolean isErrorEnabled()
    {
        return this.logLevel <= LEVEL_ERROR;
    }
    
    public void fatalError(String message)
    {
        if ( this.logLevel <= LEVEL_FATAL ) 
        {
            System.out.println( message );
        }
    }
    
    public void fatalError(String message, Throwable throwable)
    {
        if ( this.logLevel <= LEVEL_FATAL ) 
        {
            System.out.println( message );
            throwable.printStackTrace( System.out );
        }
    }
    
    public boolean isFatalErrorEnabled()
    {
        return this.logLevel <= LEVEL_FATAL;
    }
    
    public Logger getChildLogger(String name)
    {
        return new ConsoleLogger( this.logLevel );
    }
}
