Inspect/Debug.pm

filter: Statementstmt 30.3% 24 / 79 Branchbran 15.3% 8 / 52 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 55.5% 5 / 9 Podpod 100% 4 / 4 totaltotal 26.4% 41 / 155 CC 30 SCAR 47.0
CC
30
Cov
23%
debug
232.4
trace
42.0
import
21.8
CRAP 109.8
« Inspect/L5/Socks.pm ↑ Summary Inspect/L3/IP.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Net::Inspect::Debug;
4  
5 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our @EXPORT = qw(debug trace);
7 1 our @EXPORT_OK = qw($DEBUG $DEBUG_RX %TRACE xdebug xtrace);
8  
9 1 our $DEBUG = 0; # is debugging enabled at all
10 1 our $DEBUG_RX = undef; # debug only packages matching given regex
11 1 my $DEBUG_SUB = undef; # external debug function to call instead of internal
12 1 my $OUTPUT_SUB = undef; # external output function to call instead of internal
13  
14 # import exported stuff, special case var => \$debug, func => \$code
15 sub import {
16 # on default import go immediately to Exporter
17 1 ▶ goto &Exporter::import if @_ == 1;
Subroutine import: called CC=8 SCAR=30.8
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 1 1
18  
19 # extract var => \$var from import and alias it to DEBUG
20 # extract sub => \$code and set $DEBUG_SUB
21 1 ▶ for(my $i=1;$i<@_;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 3 1 4
22 3 ▶ if ( $_[$i] eq 'var' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($_[$i] eq 'var') { } 0 3 3
elsif ($_[$i] eq 'sub') { } 0 3 3
elsif ($_[$i] eq 'output') { } 0 3 3
elsif ($_[$i] =~ /^(?:debug)?(\d+)$/) { } 0 3 3
23 0 *DEBUG = $_[$i+1];
24 0 splice(@_,$i,2);
25 0 $i-=2;
26 } elsif ( $_[$i] eq 'sub' ) {
27 0 $DEBUG_SUB = $_[$i+1];
28 0 splice(@_,$i,2);
29 0 $i-=2;
30 } elsif ( $_[$i] eq 'output' ) {
31 0 $OUTPUT_SUB = $_[$i+1];
32 0 splice(@_,$i,2);
33 0 $i-=2;
34 } elsif ( $_[$i] =~m{^(?:debug)?(\d+)$} ) {
35 0 $DEBUG = $1;
36 0 splice(@_,$i,1);
37 0 $i--;
38 }
39 }
40  
41 # call Exporter only if we have remaining args, because we don't
42 # want to trigger the default export if user gave args (e.g. var,sub)
43 1 ▶ goto &Exporter::import if @_>1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 0 1
44 }
45  
46 # print out debug message or forward to external debug func
47 sub debug {
48 0 ▶ $DEBUG or return;
Subroutine debug: not called CC=16 SCAR=54.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::Debug::DEBUG or return 0 0 0
49 0 my $msg = shift;
50 1 ▶ $msg = do { no warnings; sprintf($msg,@_) } if @_;
Subroutine BEGIN: called CC=1 SCAR=1.7
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
51 0 ▶ if ( $DEBUG_SUB ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG_SUB) 0 0 0
52 0 @_ = ($msg);
53 # goto foreign debug sub
54 # if DEBUG_RX is set this will be done later
55 0 ▶ goto &$DEBUG_SUB if ! $DEBUG_RX;
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::Inspect::Debug::DEBUG_RX 0 0 0
56 }
57  
58 0 my ($pkg,$line) = (caller(0))[0,2];
59 0 ▶ if ( $DEBUG_RX ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Inspect::Debug::DEBUG_RX) 0 0 0
60 0 ▶ $pkg ||= 'main';
Condition0%0/2
$pkg ||= "main"
l!l
00
MC/DC0%0/1
$pkg ||= "main"
$pkg
Truth table0%0/2
A
A= $pkg
Aresult
11
00
61 # does not match wanted package
62 0 ▶ return if $pkg !~ $DEBUG_RX;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg =~ /$Net::Inspect::Debug::DEBUG_RX/ 0 0 0
63 # goto foreign debug sub
64 0 ▶ goto &$DEBUG_SUB if $DEBUG_SUB;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG_SUB 0 0 0
65 }
66  
67 0 my $sub = (caller(1))[3];
68 0 ▶ $sub =~s{^main::}{} if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 0 0 0
69 0 ▶ $sub ||= 'Main';
Condition0%0/2
$sub ||= "Main"
l!l
00
MC/DC0%0/1
$sub ||= "Main"
$sub
Truth table0%0/2
A
A= $sub
Aresult
11
00
70 0 $msg =~ s{
71 (\\)
72 | (\r)
73 | (\n)
74 | (\t)
75 | ([\x00-\x1f\x7f-\xff])
76 }{
77 0 ▶ $1 ? "\\\\" :
Branch: 4 branches
BranchTrueFalseTotal
$1 ? : 0 0 0
$2 ? : 0 0 0
$3 ? : 0 0 0
$4 ? : 0 0 0
78 $2 ? "\\r" :
79 $3 ? "\\n" :
80 $4 ? "\\t" :
81 sprintf("\\x%02x",ord($5))
82 }xesg;
83 0 $msg = "${sub}[$line]: ".$msg;
84  
85 0 ▶ if ( $OUTPUT_SUB ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($OUTPUT_SUB) { } 0 0 0
86 0 $OUTPUT_SUB->( DEBUG => $msg );
87 } else {
88 0 $msg =~s{^}{DEBUG: }mg;
89 0 print STDERR $msg,"\n";
90 }
91 }
92  
93 sub xdebug {
94 0 ▶ $DEBUG or return;
Subroutine xdebug: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::Debug::DEBUG or return 0 0 0
95 0 my $obj = shift;
96 0 my $msg = shift;
97 0 unshift @_,"[$obj] $msg";
98 0 goto &debug;
99 }
100  
101 1 our %TRACE;
102 sub trace {
103 0 ▶ %TRACE or return;
Subroutine trace: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
%Net::Inspect::Debug::TRACE or return 0 0 0
104 0 my $pkg = lc((caller(0))[0]);
105 0 $pkg =~s/.*:://;
106 0 ▶ $TRACE{$pkg} or $TRACE{'*'} or return;
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::Debug::TRACE{$pkg} or $Net::Inspect::Debug::TRACE{"*"} or return 0 0 0
Condition0%0/3
$Net::Inspect::Debug::TRACE{$pkg} or $Net::Inspect::Debug::TRACE{"*"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Net::Inspect::Debug::TRACE{$pkg} or $Net::Inspect::Debug::TRACE{"*"}
$Net::Inspect::Debug::TRACE{$pkg} $Net::Inspect::Debug::TRACE{"*"}
Truth table0%0/3
A or B
A= $Net::Inspect::Debug::TRACE{$pkg}B= $Net::Inspect::Debug::TRACE{"*"}
ABresult
1X1
011
000
107  
108 0 my $msg = shift;
109 0 ▶ $msg = sprintf($msg,@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
110  
111 0 ▶ if ( $OUTPUT_SUB ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($OUTPUT_SUB) { } 0 0 0
112 0 $OUTPUT_SUB->( "TRACE[$pkg]" => $msg );
113 } else {
114 0 $msg =~s{\n}{\n * }g;
115 0 $msg =~s{\A}{[$pkg]: };
116 0 print STDERR $msg,"\n";
117 }
118 }
119  
120 sub xtrace {
121 0 ▶ %TRACE or return;
Subroutine xtrace: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
%Net::Inspect::Debug::TRACE or return 0 0 0
122 0 my $obj = shift;
123 0 my $msg = shift;
124 0 unshift @_, "[$obj] $msg";
125 0 goto &trace;
126 }
127  
128  
129 1 1;
130 __END__
« Inspect/L5/Socks.pm ↑ Summary Inspect/L3/IP.pm »