# 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::Logger;

use strict;
use warnings;


use Term::ANSIColor;
use Term::ANSIColor qw(:constants);
use Logging::Level;
use Logging::LogRecord;

sub new {
    my $class = shift;
    my $self  = bless {}, $class;
    $self->{name}    = shift;
    $self->{manager} = shift;
    $self->filter(undef);
    return $self;
}

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


sub manager {
    my $self    = shift;
    return $self->{manager};
}

sub debug {
    my $self    = shift;
    if ( !$self->filter->hasDebugLevel ) { return; }
    $self->manager->log(new Logging::LogRecord(LEVEL_DEBUG, @_));
}

sub info {
    my $self    = shift;
    if (  !$self->filter->hasInfoLevel) { return; }
    $self->manager->log(new Logging::LogRecord(LEVEL_INFO, @_));
}
 sub trace {
    my $self    = shift;
    if (  !$self->filter->hasTraceLevel) { return; }
    $self->manager->log(new Logging::LogRecord(LEVEL_TRACE, @_));
}
sub progress {
    my $self    = shift;
    if (  !$self->filter->hasProgressLevel) { return; }
    $self->manager->log(new Logging::LogRecord(LEVEL_PROGRESS, @_));
}

sub progressStop {
    my $self    = shift;
    if (  !$self->filter->hasProgressLevel) { return; }
    $self->manager->log(new Logging::LogRecord(LEVEL_PROGRESS, "STOP"));
}

sub severe {
    my $self    = shift;
    $self->validateProcess( 1 );
    if ( !$self->filter->hasSevereLevel) { return; }
    $self->manager->log(new Logging::LogRecord(LEVEL_SEVERE, @_));
}

sub warning {
    my $self    = shift;
    if (  !$self->filter->hasWarningLevel) { return; }
    $self->manager->log(new Logging::LogRecord(LEVEL_WARNING, @_));
}

sub debugArray {
    my $self    = shift;
    if (  !$self->filter->hasDebugLevel) { return; }
    my $name=shift;
    my @list=shift;
    my $count=0;
    for (@list){
        $self->manager->log(new Logging::LogRecord(LEVEL_WARNING, $name."[".$count."] = '".$_."'"));
        $count++;
    }
}
sub debugHash {
    my $self    = shift;
    if (  !$self->filter->hasDebugLevel) { return; }
    my $name=shift;
    my %hash=@_;
    for (keys %hash){
        $self->manager->log(new Logging::LogRecord(LEVEL_WARNING, $name."{'".$_."'} => '".$hash{$_}."'"));
    }
}
sub section {
    my $self    = shift;
    if (  !$self->filter->hasInfoLevel) { return; }
    my $message = shift;
    $self->info(" ");
    my $record=new Logging::LogRecord(LEVEL_INFO, RED."--== ". GREEN. $message. RED. " ==--");
    $self->manager->log($record);
}

sub startProcess {
    my $self=shift;
    $self->manager->{inProcessValidation}=1;
    if ( !$self->filter->hasInfoLevel ) { return; }
        my $message=shift;
    $message=BOLD. WHITE. sprintf( "%-50s", $message);
    my $record=new Logging::LogRecord(LEVEL_DEBUG, $message);
    $record->fragment(1);
    $self->manager->log($record);
}

sub validateProcess {
    my $self      = shift;
    if (!$self->manager->{inProcessValidation}) {
        return;
    }
    if ( ! $self->filter->hasInfoLevel) { return; }
    my $errorCode=shift;
    $self->manager->{inProcessValidation}=0;
    my $data= WHITE. " [";
    if ( !defined $errorCode || $errorCode==0  ) {
        $data.=GREEN. "  OK  ",;
    }
    else {
        $data.=RED. "FAILED",;
    }
    $data.=WHITE. "]";
    
    $self->manager->log(new Logging::LogRecord(LEVEL_INFO, $data));
}

sub status {
    my $self    = shift;
    my $message = shift;
    my $error   = shift;
    
    if ( ! $self->filter->hasInfoLevel) { return; }
    $self->startProcess($message);
    $self->validateProcess( $error, "");
}



1;
