| 1 | #======================================================================== | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Badger::Debug | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||
| 6 | # Mixin module implementing functionality for debugging. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # AUTHOR | ||||||||||||||||||||||||||||
| 9 | # Andy Wardley <abw@wardley.org> | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | #======================================================================== | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package Badger::Debug; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 70 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | use Badger::Rainbow | ||||||||||||||||||||||||||||
| 17 | 70 | ▶ | ANSI => 'bold red yellow green cyan white'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 70 | ▶ | use Scalar::Util qw( blessed refaddr ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | use Badger::Class | ||||||||||||||||||||||||||||
| 20 | 70 | base => 'Badger::Exporter', | |||||||||||||||||||||||||||
| 21 | version => 0.01, | ||||||||||||||||||||||||||||
| 22 | constants => 'PKG REFS SCALAR ARRAY HASH CODE REGEX DELIMITER', | ||||||||||||||||||||||||||||
| 23 | words => 'DEBUG', | ||||||||||||||||||||||||||||
| 24 | import => 'class', | ||||||||||||||||||||||||||||
| 25 | constant => { | ||||||||||||||||||||||||||||
| 26 | UNDEF => '<undef>', | ||||||||||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||||||||||
| 28 | exports => { | ||||||||||||||||||||||||||||
| 29 | tags => { | ||||||||||||||||||||||||||||
| 30 | debug => 'debugging debug debugf debug_up debug_at debug_caller | ||||||||||||||||||||||||||||
| 31 | debug_callers debug_args', | ||||||||||||||||||||||||||||
| 32 | dump => 'dump dump_data dump_data_inline | ||||||||||||||||||||||||||||
| 33 | dump_ref dump_hash dump_list dump_text' | ||||||||||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||||||||||
| 35 | hooks => { | ||||||||||||||||||||||||||||
| 36 | color => \&enable_colour, | ||||||||||||||||||||||||||||
| 37 | colour => \&enable_colour, | ||||||||||||||||||||||||||||
| 38 | dumps => [\&_export_debug_dumps, 1], # expects 1 arguments | ||||||||||||||||||||||||||||
| 39 | default => [\&_export_debug_default, 1], | ||||||||||||||||||||||||||||
| 40 | modules => [\&_export_debug_modules, 1], | ||||||||||||||||||||||||||||
| 41 | 'DEBUG' => [\&_export_debug_constant, 1], | ||||||||||||||||||||||||||||
| 42 | '$DEBUG' => [\&_export_debug_variable, 1], | ||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||
| 44 | 70 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 70 | our $PAD = ' '; | |||||||||||||||||||||||||||
| 47 | 70 | our $MAX_TEXT = 48; | |||||||||||||||||||||||||||
| 48 | 70 | our $MAX_DEPTH = 3; # prevent runaways in debug/dump | |||||||||||||||||||||||||||
| 49 | 70 | ▶ | our $FORMAT = "[<where> line <line>]\n<msg>" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | unless defined $FORMAT; | ||||||||||||||||||||||||||||
| 51 | 70 | ▶ | our $PROMPT = '> ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | unless defined $PROMPT; | ||||||||||||||||||||||||||||
| 53 | 70 | our $MESSAGE = "$PROMPT%s"; | |||||||||||||||||||||||||||
| 54 | 70 | our $HIDE_UNDER = 1; | |||||||||||||||||||||||||||
| 55 | 70 | our $CALLER_UP = 0; # hackola to allow debug() to use a different caller | |||||||||||||||||||||||||||
| 56 | 70 | our $CALLER_AT = { }; # ditto | |||||||||||||||||||||||||||
| 57 | 70 | our $DUMPING = { }; | |||||||||||||||||||||||||||
| 58 | 70 | ▶ | our $DEBUG = 0 unless defined $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 70 | our $DUMP_METHOD = 'dump'; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 62 | # export hooks | ||||||||||||||||||||||||||||
| 63 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub _export_debug_dumps { | ||||||||||||||||||||||||||||
| 66 | 141 | ▶ | my ($self, $target, $symbol, $value, $symbols) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_export_debug_dumps: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | $self->export_symbol($target, dumper => sub { | ||||||||||||||||||||||||||||
| 68 | 1 | ▶ | $_[0]->dump_hash($_[0],$_[1],$value); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 141 | }); | |||||||||||||||||||||||||||
| 70 | 141 | unshift(@$symbols, ':dump'); | |||||||||||||||||||||||||||
| 71 | 141 | return $self; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub _export_debug_default { | ||||||||||||||||||||||||||||
| 76 | 1255 | ▶ | my ($self, $target, $symbol, $value, $symbols) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_export_debug_default: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 1255 | unshift( | |||||||||||||||||||||||||||
| 78 | @$symbols, | ||||||||||||||||||||||||||||
| 79 | '$DEBUG' => $value, | ||||||||||||||||||||||||||||
| 80 | 'DEBUG' => $value, | ||||||||||||||||||||||||||||
| 81 | 'debug', | ||||||||||||||||||||||||||||
| 82 | 'debugging' | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | 1255 | return $self; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub _export_debug_variable { | ||||||||||||||||||||||||||||
| 89 | 1257 | ▶ | my ($self, $target, $symbol, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_export_debug_variable: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 90 | 70 | ▶ | no strict REFS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # use any existing value in $DEBUG | ||||||||||||||||||||||||||||
| 93 | 3 | $value = ${ $target.PKG.DEBUG } | |||||||||||||||||||||||||||
| 94 | 1257 | ▶ | if defined ${ $target.PKG.DEBUG }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1257 | ▶ | $self->debug("$symbol option setting $target \$DEBUG to $value\n") if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1257 | *{ $target.PKG.DEBUG } = \$value; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub _export_debug_constant { | ||||||||||||||||||||||||||||
| 102 | 1257 | ▶ | my ($self, $target, $symbol, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_export_debug_constant: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 103 | 70 | ▶ | no strict REFS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # use any existing value in $DEBUG | ||||||||||||||||||||||||||||
| 106 | 1256 | $value = ${ $target.PKG.DEBUG } | |||||||||||||||||||||||||||
| 107 | 1257 | ▶ | if defined ${ $target.PKG.DEBUG }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1257 | ▶ | $self->debug("$symbol option setting $target DEBUG to $value\n") if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 1257 | my $temp = $value; # make sure this is a const sub on 5.22 | |||||||||||||||||||||||||||
| 111 | 1257 | ▶ | *{ $target.PKG.DEBUG } = sub () { $temp }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub _export_debug_modules { | ||||||||||||||||||||||||||||
| 116 | 2 | ▶ | my ($self, $target, $symbol, $modules) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_export_debug_modules: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | 2 | $self->debug_modules($modules); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 122 | # exportable debugging methods | ||||||||||||||||||||||||||||
| 123 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub debugging { | ||||||||||||||||||||||||||||
| 126 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
debugging: called CC=7 SCAR=20.0
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 15 | ▶ | my $pkg = ref $self || $self; | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 128 | 70 | ▶ | no strict REFS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # return current $DEBUG value when called without args | ||||||||||||||||||||||||||||
| 131 | 15 | ▶ | return ${ $pkg.PKG.DEBUG } || 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
${$pkg . '::' . 'DEBUG';} || 0
MC/DC100%1/1
${$pkg . '::' . 'DEBUG';} || 0
${$pkg . '::' . 'DEBUG';}
Truth table100%2/2
A
A= ${$pkg . '::' . 'DEBUG';}
| |||||||||||||||||||||||||||||
| 132 | unless @_; | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # set new debug value when called with an argument | ||||||||||||||||||||||||||||
| 135 | 8 | my $debug = shift; | |||||||||||||||||||||||||||
| 136 | 8 | ▶ | $debug = 0 if $debug =~ /^off$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # TODO: consider setting different parts of the flag, like TT2, | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 8 | ▶ | $self->debug("debugging() Setting $pkg debug to $debug\n") if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 8 | ▶ | if (defined ${ $pkg.PKG.DEBUG }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | # update existing variable | ||||||||||||||||||||||||||||
| 144 | 8 | ${ $pkg.PKG.DEBUG } = $debug; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||
| 147 | # define new variable, poking it into the symbol table using | ||||||||||||||||||||||||||||
| 148 | # *{...} rather than ${...} so that it's visible at compile time, | ||||||||||||||||||||||||||||
| 149 | # thus preventing any "Variable $DEBUG not defined errors | ||||||||||||||||||||||||||||
| 150 | 0 | *{ $pkg.PKG.DEBUG } = \$debug; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 8 | return $debug; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub debug { | ||||||||||||||||||||||||||||
| 157 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
debug: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 9 | ▶ | my $msg = join('', @_), | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 159 | my $class = ref $self || $self; | ||||||||||||||||||||||||||||
| 160 | 9 | ▶ | my $format = $CALLER_AT->{ format } || $FORMAT; | ||||||||||||||||||||||||||
|
Condition66%2/3
$Badger::Debug::CALLER_AT->{'format'} || $Badger::Debug::FORMAT
MC/DC0%0/2
$Badger::Debug::CALLER_AT->{'format'} || $Badger::Debug::FORMAT
$Badger::Debug::CALLER_AT->{'format'}
$Badger::Debug::FORMAT
Truth table66%2/3
A || B
A= $Badger::Debug::CALLER_AT->{'format'}B= $Badger::Debug::FORMAT
| |||||||||||||||||||||||||||||
| 161 | 9 | my ($pkg, $file, $line) = caller($CALLER_UP); | |||||||||||||||||||||||||||
| 162 | 9 | my (undef, undef, undef, $sub) = caller($CALLER_UP + 1); | |||||||||||||||||||||||||||
| 163 | 9 | ▶ | if (defined $sub) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 2 | $sub =~ s/.*?([^:]+)$/::$1()/; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||
| 167 | 7 | $sub = ''; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 9 | ▶ | my $where = ($class eq $pkg) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | ? $class . $sub | ||||||||||||||||||||||||||||
| 171 | : $pkg . $sub . " ($class)"; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 9 | $msg = join("\n", map { sprintf($MESSAGE, $_) } split("\n", $msg)); | |||||||||||||||||||||||||||
| 174 | # $msg =~ s/^/$PROMPT/gm; | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # We load this dynamically because it uses Badger::Debug and we don't | ||||||||||||||||||||||||||||
| 177 | # want to end up in a gruesome birth spiral | ||||||||||||||||||||||||||||
| 178 | 9 | require Badger::Timestamp; | |||||||||||||||||||||||||||
| 179 | 9 | my $now = Badger::Timestamp->now; | |||||||||||||||||||||||||||
| 180 | 9 | my $data = { | |||||||||||||||||||||||||||
| 181 | msg => $msg, | ||||||||||||||||||||||||||||
| 182 | where => $where, | ||||||||||||||||||||||||||||
| 183 | class => $class, | ||||||||||||||||||||||||||||
| 184 | file => $file, | ||||||||||||||||||||||||||||
| 185 | line => $line, | ||||||||||||||||||||||||||||
| 186 | pkg => $pkg, | ||||||||||||||||||||||||||||
| 187 | sub => $sub, | ||||||||||||||||||||||||||||
| 188 | date => $now->date, | ||||||||||||||||||||||||||||
| 189 | time => $now->time, | ||||||||||||||||||||||||||||
| 190 | pid => $$, | ||||||||||||||||||||||||||||
| 191 | %$CALLER_AT, | ||||||||||||||||||||||||||||
| 192 | }; | ||||||||||||||||||||||||||||
| 193 | 9 | ▶ | $format =~ s/<(\w+)>/defined $data->{ $1 } ? $data->{ $1 } : "<$1 undef>"/eg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 9 | ▶ | $format .= "\n" unless $format =~ /\n$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 9 | print STDERR $format; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | sub debugf { | ||||||||||||||||||||||||||||
| 201 | 1 | ▶ | local $CALLER_UP = 1; | ||||||||||||||||||||||||||
|
Subroutine
debugf: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 202 | 1 | shift->debug( sprintf(shift, @_) ); | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | sub debug_up { | ||||||||||||||||||||||||||||
| 207 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
debug_up: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | 0 | local $CALLER_UP = shift; | |||||||||||||||||||||||||||
| 209 | 0 | $self->debug(@_); | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | sub debug_at { | ||||||||||||||||||||||||||||
| 214 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
debug_at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 215 | 3 | local $CALLER_AT = shift; | |||||||||||||||||||||||||||
| 216 | 3 | local $CALLER_UP = 1; | |||||||||||||||||||||||||||
| 217 | 3 | $self->debug(@_); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | sub debug_caller { | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
debug_caller: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 223 | 0 | my ($pkg, $file, $line, $sub) = caller(1); | |||||||||||||||||||||||||||
| 224 | 0 | my $msg = "$sub called from "; | |||||||||||||||||||||||||||
| 225 | 0 | ($pkg, undef, undef, $sub) = caller(2); | |||||||||||||||||||||||||||
| 226 | 0 | $msg .= "$sub in $file at line $line\n"; | |||||||||||||||||||||||||||
| 227 | 0 | $self->debug($msg); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | sub debug_callers { | ||||||||||||||||||||||||||||
| 232 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
debug_callers: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 0 | my $msg = ''; | |||||||||||||||||||||||||||
| 234 | 0 | my $i = 1; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | while (1) { | |||||||||||||||||||||||||||
| 237 | 0 | my @info = caller($i); | |||||||||||||||||||||||||||
| 238 | 0 | ▶ | last unless @info; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 0 | my ($pkg, $file, $line, $sub) = @info; | |||||||||||||||||||||||||||
| 240 | 0 | $msg .= sprintf( | |||||||||||||||||||||||||||
| 241 | "%4s: Called from %s in %s at line %s\n", | ||||||||||||||||||||||||||||
| 242 | '#' . $i++, $sub, $file, $line | ||||||||||||||||||||||||||||
| 243 | ); | ||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | 0 | $self->debug($msg); | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | sub debug_args { | ||||||||||||||||||||||||||||
| 250 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
debug_args: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 251 | $self->debug_up( | ||||||||||||||||||||||||||||
| 252 | 2, "args: ", | ||||||||||||||||||||||||||||
| 253 | 0 | join(', ', map { $self->dump_data_inline($_) } @_), | |||||||||||||||||||||||||||
| 254 | "\n" | ||||||||||||||||||||||||||||
| 255 | ); | ||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | sub debug_modules { | ||||||||||||||||||||||||||||
| 260 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
debug_modules: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 261 | 2 | ▶ | my $modules = @_ == 1 ? shift : [ @_ ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | 2 | my $debug = 1; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 2 | ▶ | $modules = [ split(DELIMITER, $modules) ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | unless ref $modules eq ARRAY; | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | # TODO: handle other refs? | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 2 | foreach my $pkg (@$modules) { | |||||||||||||||||||||||||||
| 270 | 70 | ▶ | no strict REFS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 271 | 2 | *{ $pkg.PKG.DEBUG } = \$debug; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 277 | # data dumping methods | ||||||||||||||||||||||||||||
| 278 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | sub dump { | ||||||||||||||||||||||||||||
| 281 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 282 | 3 | my $code = $self->can('dumper'); | |||||||||||||||||||||||||||
| 283 | 3 | ▶ | return $code | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | ? $code->($self, @_) | ||||||||||||||||||||||||||||
| 285 | : $self->dump_ref($self, @_); | ||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | sub dump_data { | ||||||||||||||||||||||||||||
| 290 | 0 | ▶ | local $DUMPING = { }; | ||||||||||||||||||||||||||
|
Subroutine
dump_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 291 | 0 | _dump_data(@_); | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | sub _dump_data { | ||||||||||||||||||||||||||||
| 296 | 13 | ▶ | if (! defined $_[1]) { | ||||||||||||||||||||||||||
|
Subroutine
_dump_data: called CC=5 SCAR=16.5
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 297 | 0 | return UNDEF; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | elsif (! ref $_[1]) { | ||||||||||||||||||||||||||||
| 300 | 9 | return $_[1]; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | ▶ | elsif (blessed($_[1]) && (my $code = $_[1]->can($DUMP_METHOD))) { | |||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($_[1]) and my $code = $_[1]->can($Badger::Debug::DUMP_METHOD)
MC/DC50%1/2
builtin::blessed($_[1]) and my $code = $_[1]->can($Badger::Debug::DUMP_METHOD)
builtin::blessed($_[1])
my $code = $_[1]->can($Badger::Debug::DUMP_METHOD)
Truth table66%2/3
A and B
A= builtin::blessed($_[1])B= my $code = $_[1]->can($Badger::Debug::DUMP_METHOD)
| |||||||||||||||||||||||||||||
| 303 | 2 | shift; # remove $self object, leave target object first | |||||||||||||||||||||||||||
| 304 | 2 | return $code->(@_); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | else { | ||||||||||||||||||||||||||||
| 307 | 2 | goto &dump_ref; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | sub dump_ref { | ||||||||||||||||||||||||||||
| 313 | 4 | ▶ | my ($self, $data, $indent) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dump_ref: called CC=6 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||
| 314 | 4 | ▶ | return "<$data>" if $DUMPING->{ $data }++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # TODO: change these to reftype | ||||||||||||||||||||||||||||
| 317 | 4 | ▶ | if (UNIVERSAL::isa($data, HASH)) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 318 | 3 | return dump_hash($self, $data, $indent); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | elsif (UNIVERSAL::isa($data, ARRAY)) { | ||||||||||||||||||||||||||||
| 321 | 1 | return dump_list($self, $data, $indent); | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | elsif (UNIVERSAL::isa($data, REGEX)) { | ||||||||||||||||||||||||||||
| 324 | 0 | return dump_text($self, $data); | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | elsif (UNIVERSAL::isa($data, SCALAR)) { | ||||||||||||||||||||||||||||
| 327 | 0 | return dump_text($self, $$data); | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | else { | ||||||||||||||||||||||||||||
| 330 | 0 | return $data; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | sub dump_data_inline { | ||||||||||||||||||||||||||||
| 336 | 0 | ▶ | local $PAD = ''; | ||||||||||||||||||||||||||
|
Subroutine
dump_data_inline: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 337 | 0 | my $text = shift->dump_data(@_); | |||||||||||||||||||||||||||
| 338 | 0 | $text =~ s/\n/ /g; | |||||||||||||||||||||||||||
| 339 | 0 | return $text; | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | sub dump_hash { | ||||||||||||||||||||||||||||
| 344 | 4 | ▶ | my ($self, $hash, $indent, $keys) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dump_hash: called CC=13 SCAR=27.9
Pod
documented
| |||||||||||||||||||||||||||||
| 345 | 4 | ▶ | $indent ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$indent ||= 0
MC/DC100%1/1
$indent ||= 0
$indent
Truth table100%2/2
A
A= $indent
| |||||||||||||||||||||||||||||
| 346 | 4 | ▶ | return "..." if $indent > $MAX_DEPTH; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | 4 | my $pad = $PAD x $indent; | |||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 4 | ▶ | return '{ }' unless $hash && %$hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$hash and %$hash
MC/DC0%0/2
$hash and %$hash
$hash
%$hash
Truth table33%1/3
A and B
A= $hashB= %$hash
| |||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 4 | ▶ | if ($keys) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | 1 | ▶ | $keys = [ split(DELIMITER, $keys) ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | unless ref $keys; | ||||||||||||||||||||||||||||
| 354 | 1 | ▶ | $keys = { map { $_ => 1 } @$keys } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | if ref $keys eq ARRAY; | ||||||||||||||||||||||||||||
| 356 | 1 | ▶ | return $self->error("Invalid keys passed to dump_hash(): $keys") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | unless ref $keys eq HASH; | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 1 | ▶ | $self->debug("constructed hash keys: ", join(', ', %$keys)) if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | return "\{\n" | ||||||||||||||||||||||||||||
| 363 | . join( ",\n", | ||||||||||||||||||||||||||||
| 364 | 11 | map { "$pad$PAD$_ => " . _dump_data($self, $hash->{$_}, $indent + 1) } | |||||||||||||||||||||||||||
| 365 | sort | ||||||||||||||||||||||||||||
| 366 | 12 | ▶ | grep { $keys ? $keys->{ $_ } : 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | 4 | ▶ | grep { (/^_/ && $HIDE_UNDER) ? 0 : 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
/^_/ and $Badger::Debug::HIDE_UNDER
MC/DC0%0/2
/^_/ and $Badger::Debug::HIDE_UNDER
/^_/
$Badger::Debug::HIDE_UNDER
Truth table33%1/3
A and B
A= /^_/B= $Badger::Debug::HIDE_UNDER
| |||||||||||||||||||||||||||||
| 368 | keys %$hash | ||||||||||||||||||||||||||||
| 369 | ) | ||||||||||||||||||||||||||||
| 370 | . "\n$pad}"; | ||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | sub dump_list { | ||||||||||||||||||||||||||||
| 375 | 1 | ▶ | my ($self, $list, $indent) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dump_list: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 376 | 1 | ▶ | $indent ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$indent ||= 0
MC/DC0%0/1
$indent ||= 0
$indent
Truth table50%1/2
A
A= $indent
| |||||||||||||||||||||||||||||
| 377 | 1 | my $pad = $PAD x $indent; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 1 | ▶ | return '[ ]' unless @$list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | return "\[\n$pad$PAD" | ||||||||||||||||||||||||||||
| 381 | . ( @$list | ||||||||||||||||||||||||||||
| 382 | 1 | ▶ | ? join(",\n$pad$PAD", map { _dump_data($self, $_, $indent + 1) } @$list) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | : '' ) | ||||||||||||||||||||||||||||
| 384 | . "\n$pad]"; | ||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | sub dump_text { | ||||||||||||||||||||||||||||
| 389 | 0 | ▶ | my ($self, $text, $length) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dump_text: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 390 | 0 | ▶ | $text = $$text if ref $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | 0 | ▶ | $length ||= $MAX_TEXT; | ||||||||||||||||||||||||||
|
Condition0%0/3
$length ||= $Badger::Debug::MAX_TEXT
MC/DC0%0/2
$length ||= $Badger::Debug::MAX_TEXT
$length
$Badger::Debug::MAX_TEXT
Truth table0%0/3
A ||= B
A= $lengthB= $Badger::Debug::MAX_TEXT
| |||||||||||||||||||||||||||||
| 392 | 0 | my $snippet = substr($text, 0, $length); | |||||||||||||||||||||||||||
| 393 | 0 | ▶ | $snippet .= '...' if length $text > $length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 0 | $snippet =~ s/\n/\\n/g; | |||||||||||||||||||||||||||
| 395 | 0 | return $snippet; | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 401 | # enable_colour() | ||||||||||||||||||||||||||||
| 402 | # | ||||||||||||||||||||||||||||
| 403 | # Export hook which gets called when the Badger::Debug module is | ||||||||||||||||||||||||||||
| 404 | # used with the 'colour' or 'color' option. It redefines the formats | ||||||||||||||||||||||||||||
| 405 | # for $Badger::Base::DEBUG_FORMAT and $Badger::Exception::FORMAT | ||||||||||||||||||||||||||||
| 406 | # to display in glorious ANSI technicolor. | ||||||||||||||||||||||||||||
| 407 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | sub enable_colour { | ||||||||||||||||||||||||||||
| 410 | 0 | ▶ | my ($class, $target, $symbol) = @_; | ||||||||||||||||||||||||||
|
Subroutine
enable_colour: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 411 | 0 | ▶ | $target ||= (caller())[0]; | ||||||||||||||||||||||||||
|
Condition0%0/3
$target ||= (caller())[0]
MC/DC0%0/2
$target ||= (caller())[0]
$target
(caller())[0]
Truth table0%0/3
A ||= B
A= $targetB= (caller())[0]
| |||||||||||||||||||||||||||||
| 412 | 0 | ▶ | $symbol ||= 'colour'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$symbol ||= "colour"
MC/DC0%0/1
$symbol ||= "colour"
$symbol
Truth table0%0/2
A
A= $symbol
| |||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 0 | print bold green "Enabling debug in $symbol from $target\n"; | |||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | # colour the debug format | ||||||||||||||||||||||||||||
| 417 | 0 | $MESSAGE = cyan($PROMPT) . yellow('%s'); | |||||||||||||||||||||||||||
| 418 | 0 | $FORMAT | |||||||||||||||||||||||||||
| 419 | = cyan('[<where> line <line>]') | ||||||||||||||||||||||||||||
| 420 | . "\n<msg>"; | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | # exceptions are in red | ||||||||||||||||||||||||||||
| 423 | 0 | $Badger::Exception::FORMAT | |||||||||||||||||||||||||||
| 424 | = bold red $Badger::Exception::FORMAT; | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | $Badger::Exception::MESSAGES->{ caller } | ||||||||||||||||||||||||||||
| 427 | 0 | = yellow('<4>') . cyan(' called from ') | |||||||||||||||||||||||||||
| 428 | . yellow("<1>\n") . cyan(' in ') | ||||||||||||||||||||||||||||
| 429 | . white('<2>') . cyan(' at line ') | ||||||||||||||||||||||||||||
| 430 | . white('<3>'); | ||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 70 | 1; | |||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | __END__ | ||||||||||||||||||||||||||||