1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
=head1 NAME
Log::Handler::Output - The output builder class.
=head1 DESCRIPTION
Just for internal usage!
=head1 METHODS
=head2 new()
=head2 log()
=head2 flush()
=head2 errstr()
=head1 PREREQUISITES
Carp
UNIVERSAL
Devel::Backtrace
=head1 AUTHOR
Jonny Schulz <jschulz.cpan(at)bloonix.de>.
=head1 COPYRIGHT
Copyright (C) 2007 by Jonny Schulz. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
package Log::Handler::Output;
use strict;
use warnings;
use Carp;
use UNIVERSAL;
use Devel::Backtrace;
our $VERSION = '0.04';
our $ERRSTR = '';
sub new {
my ($class, $options, $output) = @_;
my $self = bless $options, $class;
$self->{output} = $output;
return $self;
}
sub log {
my $self = shift;
my $level = shift;
my $wanted = {message=>join(' ', @_)||''};
my $output = $self->{output};
my $pattern = $self->{pattern};
my $message = { };
if ($self->{filter_caller}) {
my $caller = (caller(1+$Log::Handler::CALLER_LEVEL))[0];
return 1 if $caller !~ $self->{filter_caller};
}
foreach my $r (@{$self->{wanted_pattern}}) {
if (ref($r->{code})) {
$wanted->{$r->{name}} = &{$r->{code}}($self, $level);
} else {
$wanted->{$r->{name}} = $r->{code};
}
}
if ($self->{message_layout}) {
&{$self->{message_layout_code}}($wanted, $message);
} else {
$message->{message} = $wanted->{message};
}
if ($self->{message_pattern}) {
&{$self->{message_pattern_code}}($wanted, $message);
}
if ($self->{debug_trace} || $Log::Handler::TRACE) {
$self->_add_trace($message);
} elsif ($self->{newline} && $message->{message} =~ /.\z/) {
$message->{message} .= "\n";
}
if ($self->{filter_message}) {
$self->_filter_msg($message) or return 1;
}
if ($self->{prepare_message}) {
# each output can have an own prepare function. for this reason
# the message is passed as hash and not as a reference.
my $prepare_message = $self->_prepare_message(%$message)
or return $self->_raise_error($output->errstr);
$output->log($prepare_message)
or return $self->_raise_error($output->errstr);
} else {
$output->log($message)
or return $self->_raise_error($output->errstr);
}
return 1;
}
sub flush {
my $self = shift;
my $output = $self->{output};
if ( UNIVERSAL::can($output, 'flush') ) {
$output->flush
or return $self->_raise_error($output->errstr);
}
return 1;
}
sub errstr { $ERRSTR }
#
# private stuff
#
sub _add_trace {
my ($self, $message) = @_;
if ( $message->{message} =~ /.\z/ ) {
$message->{message} .= "\n";
}
my $bt = Devel::Backtrace->new($self->{debug_skip});
foreach my $point (reverse 0..$bt->points-1) {
$message->{message} .= ' ' x 3 . "CALL($point):";
my $caller = $bt->point($point);
foreach my $key (qw/package filename line subroutine hasargs wantarray evaltext is_require/) {
next unless defined $caller->{$key};
if ($self->{debug_mode} == 1) { # line mode
$message->{message} .= " $key($caller->{$key})";
} elsif ($self->{debug_mode} == 2) { # block mode
$message->{message} .= "\n" . ' ' x 6 . sprintf('%-12s', $key) . $caller->{$key};
}
}
$message->{message} .= "\n";
}
}
sub _prepare_message {
my $self = shift;
my $msg = {@_};
my $code = $self->{prepare_message};
eval { &$code($msg) };
return $@ ? $self->_raise_error($@) : $msg;
}
sub _filter_msg {
my ($self, $message) = @_;
my $filter = $self->{filter_message};
my $result = $filter->{result};
my $code = $filter->{code};
my $return = ();
if (!$filter->{condition}) {
$return = &$code($message) || 0;
} else {
foreach my $match ( keys %$result ) {
$result->{$match} =
$message->{message} =~ /$filter->{$match}/ || 0;
}
$return = &$code($result);
}
return $return;
}
sub _raise_error {
my $self = shift;
$ERRSTR = shift;
return undef unless $self->{die_on_errors};
my $class = ref($self);
Carp::croak "$class: $ERRSTR";
}
1;
|