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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
###########################################################################
#
# Caller.pm
#
# Copyright (C) 1999 Raphael Manfredi.
# Copyright (C) 2002-2017 Mark Rogaski, mrogaski@cpan.org;
# all rights reserved.
#
# See the README file included with the
# distribution for license information.
#
##########################################################################
use strict;
########################################################################
package Log::Agent::Tag::Caller;
require Log::Agent::Tag;
use vars qw(@ISA);
@ISA = qw(Log::Agent::Tag);
#
# ->make
#
# Creation routine.
#
# Calling arguments: a hash table list.
#
# The keyed argument list may contain:
# -OFFSET value for the offset attribute [NOT DOCUMENTED]
# -INFO string of keywords like "package filename line subroutine"
# -FORMAT formatting instructions, like "%s:%d", used along with -INFO
# -POSTFIX whether to postfix log message or prefix it.
# -DISPLAY a string like '($subroutine/$line)', supersedes -INFO
# -SEPARATOR separator string to use between tag and message
#
# Attributes:
# indices listref of indices to select in the caller() array
# offset how many stack frames are between us and the caller we trace
# format how to format extracted caller() info
# postfix true if info to append to logged string
#
sub make {
my $self = bless {}, shift;
my (%args) = @_;
$self->{'offset'} = 0;
my $info;
my $postfix = 0;
my $separator;
my %set = (
-offset => \$self->{'offset'},
-info => \$info,
-format => \$self->{'format'},
-postfix => \$postfix,
-display => \$self->{'display'},
-separator => \$separator,
);
while (my ($arg, $val) = each %args) {
my $vset = $set{lc($arg)};
next unless ref $vset;
$$vset = $val;
}
$self->_init("caller", $postfix, $separator);
return $self if $self->display; # A display string takes precedence
#
# pre-process info to compute the indices
#
my $i = 0;
my %indices = map { $_ => $i++ } qw(pac fil lin sub); # abbrevs
my @indices = ();
foreach my $token (split(' ', $info)) {
my $abbr = substr($token, 0, 3);
push(@indices, $indices{$abbr}) if exists $indices{$abbr};
}
$self->{'indices'} = \@indices;
return $self;
}
#
# Attribute access
#
sub offset { $_[0]->{'offset'} }
sub indices { $_[0]->{'indices'} }
sub format { $_[0]->{'format'} }
sub display { $_[0]->{'display'} }
sub postfix { $_[0]->{'postfix'} }
#
# expand_a
#
# Expand the %a macro and return new string.
#
if ($] >= 5.005) { eval q{ # if VERSION >= 5.005
# 5.005 and later version grok /(?<!)/
sub expand_a {
my ($str, $aref) = @_;
$str =~ s/((?<!%)(?:%%)*)%a/join(':', @$aref)/ge;
return $str;
}
}} else { eval q{ # else /* VERSION < 5.005 */
# pre-5.005 does not grok /(?<!)/
sub expand_a {
my ($str, $aref) = @_;
$str =~ s/%%/\01/g;
$str =~ s/%a/join(':', @$aref)/ge;
$str =~ s/\01/%%/g;
return $str;
}
}} # endif /* VERSION >= 5.005 */
#
# ->string -- defined
#
# Compute string with properly formatted caller info
#
sub string {
my $self = shift;
#
# The following code:
#
# sub foo {
# my ($pack, $file, $line, $sub) = caller(0);
# print "excuting $sub called at $file/$line in $pack";
# }
#
# will report who called us, except that $sub will be US, not our CALLER!
# This is an "anomaly" somehow, and therefore to get the routine name
# that called us, we need to move one frame above the ->offset value.
#
my @caller = caller($self->offset);
# Kludge for anomalies in caller()
# Thanks to Jeff Boes for finding the second one!
$caller[3] = (caller($self->offset + 1))[3] || '(main)';
my ($package, $filename, $line, $subroutine) = @caller;
#
# If there is a display, it takes precedence and is formatted accordingly,
# with limited variable substitution. The variables that are recognized
# are:
#
# $package or $pack package name of caller
# $filename or $file filename of caller
# $line line number of caller
# $subroutine or $sub routine name of caller
#
# We recognize both $line and ${line}, the difference being that the
# first needs to be at a word boundary (i.e. $lineage would not result
# in any expansion).
#
# Otherwise, the necessary information is gathered from the caller()
# output, and formatted via sprintf, along with the special %a macro
# which stands for all the information, separated by ':'.
#
# NB: The default format is "[%a]" for postfixed info, "(%a)" otherwise.
#
my $display = $self->display;
if ($display) {
$display =~ s/\$pack(?:age)?\b/$package/g;
$display =~ s/\$\{pack(?:age)?}/$package/g;
$display =~ s/\$file(?:name)?\b/$filename/g;
$display =~ s/\$\{file(?:name)?}/$filename/g;
$display =~ s/\$line\b/$line/g;
$display =~ s/\$\{line}/$line/g;
$display =~ s/\$sub(?:routine)?\b/$subroutine/g;
$display =~ s/\$\{sub(?:routine)?}/$subroutine/g;
} else {
my @show = map { $caller[$_] } @{$self->indices};
my $format = $self->format || ($self->postfix ? "[%a]" : "(%a)");
$format = expand_a($format, \@show); # depends on Perl's version
$display = sprintf $format, @show;
}
return $display;
}
1; # for "require"
__END__
=head1 NAME
Log::Agent::Tag::Caller - formats caller information
=head1 SYNOPSIS
Not intended to be used directly
Inherits from Log::Agent::Tag.
=head1 DESCRIPTION
This class handles caller information for Log::Agent services and is not
meant to be used directly.
This manpage therefore only documents the creation routine parameters
that can be specified at the Log::Agent level via the C<-caller> switch
in the logconfig() routine.
=head1 CALLER INFORMATION ENTITIES
This class knows about four entities: I<package>, I<filename>, I<line>
and I<subroutine>, which are to be understood within the context of the
Log::Agent routine being called (e.g. a logwarn() routine), namely:
=over 4
=item package
This is the package name where the call to the logwarn() routine was made.
It can be specified as "pack" for short, or spelled out completely.
=item filename
This is the file where the call to the logwarn() routine was made.
It can be specified as "file" for short, or spelled out completely.
=item line
This is the line number where the call to the logwarn() routine was made,
in file I<filename>. The name is short enough to be spelled out completely.
=item subroutine
This is the subroutine where the call to the logwarn() routine was made.
If the call is made outside a subroutine, this will be empty.
The name is long enough to warrant the "sub" abbreviation if you don't wish
to spell it out fully.
=back
=head1 CREATION ROUTINE PARAMETERS
The purpose of those parameters is to define how caller information entities
(as defined by the previous section) will be formatted within the log message.
=over 4
=item C<-display> => I<string>
Specifies a string with minimal variable substitution: only the caller
information entities specified above, or their abbreviation, will be
interpolated. For instance:
-display => '($package::$sub/$line)'
Don't forget to use simple quotes to avoid having Perl interpolate those
as variables, or escape their leading C<$> sign otherwise. Using this
convention was deemed to more readable (and natural in Perl)
than SGML entities such as "&pack;".
Using this switch supersedes the C<-info> and C<-format> switches.
=item C<-format> => I<printf format>
Formatting instructions for the caller information entities
listed by the C<-info> switch. For instance:
-format => "%s:%4d"
if you have specified two entities in C<-info>.
The special formatting macro C<%a> stands for all the entities specified
by C<-info> and is rendered by a string where values are separated by ":".
=item C<-info> => I<"space separated list of parameters">
Specifies a list of caller information entities that are to be formatted
using the C<-format> specification. For instance:
-info => "pack sub line"
would only report those three entities.
=item C<-postfix> => I<flag>
Whether the string resulting from the formatting of the caller information
entities should be appended to the regular log message or not
(i.e. prepended, which is the default).
=item C<-separator> => I<string>
The separation string between the tag and the log message.
A single space by default.
=back
=head1 AUTHORS
Raphael Manfredi E<lt>Raphael_Manfredi@pobox.comE<gt> created the module, it
is currently maintained by Mark Rogaski E<lt>mrogaski@cpan.orgE<gt>.
Thanks to Jeff Boes for uncovering wackiness in caller().
=head1 LICENSE
Copyright (C) 1999 Raphael Manfredi.
Copyright (C) 2002 Mark Rogaski; all rights reserved.
See L<Log::Agent(3)> or the README file included with the distribution for
license information.
=head1 SEE ALSO
Log::Agent(3), Log::Agent::Message(3).
=cut
|