/usr/share/perl5/Log/Handler/Output.pm is in liblog-handler-perl 0.82-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | =head1 NAME
Log::Handler::Output - The output builder class.
=head1 DESCRIPTION
Just for internal usage!
=head1 METHODS
=head2 new()
=head2 log()
=head2 reload()
=head2 flush()
=head2 errstr()
=head1 PREREQUISITES
Carp
UNIVERSAL
=head1 AUTHOR
Jonny Schulz <jschulz.cpan(at)bloonix.de>.
=head1 COPYRIGHT
Copyright (C) 2007-2009 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;
our $VERSION = "0.09";
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 $output = $self->{output};
my $message = { };
my $wanted = { message => join(" ", grep defined, @_) };
# The patterns must be generated for each output. The reason
# is that each output can have their own time/date format
# and the code which is executed can return another value.
foreach my $r (@{$self->{wanted_pattern}}) {
$wanted->{$r->{name}} = &{$r->{code}}($self, $level);
}
if ($self->{message_pattern}) {
&{$self->{message_pattern_code}}($wanted, $message);
}
if ($self->{message_layout}) {
&{$self->{message_layout_code}}($wanted, $message);
} else {
$message->{message} = $wanted->{message};
}
if ($self->{message_pattern}) {
if ($message->{message}) {
$wanted->{message} = $message->{message};
}
&{$self->{message_pattern_code}}($wanted, $message);
}
if ($self->{debug_trace} || $Log::Handler::TRACE) {
$self->_add_trace($message);
}
if ($self->{filter_message}) {
$self->_filter_msg($message) or return 1;
}
if ($self->{prepare_message}) {
eval { &{$self->{prepare_message}}($message) };
if ($@) {
return $self->_raise_error("prepare_message failed - $@");
}
}
if ($self->{newline} && $message->{message} !~ /(?:\015|\012)\z/) {
$message->{message} .= "\n";
}
# The substr solution to determine if a newline exists
# at the end of the message is ~60% faster than the regex.
# Maybe it will be released in the future.
#if ($self->{newline}) {
# my $last = substr $message->{message}, -1, 1;
# if ($last eq "\015" || $last eq "\012" || $last eq "\015\012" || $last eq "\012\015") {
# $message->{message} .= "\n";
# }
#}
$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 reload {
my ($self, $opts) = @_;
foreach my $key (keys %$opts) {
$self->{$key} = $opts->{$key};
}
}
sub errstr {
return $ERRSTR;
}
#
# private stuff
#
sub _add_trace {
my ($self, $message) = @_;
my @caller = ();
my $skip = $self->{debug_skip};
if ( $message->{message} =~ /.\z/ ) {
$message->{message} .= "\n";
}
for (my $i=0; my @c = caller($i); $i++) {
my %frame;
@frame{qw/package filename line subroutine hasargs wantarray evaltext is_require/} = @c[0..7];
push @caller, \%frame;
}
foreach my $i (reverse $skip..$#caller) {
$message->{message} .= " " x 3 . "CALL($i):";
my $frame = $caller[$i];
foreach my $key (qw/package filename line subroutine hasargs wantarray evaltext is_require/) {
next unless defined $frame->{$key};
if ($self->{debug_mode} == 1) { # line mode
$message->{message} .= " $key($frame->{$key})";
} elsif ($self->{debug_mode} == 2) { # block mode
$message->{message} .= "\n" . " " x 6 . sprintf("%-12s", $key) . $frame->{$key};
}
}
$message->{message} .= "\n";
}
}
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;
|