Paranoid/Debug.pm

filter: Statementstmt 89.2% 216 / 242 Branchbran 73.1% 60 / 82 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 93.8% 46 / 49 Podpod 100% 9 / 9 totaltotal 86.5% 342 / 395 CC 45 SCAR 20.6
CC
45
Cov
85%
__ANON__
20.0
pdebug
14.7
_protos
14.7
CRAP 7.8
« Paranoid/Network.pm ↑ Summary Paranoid/IO/FileMultiplexer/Block/StreamHeader.pm »
1 # Paranoid::Debug -- Debug support for paranoid programs
2 #
3 # $Id: lib/Paranoid/Debug.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
4 #
5 # This software is free software. Similar to Perl, you can redistribute it
6 # and/or modify it under the terms of either:
7 #
8 # a) the GNU General Public License
9 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>,
15 #
16 # subject to the following additional term: No trademark rights to
17 # "Paranoid" have been or are conveyed under any of the above licenses.
18 # However, "Paranoid" may be used fairly to describe this unmodified
19 # software, in good faith, but not as a trademark.
20 #
21 # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22 # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23 #
24 #####################################################################
25  
26 #####################################################################
27 #
28 # Environment definitions
29 #
30 #####################################################################
31  
32 package Paranoid::Debug;
33  
34 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 70 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 70 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 70 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 70 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 70 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
42  
43 70 @EXPORT = qw(PDEBUG pdebug pIn pOut subPreamble subPostamble
44 PDEBUG1 PDEBUG2 PDEBUG3 PDEBUG4 PDEBUG5 PDEBUG6 PDEBUG7 PDEBUG8);
45 70 @EXPORT_OK = (
46 @EXPORT,
47 qw(pderror PDPREFIX PDLEVEL1 PDLEVEL2 PDLEVEL3 PDLEVEL4 PDMAXINDENT),
48 );
49 70 %EXPORT_TAGS = (
50 all => [@EXPORT_OK],
51 constants => [
52 qw(PDEBUG1 PDEBUG2 PDEBUG3 PDEBUG4 PDEBUG5 PDEBUG6
53 PDEBUG7 PDEBUG8)
54 ],
55 );
56  
57 70 ▶use constant PDLEVEL1 => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 70 ▶use constant PDLEVEL2 => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 70 ▶use constant PDLEVEL3 => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 70 ▶use constant PDLEVEL4 => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 70 ▶use constant PDMAXIND => 40;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 70 ▶use constant PDMAXSCALAR => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 70 ▶use constant PDEBUG1 => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 70 ▶use constant PDEBUG2 => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 70 ▶use constant PDEBUG3 => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 70 ▶use constant PDEBUG4 => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 70 ▶use constant PDEBUG5 => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 70 ▶use constant PDEBUG6 => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 70 ▶use constant PDEBUG7 => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 70 ▶use constant PDEBUG8 => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 70 ▶use constant CSF_PKG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 70 ▶use constant CSF_FNM => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 70 ▶use constant CSF_LN => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 70 ▶use constant CSF_SUB => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 70 ▶use constant CSF_HAS => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 70 ▶use constant CSF_WNT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 70 ▶use constant CSF_EVL => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 70 ▶use constant CSF_REQ => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 70 ▶use constant CSF_HNT => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 70 ▶use constant CSF_BIT => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 70 ▶use constant CSF_HSH => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 #####################################################################
87 #
88 # Module code follows
89 #
90 #####################################################################
91  
92 {
93 70 my $dlevel = 0; # Start with no indentation
94 70 my $pdebug = 0; # Start with debug output disabled
95 70 my $maxLevel = PDMAXIND; # Start with normal max indentation
96  
97 0 ▶ my $odefprefix = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
98  
99 # Old default Prefix to use with debug messages looks like:
100 #
101 # [PID - $dlevel] Subroutine:
102 #
103 0 my $caller = shift;
104 0 my $indentation = shift;
105 0 my $oi = $indentation;
106 0 my $prefix;
107  
108 # Cap indentation
109 0 ▶ $indentation = $maxLevel if $indentation > $maxLevel;
Branch: 1 branch
BranchTrueFalseTotal
if $indentation > $maxLevel 0 0 0
110  
111 # Construct the prefix
112 0 $prefix = ' ' x $indentation . "[$$-$oi] $caller: ";
113  
114 0 return $prefix;
115 70 };
116 0 ▶ my $defprefix = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
117  
118 # Default Prefix to use with debug messages looks like:
119 #
120 # [PID-level caller]
121 #
122 0 my $caller = shift;
123 0 my $indentation = shift;
124 0 my $prefix = '';
125 0 my $oi = $indentation;
126  
127 # Cap indentation
128 0 ▶ $indentation = ( $maxLevel / 3 ) if ( $indentation * 3 ) > $maxLevel;
Branch: 1 branch
BranchTrueFalseTotal
if $indentation * 3 > $maxLevel 0 0 0
129  
130 # Compose the prefix
131 0 ▶ if ( $indentation == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($indentation == 1) { } 0 0 0
elsif ($indentation > 1) { } 0 0 0
132 0 $prefix = '+> ';
133 } elsif ( $indentation > 1 ) {
134 0 $prefix = '| ' x ( $indentation - 1 ) . '+> ';
135 }
136 0 $prefix .= "[$$-$oi $caller] ";
137  
138 0 return $prefix;
139 70 };
140 70 my $pdprefix = $defprefix;
141  
142 530708 ▶ sub PDEBUG : lvalue {
Subroutine PDEBUG: called CC=1 SCAR=0.0
Pod documented
143 530708 $pdebug;
144 }
145  
146 530708 ▶ sub PDPREFIX : lvalue {
Subroutine PDPREFIX: called CC=1 SCAR=0.0
Pod documented
147 530708 $pdprefix;
148 }
149  
150 0 ▶ sub PDMAXINDENT : lvalue {
Subroutine PDMAXINDENT: not called CC=1 SCAR=6.9
Pod documented
151 0 $maxLevel;
152 }
153  
154 1 ▶ sub pderror ($) {
Subroutine pderror: called CC=1 SCAR=0.0
Pod documented
155  
156 # Purpose: Print passed string to STDERR
157 # Returns: Return value from print function
158 # Usage: $rv = pderror("Foo!");
159  
160 1 my $msg = shift;
161  
162 1 $@ = $msg;
163  
164 1 return print STDERR "$msg\n";
165 }
166  
167 530708 ▶ sub pdebug ($;$@) {
Subroutine pdebug: called CC=14 SCAR=26.9
Pod documented
168  
169 # Purpose: Calls pderror() if the message level is less than or equal
170 # to the value of PDBEBUG, after prepending the string
171 # returned by the PDPREFIX routine, if defined
172 # Returns: Always returns the passed message, regardless of PDEBUG's
173 # value
174 # Usage: pdebug($message, $level);
175  
176 530708 my $msg = shift;
177 530708 ▶ my $level = shift || 1;
Condition100%2/2
shift() || 1
l!l
53068424
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
178 530708 my @pfargs = @_;
179 530708 my $prefix = PDPREFIX;
180 530708 my ( $ci, @crec, $caller, $n, $np );
181  
182 530708 ▶ $msg = '' unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 530708 530708
183  
184 # If called with a negative level it merely means we
185 # need to go a little bit deeper in the call stack to find the
186 # true initiator of the message. This provides the mechanism for
187 # Paranoid::Log::plog to pass indirect debug messages
188 530708 ▶ $ci = $level < 0 ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$level < 0 ? : 394162 136546 530708
189 530708 ▶ $level *= -1 if $level < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $level < 0 394162 136546 530708
190  
191 # Get the call stack info
192 530708 @crec = caller $ci;
193 530708 ▶ $caller =
Branch: 2 branches
BranchTrueFalseTotal
defined $crec[3] ? : 530673 35 530708
defined $crec[1] ? : 0 35 35
194 defined $crec[CSF_SUB] ? $crec[CSF_SUB]
195 : defined $crec[CSF_FNM] ? "$crec[CSF_FNM]/$crec[CSF_LN]"
196 : 'undef';
197  
198 # Filter message through sprintf if args were passed
199 530708 $n = [ $msg =~ m#(%[\w.]+)#sg ];
200 530708 $np = $n = scalar @$n;
201 530708 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 455103 75605 530708
202  
203 # Adjust n upwards if we were given more list items than
204 # we see placeholders for in the messsage string
205 455103 ▶ $n = scalar @pfargs if @pfargs > $n;
Branch: 1 branch
BranchTrueFalseTotal
if @pfargs > $n 155 454948 455103
206  
207 # Make sure the requisite number of args are translated for undefs
208 455103 ▶ while ( $n > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) 768867 455103 1223970
209 768867 $n--;
210 768867 ▶ $pfargs[$n] = 'undef' unless defined $pfargs[$n];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pfargs[$n] 693 768174 768867
211 }
212  
213 # Consolidate extra args into the last placeholder's spot
214 455103 ▶ if ( scalar @pfargs > $np ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pfargs > $np) 155 454948 455103
215 155 $n = $np - 1;
216 155 @pfargs =
217 ( @pfargs[ 0 .. ( $n - 1 ) ], "@pfargs[$n .. $#pfargs]" );
218 }
219  
220 # Filter through sprintf
221 {
222 70 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 455103 $msg = sprintf $msg, @pfargs;
224 }
225 }
226  
227 530708 ▶ return $msg if $level > PDEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $level > PDEBUG() 530708 0 530708
228  
229 # Execute the code block, if that's what it is
230 0 ▶ $prefix = &$prefix( $caller, $dlevel ) if ref($prefix) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $prefix eq "CODE" 0 0 0
231  
232 {
233 0 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.2
234 0 pderror("$prefix$msg");
235 }
236  
237 0 return $msg;
238 }
239  
240 268 ▶ sub pIn () {
Subroutine pIn: called CC=1 SCAR=0.0
Pod documented
241  
242 # Purpose: Increases indentation level
243 # Returns: Always True (1)
244 # Usage: pIn();
245  
246 268 $dlevel++;
247  
248 268 return 1;
249 }
250  
251 154 ▶ sub pOut () {
Subroutine pOut: called CC=1 SCAR=0.0
Pod documented
252  
253 # Purpose: Decreases indentation level
254 # Returns: Always True (1)
255 # Usage: pOut();
256  
257 154 $dlevel--;
258  
259 154 return 1;
260 }
261  
262 70 my %subprotos;
263  
264 394132 ▶ sub _protos ($$) {
Subroutine _protos: called CC=14 SCAR=26.9
265  
266 # Purpose: Converts a string prototype declaration to an array
267 # Returns: Array
268 # Usage: @proto = _protos($caller, $proto);
269  
270 394132 my $caller = shift;
271 394132 my $proto = shift;
272 394132 my ( $t, $p, @rv );
273  
274 394132 ▶ if ( defined $proto and length $proto ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto and length $proto) 378271 15861 394132
Condition66%2/3
defined $proto and length $proto
!ll&&!rl&&r
158610378271
MC/DC50%1/2
defined $proto and length $proto
defined $proto length $proto
Truth table66%2/3
A and B
A= defined $protoB= length $proto
ABresult
0X0
100
111
275  
276 378271 ▶ if ( exists $subprotos{$caller} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $subprotos{$caller}) { } 375823 2448 378271
277  
278 # Return cached values if we have them
279 375823 @rv = @{ $subprotos{$caller} };
280  
281 } else {
282  
283 # Parse and generate list of argument types
284 2448 $p = $proto;
285 2448 ▶ while ( length $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $p) 4199 2448 6647
286 4199 ▶ if ( $p =~ /^\\/s ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($p =~ /^\\/s) { } 232 3967 4199
elsif ($p =~ /^([\$\@%&\*pb])/s) { } 3592 375 3967
elsif ($p =~ /^;/) { } 375 0 375
287  
288 # argument is a reference to... something.
289 232 push @rv, 'ref';
290 232 $p =~ s/^\\(\[[^\]]+\]|.)//s;
291  
292 } elsif ( $p =~ /^([\$@%&\*pb])/s ) {
293  
294 3592 $t = $1;
295 3592 ▶ push @rv,
Branch: 6 branches
BranchTrueFalseTotal
$t eq '$' ? : 3426 166 3592
$t eq 'p' ? : 0 166 166
$t eq 'b' ? : 3 163 166
$t eq '@' ? : 131 32 163
$t eq '%' ? : 32 0 32
$t eq '&' ? : 0 0 0
296 $t eq '$' ? 'scalar'
297 : $t eq 'p' ? 'private'
298 : $t eq 'b' ? 'bytes'
299 : $t eq '@' ? 'array'
300 : $t eq '%' ? 'hash'
301 : $t eq '&' ? 'code'
302 : 'glob';
303 3592 $p =~ s/^.//s;
304  
305 } elsif ( $p =~ /^;/ ) {
306  
307 # Remove the optional delimiter
308 375 $p =~ s/^;//s;
309  
310 } else {
311 0 croak
312 "unknown prototype: $proto (stopped on $p) in\n\t"
313 . join ',',
314 caller 2;
315 }
316 }
317  
318 # Cache results
319 2448 $subprotos{$caller} = [@rv];
320 }
321  
322 }
323  
324 394132 return @rv;
325 }
326  
327 394132 ▶ sub _summarize (\@@) {
Subroutine _summarize: called CC=13 SCAR=25.9
328  
329 # Purpose: Summarizes potentially long types of scalars
330 # Returns: Array
331 # Usage: @args = _summarize(@argt, @args);
332  
333 394132 my $tref = shift;
334 394132 my @args = @_;
335 394132 my ( @tmp, %tmp, $i, $l );
336  
337 70 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
338 70 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
339  
340 # Iterate over args/argt and summarize where appropriate
341 394132 ▶ for ( $i = 0; $i < @args; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @args) 689166 394132 1083298
342 689166 ▶ if ( $$tref[$i] eq 'scalar' and not ref $args[$i] ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($tref->[$i] eq 'scalar' and not ref $args[$i]) { } 593971 95195 689166
elsif ($tref->[$i] eq 'array') { } 744 94451 95195
elsif ($tref->[$i] eq 'hash') { } 576 93875 94451
elsif ($tref->[$i] eq 'private') { } 0 93875 93875
elsif ($tref->[$i] eq 'bytes') { } 62 93813 93875
Condition100%3/3
$tref->[$i] eq "scalar" and not ref $args[$i]
!ll&&!rl&&r
1356881627593971
MC/DC100%2/2
$tref->[$i] eq "scalar" and not ref $args[$i]
$tref->[$i] eq "scalar" not ref $args[$i]
Truth table100%3/3
A and not(B)
A= $tref->[$i] eq "scalar"B= not ref $args[$i]
ABresult
0X0
100
111
343 593971 ▶ if ( defined $args[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[$i]) 473246 120725 593971
344 473246 $l = length $args[$i];
345 473246 ▶ $args[$i] =
Branch: 1 branch
BranchTrueFalseTotal
if $l > 20 2146 471100 473246
346 substr( $args[$i], 0, PDMAXSCALAR ) . "... ($l bytes)"
347 if $l > PDMAXSCALAR;
348 473246 $args[$i] =~ s/\n/\\n/sg;
349 473246 $args[$i] =~ s/[^[:print:]]/./sg;
350 }
351 } elsif ( $$tref[$i] eq 'array' ) {
352 744 @tmp = splice @args, $i;
353 744 $args[$i] = sprintf 'list of %d items', scalar @tmp;
354 } elsif ( $$tref[$i] eq 'hash' ) {
355 576 %tmp = splice @args, $i;
356 576 $args[$i] = sprintf 'hash of %d k/v pairs', scalar keys %tmp;
357 } elsif ( $$tref[$i] eq 'private' ) {
358 0 ▶ if ( defined $args[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[$i]) 0 0 0
359 0 $l = length $args[$i];
360 0 $args[$i] = "REDACTED ($l bytes)";
361 }
362 } elsif ( $$tref[$i] eq 'bytes' ) {
363 62 ▶ if ( defined $args[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args[$i]) 62 0 62
364 62 $l = length $args[$i];
365 62 $args[$i] = "$l bytes";
366 }
367 }
368  
369 689166 ▶ $args[$i] = 'undef' unless defined $args[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args[$i] 121851 567315 689166
370 }
371  
372 394132 return @args;
373 }
374  
375 197011 ▶ sub subPreamble ($;$@) {
Subroutine subPreamble: called CC=2 SCAR=6.9
Pod documented
376  
377 # Purpose: Specialized wrapper meant specifically for use in
378 # functions and methods
379 # Returns: Output of pdebug()
380 # Usage: subPreamble(PDEBUG1, '$$@', @_);
381  
382 197011 my $level = shift;
383 197011 my $proto = shift;
384 197011 my @args = splice @_;
385 197011 my @caller = caller(1);
386 197011 my @argt = _protos $caller[CSF_SUB] . "-pre", $proto;
387 197011 my ( $rv, $msg );
388  
389 70 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
390  
391 # Summarize the args
392 197011 @args = _summarize( @argt, @args );
393  
394 # Print message
395 197011 $msg = 'entering';
396 197011 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 180917 16094 197011
397 180917 $msg .= ' w/' . '(%s)' x ( scalar @args );
398 }
399 197011 $rv = pdebug( $msg, ( $level * -1 ), @args );
400  
401 # Increase indentation level
402 197011 $dlevel++;
403  
404 197011 return $rv;
405 }
406  
407 197121 ▶ sub subPostamble ($$@) {
Subroutine subPostamble: called CC=2 SCAR=6.9
Pod documented
408  
409 # Purpose: Specialized wrapper meant specifically for use in
410 # functions and methods
411 # Returns: Output of pdebug()
412 # Usage: subPostamble(PDEBUG1, '$', $rv);
413  
414 197121 my $level = shift;
415 197121 my $proto = shift;
416 197121 my @args = splice @_;
417 197121 my @caller = caller(1);
418 197121 my @argt = _protos $caller[CSF_SUB] . '-post', $proto;
419 197121 my ( $rv, $msg );
420  
421 # Decrease indentation level
422 197121 $dlevel--;
423  
424 # Summarize the args
425 197121 @args = _summarize( @argt, @args );
426  
427 # Print message
428 197121 $msg = 'leaving w/rv: ';
429 197121 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 197100 21 197121
430 197100 $msg .= '(%s)' x ( scalar @args );
431 }
432 197121 $rv = pdebug( $msg, ( $level * -1 ), @args );
433  
434 197121 return $rv;
435 }
436  
437 }
438  
439 70 1;
440  
441 __END__
« Paranoid/Network.pm ↑ Summary Paranoid/IO/FileMultiplexer/Block/StreamHeader.pm »