| 1 | package JIP::Debug; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Term::ANSIColor 3.00 (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Devel::StackTrace; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||
|
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.032'; | |||||||||||||||||||||||||||||||||||
| 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 | 16 | ▶ | our $MAYBE_COLORED = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||
| 45 | 16 | my ($text) = @ARG; | |||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||
| 47 | 16 | ▶ | return defined $text && defined $COLOR ? Term::ANSIColor::colored( $text, $COLOR ) : $text; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $text and defined $JIP::Debug::COLOR
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
| |||||||||||||||||||||||||||||||||||||
| 48 | 1 | }; | |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 8 | ▶ | our $MAKE_MSG_HEADER = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||
| 51 | # $MAKE_MSG_HEADER=0, to_debug=1 | ||||||||||||||||||||||||||||||||||||
| 52 | 8 | my ( $package, undef, $line ) = caller 1; | |||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||
| 54 | # $MAKE_MSG_HEADER=0, to_debug=1, subroutine=2 | ||||||||||||||||||||||||||||||||||||
| 55 | 8 | my $subroutine = ( caller 2 )[3]; | |||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||
| 57 | 8 | $subroutine = resolve_subroutine_name($subroutine); | |||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | 8 | ▶ | my $text = join q{, }, ( | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 60 | 'package=' . $package, | ||||||||||||||||||||||||||||||||||||
| 61 | ( defined $subroutine ? ( 'subroutine=' . $subroutine ) : () ), | ||||||||||||||||||||||||||||||||||||
| 62 | 'line=' . $line, | ||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||
| 64 | 8 | $text = sprintf '[%s]:', $text; | |||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||||||||||
| 67 | 8 | ▶ | my $msg_delimiter = defined $MSG_DELIMITER ? $MSG_DELIMITER : q{}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 68 | 8 | $text = sprintf qq{%s\n%s\n%s}, $msg_delimiter, $text, $msg_delimiter; | |||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||
| 71 | 8 | return $MAYBE_COLORED->($text); | |||||||||||||||||||||||||||||||||||
| 72 | 1 | }; | |||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||
| 74 | 1 | our $NO_LABEL_KEY = '<no label>'; | |||||||||||||||||||||||||||||||||||
| 75 | 1 | our %COUNT_OF_LABEL = ( $NO_LABEL_KEY => 0 ); | |||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | # Supported on Perl 5.22+ | ||||||||||||||||||||||||||||||||||||
| 78 | 1 | eval { | |||||||||||||||||||||||||||||||||||
| 79 | 1 | require Sub::Util; | |||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||
| 81 | 1 | ▶ | if ( my $set_subname = Sub::Util->can('set_subname') ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 82 | 1 | $set_subname->( 'MAYBE_COLORED', $MAYBE_COLORED ); | |||||||||||||||||||||||||||||||||||
| 83 | 1 | $set_subname->( 'MAKE_MSG_HEADER', $MAKE_MSG_HEADER ); | |||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||
| 87 | 1 | ▶ | sub to_debug { | ||||||||||||||||||||||||||||||||||
|
Subroutine
to_debug: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 88 | 1 | my $msg_body; | |||||||||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||||||||||
| 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 | $msg_body = 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 | 2 | ▶ | sub to_debug_raw { | ||||||||||||||||||||||||||||||||||
|
Subroutine
to_debug_raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 104 | 2 | my ($msg_text) = @ARG; | |||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | 2 | my $msg = sprintf $MSG_FORMAT, $MAKE_MSG_HEADER->(), $msg_text; | |||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | 2 | return send_to_output($msg); | |||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||
| 111 | 1 | ▶ | sub to_debug_empty { | ||||||||||||||||||||||||||||||||||
|
Subroutine
to_debug_empty: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 112 | 1 | my $msg = sprintf $MSG_FORMAT, $MAKE_MSG_HEADER->(), $MSG_EMPTY_LINES; | |||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | 1 | return send_to_output($msg); | |||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||
| 117 | 16 | ▶ | sub to_debug_count { | ||||||||||||||||||||||||||||||||||
|
Subroutine
to_debug_count: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 118 | 16 | my ( $label, $cb ); | |||||||||||||||||||||||||||||||||||
| 119 | 16 | ▶ | if ( @ARG == 2 && ref $ARG[1] eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
@JIP::Debug::ARG == 2 and ref $JIP::Debug::ARG[1] eq "CODE"
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"
| |||||||||||||||||||||||||||||||||||||
| 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"
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"
| |||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $label and length $label
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
| |||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | 16 | ▶ | my $count = $COUNT_OF_LABEL{$label} || 0; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$JIP::Debug::COUNT_OF_LABEL{$label} || 0
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}
| |||||||||||||||||||||||||||||||||||||
| 132 | 16 | $count += 1; | |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||
| 141 | 16 | return send_to_output($msg); | |||||||||||||||||||||||||||||||||||
| 142 | } ## end sub to_debug_count | ||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||
| 144 | 1 | ▶ | sub to_debug_trace { | ||||||||||||||||||||||||||||||||||
|
Subroutine
to_debug_trace: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 145 | 1 | my ($cb) = @ARG; | |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||
| 156 | 1 | return send_to_output($msg); | |||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||
| 159 | 1 | ▶ | sub to_debug_truncate { | ||||||||||||||||||||||||||||||||||
|
Subroutine
to_debug_truncate: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | return if !$HANDLE; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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 | 23 | ▶ | sub send_to_output { | ||||||||||||||||||||||||||||||||||
|
Subroutine
send_to_output: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 170 | 23 | my ($msg) = @ARG; | |||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||
| 172 | 23 | ▶ | return if !$HANDLE; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||
| 174 | 23 | flock $HANDLE, LOCK_EX; | |||||||||||||||||||||||||||||||||||
| 175 | 23 | ▶ | $HANDLE->print($msg) || croak( q{Can't write to output: } . $OS_ERROR ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 176 | 23 | flock $HANDLE, LOCK_UN; | |||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||
| 178 | 23 | return 1; | |||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||
| 181 | 14 | ▶ | sub resolve_subroutine_name { | ||||||||||||||||||||||||||||||||||
|
Subroutine
resolve_subroutine_name: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 182 | 14 | my ($subroutine) = @ARG; | |||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||
| 184 | 14 | ▶ | return if !defined $subroutine; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||
| 186 | 12 | my ($subroutine_name) = $subroutine =~ m{::(\w+)$}x; | |||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||
| 188 | 12 | return $subroutine_name; | |||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | 1 | 1; | |||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||||||||||