| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | if ( $_[$i] eq 'var' ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ( $DEBUG_SUB ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my ($pkg,$line) = (caller(0))[0,2]; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ( $DEBUG_RX ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $pkg ||= 'main'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$pkg ||= "main"
MC/DC0%0/1
$pkg ||= "main"
$pkg
Truth table0%0/2
A
A= $pkg
| |||||||||||||||||||||||||||||
| 61 | # does not match wanted package | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | return if $pkg !~ $DEBUG_RX; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | # goto foreign debug sub | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | goto &$DEBUG_SUB if $DEBUG_SUB; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $sub = (caller(1))[3]; | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | $sub =~s{^main::}{} if $sub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $sub ||= 'Main'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$sub ||= "Main"
MC/DC0%0/1
$sub ||= "Main"
$sub
Truth table0%0/2
A
A= $sub
| |||||||||||||||||||||||||||||
| 70 | 0 | $msg =~ s{ | |||||||||||||||||||||||||||
| 71 | (\\) | ||||||||||||||||||||||||||||
| 72 | | (\r) | ||||||||||||||||||||||||||||
| 73 | | (\n) | ||||||||||||||||||||||||||||
| 74 | | (\t) | ||||||||||||||||||||||||||||
| 75 | | ([\x00-\x1f\x7f-\xff]) | ||||||||||||||||||||||||||||
| 76 | }{ | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $1 ? "\\\\" : | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 104 | 0 | my $pkg = lc((caller(0))[0]); | |||||||||||||||||||||||||||
| 105 | 0 | $pkg =~s/.*:://; | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | $TRACE{$pkg} or $TRACE{'*'} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$Net::Inspect::Debug::TRACE{$pkg} or $Net::Inspect::Debug::TRACE{"*"}
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{"*"}
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my $msg = shift; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | $msg = sprintf($msg,@_) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ( $OUTPUT_SUB ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||