# -*- cperl -*-
# -----------------------------------------------------------------------------
# $Id: Logger.pm,v 1.1.1.1 2004/09/27 14:01:00 admin Exp $
# -----------------------------------------------------------------------------
package Celaenito::Logger;
use strict;
use warnings;
use Log::Dispatch::Config;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(logdebug loginfo lognotice logwarn
		 logerror logcrit logalert logemerg);
our $LOGGER;

BEGIN {
    my $cutoff = 15; # パッケージ名がこれ以上長ければ切取る。
    my %expand = (warn => 'warning', crit => 'critical', emerg => 'emergency' );
    foreach my $func (qw[debug info notice warn error crit alert emerg]) {
	my $level = $expand{$func} || $func;
	my $src = qq{
	    sub log$func (\$) {
		my (\$msg) = \@_;
		\$msg =~ s/\\s+\$//s;
		\$msg =~ s/\\n/\\n-- /g;
		
		my (\$pkg) = caller(0);
		if (length(\$pkg) > $cutoff) {
		    \$pkg = '..' . substr \$pkg, - ($cutoff - 2);
		}

                eval {
                    \$LOGGER->log(
                        level   => '$level',
                        message => "[\$pkg] \$msg",
		       );
                };
	    }
	};
	eval $src;
    }
}

sub run {
    Log::Dispatch::Config->configure('log.conf');
    $LOGGER = Log::Dispatch->instance;
}

1;
