# This file is part of "KarmaLab.net Perl Library".
#    Copyright 2005, Yoran Brault
#    eMail: yoran.brault@bad_karma-lab.net (remove "bad_" before sending)
#    Site : http://artisan.karma-lab.net
#
# "KarmaLab.net Perl Library" is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# "KarmaLab.net Perl Library" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with "KarmaLab.net Perl Library"; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.

package Logging::LoggerManager;

use strict;
use warnings;

use Logging::Level;
use Logging::Logger;
use Logging::LogFilter;
use Logging::Formaters::ColoredLogFormater;
use Logging::Handlers::ConsoleLogHandler;

my $instance;

sub Instance {
    if ( !defined $instance ) {
        $instance             = {};
        $instance             = bless( $instance, __PACKAGE__ );
        $instance->{filter}   = new Logging::LogFilter( LEVEL_INFO + LEVEL_WARNING + LEVEL_SEVERE  + LEVEL_TRACE + LEVEL_PROGRESS);
        $instance->{formater} = new Logging::Formaters::ColoredLogFormater;
        $instance->{handler}  = new Logging::Handlers::ConsoleLogHandler;
    }
    return $instance;
}

sub logger {
    my $self   = shift;
    my $name   = shift;
    my $logger = new Logging::Logger( $name, $self );
    if ( defined $self->{settings}) {
        my $visibility = $self->{settings}->getValue( __PACKAGE__, $name . ".visibility" );
        if ( defined $visibility ) {
            $logger->filter( new Logging::LogFilter($visibility) );
        }
    }    
    return $logger;
}

sub configure() {
    my $self       = shift;
    my $settings   = shift;
    my $visibility = $settings->getValue( __PACKAGE__, "visibility" );
    if ( defined $visibility ) {
        $self->filter->visibility($visibility);
    }
    $self->{settings} = $settings;
}

sub filter {
    my $self = shift;
    if (@_) { $self->{filter} = shift }
    return $self->{filter};
}

sub formater {
    my $self = shift;
    if (@_) { $self->{formater} = shift }
    return $self->{formater};
}

sub handler {
    my $self = shift;
    if (@_) { $self->{handler} = shift }
    return $self->{handler};
}

# Affichage des message
sub log {
    my $self = shift;
    $self->handler->handle( $self, shift );
}
1;
