1
0
Fork 0
mirror of https://github.com/perlbot/perlbuut synced 2025-06-07 10:35:41 -04:00
perlbuut/lib/Bot/BB3/.svn/text-base/Logger.pm.svn-base
2009-12-05 00:02:04 -05:00

64 lines
1.1 KiB
Text

package Bot::BB3::Logger;
use strict;
use Term::ANSIColor qw/:constants/;
sub import {
my( $class ) = @_;
my $calling_package = caller;
no strict;
for( qw/debug log warn error/ ) {
*{"${calling_package}::$_"} = \&$_;
}
}
# goto &foo; automatically calls foo and passes it @_.
# it also removes the current subroutine from the callstack
# and yes I mostly do it for amusment.
sub debug {
unshift @_, 'debug';
goto &write_message;
}
sub log {
unshift @_, 'log';
goto &write_message;
}
sub warn {
unshift @_, 'warn';
goto &write_message;
}
sub error {
unshift @_, 'error';
goto &write_message;
}
my %COLOR_MAP = (
error => RED,
warn => YELLOW,
log => CYAN,
debug => MAGENTA,
);
sub write_message {
my( $level, @message ) = @_;
my( $package, $filename, $line, $sub ) = caller(1); # Ignore the rest of the args
my $message = "@message";
$sub =~ s/^${package}:://;
my $level_color = $COLOR_MAP{$level};
my $reset = RESET;
my $white = WHITE; # This is actually sort of gray..
# Default output
print STDERR "[$level_color$level$reset] $white$package - $line - $sub$reset: $message\n";
}
1;