Agent/Tag/Caller.pm

filter: Statementstmt 98.4% 63 / 64 Branchbran 64.2% 9 / 14 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod 37.5% 3 / 8 totaltotal 83.6% 87 / 104 CC 10 SCAR 13.0
CC
10
Cov
89%
make
6.0
string
5.1
expand_a
1.0
CRAP 3.7
« Agent/Message.pm ↑ Summary Agent/Channel/Handle.pm »
1 ###########################################################################
2 #
3 # Caller.pm
4 #
5 # Copyright (C) 1999 Raphael Manfredi.
6 # Copyright (C) 2002-2017 Mark Rogaski, mrogaski@cpan.org;
7 # all rights reserved.
8 #
9 # See the README file included with the
10 # distribution for license information.
11 #
12 ##########################################################################
13  
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 ########################################################################
17 package Log::Agent::Tag::Caller;
18  
19 1 require Log::Agent::Tag;
20 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 @ISA = qw(Log::Agent::Tag);
22  
23 #
24 # ->make
25 #
26 # Creation routine.
27 #
28 # Calling arguments: a hash table list.
29 #
30 # The keyed argument list may contain:
31 # -OFFSET value for the offset attribute [NOT DOCUMENTED]
32 # -INFO string of keywords like "package filename line subroutine"
33 # -FORMAT formatting instructions, like "%s:%d", used along with -INFO
34 # -POSTFIX whether to postfix log message or prefix it.
35 # -DISPLAY a string like '($subroutine/$line)', supersedes -INFO
36 # -SEPARATOR separator string to use between tag and message
37 #
38 # Attributes:
39 # indices listref of indices to select in the caller() array
40 # offset how many stack frames are between us and the caller we trace
41 # format how to format extracted caller() info
42 # postfix true if info to append to logged string
43 #
44 3 ▶sub make {
Subroutine make: called CC=6 SCAR=17.9
Pod undocumented
45 3 my $self = bless {}, shift;
46 3 my (%args) = @_;
47  
48 3 $self->{'offset'} = 0;
49  
50 3 my $info;
51 3 my $postfix = 0;
52 3 my $separator;
53  
54 my %set = (
55 -offset => \$self->{'offset'},
56 -info => \$info,
57 -format => \$self->{'format'},
58 -postfix => \$postfix,
59 3 -display => \$self->{'display'},
60 -separator => \$separator,
61 );
62  
63 3 ▶ while (my ($arg, $val) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 9 3 12
64 9 my $vset = $set{lc($arg)};
65 9 ▶ next unless ref $vset;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vset 0 9 9
66 9 $$vset = $val;
67 }
68  
69 3 $self->_init("caller", $postfix, $separator);
70  
71 3 ▶ return $self if $self->display; # A display string takes precedence
Branch: 1 branch
BranchTrueFalseTotal
if $self->display 1 2 3
72  
73 #
74 # pre-process info to compute the indices
75 #
76  
77 2 my $i = 0;
78 2 my %indices = map { $_ => $i++ } qw(pac fil lin sub); # abbrevs
79 2 my @indices = ();
80  
81 2 foreach my $token (split(' ', $info)) {
82 6 my $abbr = substr($token, 0, 3);
83 6 ▶ push(@indices, $indices{$abbr}) if exists $indices{$abbr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $indices{$abbr} 6 0 6
84 }
85  
86 2 $self->{'indices'} = \@indices;
87  
88 2 return $self;
89 }
90  
91 #
92 # Attribute access
93 #
94  
95 14 ▶sub offset { $_[0]->{'offset'} }
Subroutine offset: called CC=1 SCAR=0.0
Pod undocumented
96 5 ▶sub indices { $_[0]->{'indices'} }
Subroutine indices: called CC=1 SCAR=0.0
Pod undocumented
97 5 ▶sub format { $_[0]->{'format'} }
Subroutine format: called CC=1 SCAR=0.0
Pod documented
98 10 ▶sub display { $_[0]->{'display'} }
Subroutine display: called CC=1 SCAR=0.0
Pod undocumented
99 7 ▶sub postfix { $_[0]->{'postfix'} }
Subroutine postfix: called CC=1 SCAR=0.0
Pod documented
100  
101 #
102 # expand_a
103 #
104 # Expand the %a macro and return new string.
105 #
106 5 ▶if ($] >= 5.005) { eval q{ # if VERSION >= 5.005
Subroutine expand_a: called CC=1 SCAR=0.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.005) { } 1 0 1
107  
108 # 5.005 and later version grok /(?<!)/
109 sub expand_a {
110 my ($str, $aref) = @_;
111 $str =~ s/((?<!%)(?:%%)*)%a/join(':', @$aref)/ge;
112 return $str;
113 }
114  
115 0 }} else { eval q{ # else /* VERSION < 5.005 */
116  
117 # pre-5.005 does not grok /(?<!)/
118 sub expand_a {
119 my ($str, $aref) = @_;
120 $str =~ s/%%/\01/g;
121 $str =~ s/%a/join(':', @$aref)/ge;
122 $str =~ s/\01/%%/g;
123 return $str;
124 }
125  
126 }} # endif /* VERSION >= 5.005 */
127  
128 #
129 # ->string -- defined
130 #
131 # Compute string with properly formatted caller info
132 #
133 7 ▶sub string {
Subroutine string: called CC=5 SCAR=16.3
Pod documented
134 7 my $self = shift;
135  
136 #
137 # The following code:
138 #
139 # sub foo {
140 # my ($pack, $file, $line, $sub) = caller(0);
141 # print "excuting $sub called at $file/$line in $pack";
142 # }
143 #
144 # will report who called us, except that $sub will be US, not our CALLER!
145 # This is an "anomaly" somehow, and therefore to get the routine name
146 # that called us, we need to move one frame above the ->offset value.
147 #
148  
149 7 my @caller = caller($self->offset);
150  
151 # Kludge for anomalies in caller()
152 # Thanks to Jeff Boes for finding the second one!
153 7 ▶ $caller[3] = (caller($self->offset + 1))[3] || '(main)';
Condition50%1/2
(caller $self->offset + 1)[3] || '(main)'
l!l
70
MC/DC0%0/1
(caller $self->offset + 1)[3] || '(main)'
(caller $self->offset + 1)[3]
Truth table50%1/2
A
A= (caller $self->offset + 1)[3]
Aresult
11
00
154  
155 7 my ($package, $filename, $line, $subroutine) = @caller;
156  
157 #
158 # If there is a display, it takes precedence and is formatted accordingly,
159 # with limited variable substitution. The variables that are recognized
160 # are:
161 #
162 # $package or $pack package name of caller
163 # $filename or $file filename of caller
164 # $line line number of caller
165 # $subroutine or $sub routine name of caller
166 #
167 # We recognize both $line and ${line}, the difference being that the
168 # first needs to be at a word boundary (i.e. $lineage would not result
169 # in any expansion).
170 #
171 # Otherwise, the necessary information is gathered from the caller()
172 # output, and formatted via sprintf, along with the special %a macro
173 # which stands for all the information, separated by ':'.
174 #
175 # NB: The default format is "[%a]" for postfixed info, "(%a)" otherwise.
176 #
177  
178 7 my $display = $self->display;
179 7 ▶ if ($display) {
Branch: 1 branch
BranchTrueFalseTotal
if ($display) { } 2 5 7
180 2 $display =~ s/\$pack(?:age)?\b/$package/g;
181 2 $display =~ s/\$\{pack(?:age)?}/$package/g;
182 2 $display =~ s/\$file(?:name)?\b/$filename/g;
183 2 $display =~ s/\$\{file(?:name)?}/$filename/g;
184 2 $display =~ s/\$line\b/$line/g;
185 2 $display =~ s/\$\{line}/$line/g;
186 2 $display =~ s/\$sub(?:routine)?\b/$subroutine/g;
187 2 $display =~ s/\$\{sub(?:routine)?}/$subroutine/g;
188 } else {
189 5 my @show = map { $caller[$_] } @{$self->indices};
190 5 ▶ my $format = $self->format || ($self->postfix ? "[%a]" : "(%a)");
Branch: 1 branch
BranchTrueFalseTotal
$self->postfix ? : 0 0 0
Condition33%1/3
$self->format || ($self->postfix ? '[%a]' : '(%a)')
l!l&&r!l&&!r
500
MC/DC0%0/2
$self->format || ($self->postfix ? '[%a]' : '(%a)')
$self->format ($self->postfix ? '[%a]' : '(%a)')
Truth table33%1/3
A || B
A= $self->formatB= ($self->postfix ? '[%a]' : '(%a)')
ABresult
1X1
011
000
191 5 $format = expand_a($format, \@show); # depends on Perl's version
192 5 $display = sprintf $format, @show;
193 }
194  
195 7 return $display;
196 }
197  
198 1 1; # for "require"
199 __END__
« Agent/Message.pm ↑ Summary Agent/Channel/Handle.pm »