blib/lib/Devel/SimpleTrace.pm

filter: Statementstmt 95.8% 69 / 72 Branchbran 85.7% 24 / 28 Conditioncond 82.3% 14 / 17 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 87.5% 7 / 8 Podpod n/a totaltotal 89.5% 120 / 134 CC 25 SCAR 22.7
CC
25
Cov
91%
_stack_trace
15.1
_do_die
8.2
import
3.1
CRAP 9.6
↑ Summary
1 package Devel::SimpleTrace;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 {
5 6 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 $VERSION = '0.08';
7 }
8  
9 # Install warn() and die() substitutes
10 6 $SIG{'__WARN__'} = \&_do_warn;
11 6 $SIG{'__DIE__' } = \&_do_die;
12  
13 6 my $stderr = '';
14 6 my $in_eval = 0;
15 6 my %options = (
16 showrefs => 0,
17 );
18  
19  
20 #
21 # import()
22 # ------
23 4 ▶sub import {
Subroutine import: called CC=3 SCAR=11.2
24 4 my $class = shift;
25
26 4 for my $opt (@_) {
27 1 ▶ exists $options{$opt} ? $options{$opt} = not $options{$opt}
Branch: 1 branch
BranchTrueFalseTotal
exists $options{$opt} ? : 1 0 1
28 : CORE::warn "warning: Unknown option: $opt\n"
29 }
30 }
31  
32  
33 #
34 # _use_data_dumper()
35 # ----------------
36 1 ▶sub _use_data_dumper {
Subroutine _use_data_dumper: called CC=1 SCAR=0.0
37 1 require Data::Dumper;
38 1 import Data::Dumper;
39 1 $Data::Dumper::Indent = 1; # no fancy indent
40 1 $Data::Dumper::Terse = 1; # don't use $VAR unless needed
41 1 $Data::Dumper::Sortkeys = 1; # sort keys
42 #$Data::Dumper::Deparse = 1; # deparse code refs
43 {
44 1 local $^W = 0;
45 1 ▶ *Devel::SimpleTrace::_use_data_dumper = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
46 }
47 }
48  
49  
50 #
51 # _do_warn()
52 # --------
53 1 ▶sub _do_warn {
Subroutine _do_warn: called CC=2 SCAR=6.9
54 1 local $SIG{'__WARN__'} = 'DEFAULT';
55
56 1 my $msg = join '', @_;
57 1 $msg =~ s/ at (.+?) line (\d+)\.$//;
58 1 $stderr .= $msg;
59 1 ▶ $stderr .= "\n" if substr($msg, -1, 1) ne "\n";
Branch: 1 branch
BranchTrueFalseTotal
if substr($msg, -1, 1) ne "\n" 0 1 1
60
61 1 _stack_trace($1, $2);
62
63 1 print STDERR $stderr;
64 1 $stderr = '';
65 1 $in_eval = 0;
66 }
67  
68  
69 #
70 # _do_die()
71 # -------
72 5 ▶sub _do_die {
Subroutine _do_die: called CC=8 SCAR=21.0
73 5 local $SIG{'__WARN__'} = 'DEFAULT';
74 5 local $SIG{'__DIE__' } = 'DEFAULT';
75
76 5 ▶ CORE::die @_ if ref $_[0] and not $options{showrefs};
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] and not $options{"showrefs"} 1 4 5
Condition100%3/3
ref $_[0] and not $options{"showrefs"}
!ll&&!rl&&r
311
MC/DC100%2/2
ref $_[0] and not $options{"showrefs"}
ref $_[0] not $options{"showrefs"}
Truth table100%3/3
A and not(B)
A= ref $_[0]B= not $options{"showrefs"}
ABresult
0X0
100
111
77 4 ▶ CORE::die @_ if index($_[0], "\n\tat ") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($_[0], "\n\tat ") != -1 2 2 4
78 2 my @args = @_;
79
80 2 ▶ _use_data_dumper() if ref $args[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] 1 1 2
81 2 ▶ my $msg = join '', map { ref $_ ? "Caught exception object: $_\: ".Dumper($_) : $_ } @args;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 1 2
82 2 $msg =~ s/ at (.+?) line (\d+)\.$//;
83 2 $stderr .= $msg;
84 2 ▶ $stderr .= "\n" if substr($msg, -1, 1) ne "\n";
Branch: 1 branch
BranchTrueFalseTotal
if substr($msg, -1, 1) ne "\n" 0 2 2
85
86 2 _stack_trace($1, $2);
87
88 2 ▶ if($in_eval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_eval) { } 2 0 2
89 2 $@ = $stderr;
90 2 $stderr = '';
91 2 $in_eval = 0;
92 2 CORE::die $@
93
94 } else {
95 0 print STDERR $stderr;
96 0 $stderr = '';
97 0 exit -1
98 }
99 }
100  
101  
102 #
103 # _stack_trace()
104 # ------------
105 3 ▶sub _stack_trace {
Subroutine _stack_trace: called CC=15 SCAR=27.1
106 3 my($file,$line) = @_;
107 3 ▶ $file ||= ''; $line ||= '';
Condition100%4/4
$file ||= ""
l!l
21
$line ||= ""
l!l
21
MC/DC100%1/1
$file ||= ""
$file
MC/DC100%1/1
$line ||= ""
$line
Truth table100%2/2
A
A= $file
Aresult
11
00
Truth table100%2/2
A
A= $line
Aresult
11
00
108 3 ▶ $file =~ '(eval \d+)' and $file = '<eval>';
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /(eval \d+)/ and $file = "<eval>" 2 1 3
109
110 3 my $level = 2;
111 3 my @stack = ( ['', $file, $line] ); # @stack = ( [ function, file, line ], ... )
112
113 3 ▶ while(my @context = caller($level++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@context) = caller $level++) 14 3 17
114 14 ▶ $context[1] ||= ''; $context[2] ||= '';
Condition50%2/4
$context[1] ||= ""
l!l
140
$context[2] ||= ""
l!l
140
MC/DC0%0/1
$context[1] ||= ""
$context[1]
MC/DC0%0/1
$context[2] ||= ""
$context[2]
Truth table50%1/2
A
A= $context[1]
Aresult
11
00
Truth table50%1/2
A
A= $context[2]
Aresult
11
00
115 14 ▶ $context[1] =~ '(eval \d+)' and $context[1] = '<eval>' and $in_eval = 1;
Branch: 1 branch
BranchTrueFalseTotal
$context[1] =~ /(eval \d+)/ and $context[1] = "<eval>" and $in_eval = 1 2 12 14
Condition100%2/2
$context[1] =~ /(eval \d+)/ and $context[1] = "<eval>"
!ll
122
MC/DC100%1/1
$context[1] =~ /(eval \d+)/ and $context[1] = "<eval>"
$context[1] =~ /(eval \d+)/
Truth table100%2/2
A
A= $context[1] =~ /(eval \d+)/
Aresult
00
11
116 14 ▶ $context[3] eq '(eval)' and $context[3] = '<eval>' and $in_eval = 1;
Branch: 1 branch
BranchTrueFalseTotal
$context[3] eq "(eval)" and $context[3] = "<eval>" and $in_eval = 1 6 8 14
Condition100%2/2
$context[3] eq "(eval)" and $context[3] = "<eval>"
!ll
86
MC/DC100%1/1
$context[3] eq "(eval)" and $context[3] = "<eval>"
$context[3] eq "(eval)"
Truth table100%2/2
A
A= $context[3] eq "(eval)"
Aresult
00
11
117 14 $stack[-1][0] = $context[3];
118 14 push @stack, [ '', @context[1, 2] ];
119 }
120 3 ▶ $stack[-1][0] = (caller($level-2))[0].'::' || 'main::';
Condition50%1/2
(caller $level - 2)[0] . '::' || 'main::'
l!l
30
MC/DC0%0/1
(caller $level - 2)[0] . '::' || 'main::'
(caller $level - 2)[0] . '::'
Truth table50%1/2
A
A= (caller $level - 2)[0] . '::'
Aresult
11
00
121
122 3 for my $func (@stack) {
123 17 ▶ $$func[1] eq '' and $$func[1] = 'unknown source';
Branch: 1 branch
BranchTrueFalseTotal
$func->[1] eq "" and $func->[1] = "unknown source" 1 16 17
124 17 ▶ $$func[2] and $$func[1] .= ':';
Branch: 1 branch
BranchTrueFalseTotal
$func->[2] and $func->[1] .= ':' 16 1 17
125 17 $stderr .= "\tat $$func[0]($$func[1]$$func[2])\n";
126 }
127 }
128  
129  
130 6 1;
131  
132 __END__
↑ Summary