blib/lib/JIP/Debug.pm

filter: Statementstmt 100% 111 / 111 Branchbran 75.0% 21 / 28 Conditioncond 92.8% 13 / 14 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 100% 20 / 20 Podpod 75.0% 6 / 8 totaltotal 94.2% 179 / 190 CC 19 SCAR 13.9
CC
19
Cov
95%
to_debug_count
10.0
send_to_output
3.1
__ANON__
3.0
CRAP 4.0
↑ Summary
1 package JIP::Debug;
2  
3 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Term::ANSIColor 3.00 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Devel::StackTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fcntl qw(LOCK_EX LOCK_UN);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '0.03';
16  
17 1 our @EXPORT_OK = qw(
18 to_debug
19 to_debug_raw
20 to_debug_empty
21 to_debug_count
22 to_debug_trace
23 to_debug_truncate
24 );
25  
26 1 our $HANDLE = \*STDERR;
27  
28 1 our $MSG_FORMAT = qq{%s\n%s\n\n};
29 1 our $MSG_DELIMITER = q{-} x 80;
30 1 our $MSG_EMPTY_LINES = qq{\n} x 18;
31  
32 1 our $DUMPER_INDENT = 1;
33 1 our $DUMPER_DEEPCOPY = 1;
34 1 our $DUMPER_SORTKEYS = 1;
35 1 our $DUMPER_TRAILING_COMMA = 1;
36  
37 1 our %TRACE_PARAMS = (
38 skip_frames => 1, # skip to_debug_trace
39 );
40 1 our %TRACE_AS_STRING_PARAMS;
41  
42 1 our $COLOR = 'bright_green';
43  
44 our $MAYBE_COLORED = sub {
45 16 ▶ my $text = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
46  
47 16 ▶ return defined $text && defined $COLOR
Branch: 1 branch
BranchTrueFalseTotal
defined $text && defined $JIP::Debug::COLOR ? : 10 6 16
Condition100%3/3
defined $text and defined $JIP::Debug::COLOR
!ll&&!rl&&r
4210
MC/DC100%2/2
defined $text and defined $JIP::Debug::COLOR
defined $text defined $JIP::Debug::COLOR
Truth table100%3/3
A and B
A= defined $textB= defined $JIP::Debug::COLOR
ABresult
0X0
100
111
48 ? Term::ANSIColor::colored($text, $COLOR) : $text;
49 1 };
50  
51 our $MAKE_MSG_HEADER = sub {
52 # $MAKE_MSG_HEADER=0, to_debug=1
53 8 ▶ my ($package, undef, $line) = caller 1;
Subroutine __ANON__: called CC=3 SCAR=11.1
54  
55 # $MAKE_MSG_HEADER=0, to_debug=1, subroutine=2
56 8 my $subroutine = (caller 2)[3];
57  
58 8 $subroutine = resolve_subroutine_name($subroutine);
59  
60 8 ▶ my $text = join q{, }, (
Branch: 1 branch
BranchTrueFalseTotal
defined $subroutine ? : 8 0 8
61 sprintf('package=%s', $package),
62 (defined $subroutine ? sprintf('subroutine=%s', $subroutine) : ()),
63 sprintf('line=%d', $line),
64 );
65 8 $text = qq{[$text]:};
66  
67 {
68 8 ▶ my $msg_delimiter = defined $MSG_DELIMITER ? $MSG_DELIMITER : q{};
Branch: 1 branch
BranchTrueFalseTotal
defined $JIP::Debug::MSG_DELIMITER ? : 8 0 8
69 8 $text = sprintf qq{%s\n%s\n%s}, $msg_delimiter, $text, $msg_delimiter;
70 }
71  
72 8 return $MAYBE_COLORED->($text);
73 1 };
74  
75 1 our $NO_LABEL_KEY = '<no label>';
76 1 our %COUNT_OF_LABEL = ($NO_LABEL_KEY => 0);
77  
78 # Supported on Perl 5.22+
79 1 eval {
80 1 require Sub::Util;
81  
82 1 ▶ if (my $set_subname = Sub::Util->can('set_subname')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $set_subname = "Sub::Util"->can("set_subname")) 1 0 1
83 1 $set_subname->('MAYBE_COLORED', $MAYBE_COLORED);
84 1 $set_subname->('MAKE_MSG_HEADER', $MAKE_MSG_HEADER);
85 }
86 };
87  
88 sub to_debug {
89 1 ▶ my $msg_body = do {
Subroutine to_debug: called CC=1 SCAR=0.0
Pod documented
90 1 local $Data::Dumper::Indent = $DUMPER_INDENT;
91 1 local $Data::Dumper::Deepcopy = $DUMPER_DEEPCOPY;
92 1 local $Data::Dumper::Sortkeys = $DUMPER_SORTKEYS;
93 1 local $Data::Dumper::Trailingcomma = $DUMPER_TRAILING_COMMA;
94  
95 1 Dumper(\@ARG);
96 };
97  
98 1 my $msg = sprintf $MSG_FORMAT, $MAKE_MSG_HEADER->(), $msg_body;
99  
100 1 return send_to_output($msg);
101 }
102  
103 sub to_debug_raw {
104 2 ▶ my $msg_text = shift;
Subroutine to_debug_raw: called CC=1 SCAR=0.0
Pod documented
105  
106 2 my $msg = sprintf $MSG_FORMAT, $MAKE_MSG_HEADER->(), $msg_text;
107  
108 2 return send_to_output($msg);
109 }
110  
111 sub to_debug_empty {
112 1 ▶ my $msg = sprintf $MSG_FORMAT, $MAKE_MSG_HEADER->(), $MSG_EMPTY_LINES;
Subroutine to_debug_empty: called CC=1 SCAR=0.0
Pod documented
113  
114 1 return send_to_output($msg);
115 }
116  
117 sub to_debug_count {
118 16 ▶ my ($label, $cb);
Subroutine to_debug_count: called CC=10 SCAR=23.0
Pod documented
119 16 ▶ if (@ARG == 2 && ref $ARG[1] eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (@JIP::Debug::ARG == 2 and ref $JIP::Debug::ARG[1] eq 'CODE') { } 2 14 16
elsif (@JIP::Debug::ARG == 1 and ref $JIP::Debug::ARG[0] eq 'CODE') { } 2 12 14
elsif (@JIP::Debug::ARG == 1) { } 8 4 12
Condition66%2/3
@JIP::Debug::ARG == 2 and ref $JIP::Debug::ARG[1] eq "CODE"
!ll&&!rl&&r
1402
MC/DC50%1/2
@JIP::Debug::ARG == 2 and ref $JIP::Debug::ARG[1] eq "CODE"
@JIP::Debug::ARG == 2 ref $JIP::Debug::ARG[1] eq "CODE"
Truth table66%2/3
A and B
A= @JIP::Debug::ARG == 2B= ref $JIP::Debug::ARG[1] eq "CODE"
ABresult
0X0
100
111
120 2 ($label, $cb) = @ARG;
121 }
122 ▶ elsif (@ARG == 1 && ref $ARG[0] eq 'CODE') {
Condition100%3/3
@JIP::Debug::ARG == 1 and ref $JIP::Debug::ARG[0] eq "CODE"
!ll&&!rl&&r
482
MC/DC100%2/2
@JIP::Debug::ARG == 1 and ref $JIP::Debug::ARG[0] eq "CODE"
@JIP::Debug::ARG == 1 ref $JIP::Debug::ARG[0] eq "CODE"
Truth table100%3/3
A and B
A= @JIP::Debug::ARG == 1B= ref $JIP::Debug::ARG[0] eq "CODE"
ABresult
0X0
100
111
123 2 $cb = $ARG[0];
124 }
125 elsif (@ARG == 1) {
126 8 $label = $ARG[0];
127 }
128  
129 16 ▶ $label = defined $label && length $label ? $label : $NO_LABEL_KEY;
Branch: 1 branch
BranchTrueFalseTotal
defined $label && length $label ? : 8 8 16
Condition100%3/3
defined $label and length $label
!ll&&!rl&&r
718
MC/DC100%2/2
defined $label and length $label
defined $label length $label
Truth table100%3/3
A and B
A= defined $labelB= length $label
ABresult
0X0
100
111
130  
131 16 ▶ my $count = $COUNT_OF_LABEL{$label} || 0;
Condition100%2/2
$JIP::Debug::COUNT_OF_LABEL{$label} || 0
l!l
115
MC/DC100%1/1
$JIP::Debug::COUNT_OF_LABEL{$label} || 0
$JIP::Debug::COUNT_OF_LABEL{$label}
Truth table100%2/2
A
A= $JIP::Debug::COUNT_OF_LABEL{$label}
Aresult
11
00
132 16 $count++;
133 16 $COUNT_OF_LABEL{$label} = $count;
134  
135 16 my $msg_body = sprintf '%s: %d', $label, $count;
136  
137 16 my $msg = sprintf $MSG_FORMAT, $MAKE_MSG_HEADER->(), $msg_body;
138  
139 16 ▶ $cb->($label, $count) if defined $cb;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cb 4 12 16
140  
141 16 return send_to_output($msg);
142 }
143  
144 sub to_debug_trace {
145 1 ▶ my $cb = shift;
Subroutine to_debug_trace: called CC=2 SCAR=7.0
Pod documented
146  
147 1 my $trace = Devel::StackTrace->new(%TRACE_PARAMS);
148  
149 1 my $msg_body = $trace->as_string(%TRACE_AS_STRING_PARAMS);
150 1 $msg_body =~ s{\n+$}{}x;
151  
152 1 my $msg = sprintf $MSG_FORMAT, $MAKE_MSG_HEADER->(), $msg_body;
153  
154 1 ▶ $cb->($trace) if defined $cb;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cb 0 1 1
155  
156 1 return send_to_output($msg);
157 }
158  
159 sub to_debug_truncate {
160 1 ▶ return unless $HANDLE;
Subroutine to_debug_truncate: called CC=2 SCAR=7.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $JIP::Debug::HANDLE 0 1 1
161  
162 1 flock $HANDLE, LOCK_EX;
163 1 truncate $HANDLE, 0;
164 1 flock $HANDLE, LOCK_UN;
165  
166 1 return 1;
167 }
168  
169 sub send_to_output {
170 23 ▶ my $msg = shift;
Subroutine send_to_output: called CC=3 SCAR=11.2
Pod undocumented
171  
172 23 ▶ return unless $HANDLE;
Branch: 1 branch
BranchTrueFalseTotal
unless $JIP::Debug::HANDLE 0 23 23
173  
174 23 flock $HANDLE, LOCK_EX;
175 23 ▶ $HANDLE->print($msg) or croak(sprintf q{Can't write to output: %s}, $OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
$JIP::Debug::HANDLE->print($msg) or croak(sprintf(q[Can't write to output: %s], $JIP::Debug::OS_ERROR)) 0 23 23
176 23 flock $HANDLE, LOCK_UN;
177  
178 23 return 1;
179 }
180  
181 sub resolve_subroutine_name {
182 14 ▶ my $subroutine = shift;
Subroutine resolve_subroutine_name: called CC=2 SCAR=6.9
Pod undocumented
183  
184 14 ▶ return unless defined $subroutine;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subroutine 2 12 14
185  
186 12 my ($subroutine_name) = $subroutine =~ m{::(\w+)$}x;
187  
188 12 return $subroutine_name;
189 }
190  
191 1 1;
192  
193 __END__
↑ Summary