| 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 | use Carp; | ||
| 16 | use Badger::Rainbow | ||
| 17 | ANSI => 'bold red yellow green cyan white'; | ||
| 18 | use Scalar::Util qw( blessed refaddr ); | ||
| 19 | use Badger::Class | ||
| 20 | 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 | }; | ||
| 45 | |||
| 46 | our $PAD = ' '; | ||
| 47 | our $MAX_TEXT = 48; | ||
| 48 | our $MAX_DEPTH = 3; # prevent runaways in debug/dump | ||
| 49 | our $FORMAT = "[<where> line <line>]\n<msg>" | ||
| 50 | unless defined $FORMAT; | ||
| 51 | our $PROMPT = '> ' | ||
| 52 | unless defined $PROMPT; | ||
| 53 | our $MESSAGE = "$PROMPT%s"; | ||
| 54 | our $HIDE_UNDER = 1; | ||
| 55 | our $CALLER_UP = 0; # hackola to allow debug() to use a different caller | ||
| 56 | our $CALLER_AT = { }; # ditto | ||
| 57 | our $DUMPING = { }; | ||
| 58 | our $DEBUG = 0 unless defined $DEBUG; | ||
| 59 | our $DUMP_METHOD = 'dump'; | ||
| 60 | |||
| 61 | #----------------------------------------------------------------------- | ||
| 62 | # export hooks | ||
| 63 | #----------------------------------------------------------------------- | ||
| 64 | |||
| 65 | sub _export_debug_dumps { | ||
| 66 | my ($self, $target, $symbol, $value, $symbols) = @_; | ||
| 67 | $self->export_symbol($target, dumper => sub { | ||
| 68 | $_[0]->dump_hash($_[0],$_[1],$value); | ||
| 69 | }); | ||
| 70 | unshift(@$symbols, ':dump'); | ||
| 71 | return $self; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | sub _export_debug_default { | ||
| 76 | my ($self, $target, $symbol, $value, $symbols) = @_; | ||
| 77 | unshift( | ||
| 78 | @$symbols, | ||
| 79 | '$DEBUG' => $value, | ||
| 80 | 'DEBUG' => $value, | ||
| 81 | 'debug', | ||
| 82 | 'debugging' | ||
| 83 | ); | ||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub _export_debug_variable { | ||
| 89 | my ($self, $target, $symbol, $value) = @_; | ||
| 90 | no strict REFS; | ||
| 91 | |||
| 92 | # use any existing value in $DEBUG | ||
| 93 | $value = ${ $target.PKG.DEBUG } | ||
| 94 | if defined ${ $target.PKG.DEBUG }; | ||
| 95 | |||
| 96 | $self->debug("$symbol option setting $target \$DEBUG to $value\n") if $DEBUG; | ||
| 97 | *{ $target.PKG.DEBUG } = \$value; | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | sub _export_debug_constant { | ||
| 102 | my ($self, $target, $symbol, $value) = @_; | ||
| 103 | no strict REFS; | ||
| 104 | |||
| 105 | # use any existing value in $DEBUG | ||
| 106 | $value = ${ $target.PKG.DEBUG } | ||
| 107 | if defined ${ $target.PKG.DEBUG }; | ||
| 108 | |||
| 109 | $self->debug("$symbol option setting $target DEBUG to $value\n") if $DEBUG; | ||
| 110 | my $temp = $value; # make sure this is a const sub on 5.22 | ||
| 111 | *{ $target.PKG.DEBUG } = sub () { $temp }; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub _export_debug_modules { | ||
| 116 | my ($self, $target, $symbol, $modules) = @_; | ||
| 117 | $self->debug_modules($modules); | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | #----------------------------------------------------------------------- | ||
| 122 | # exportable debugging methods | ||
| 123 | #----------------------------------------------------------------------- | ||
| 124 | |||
| 125 | sub debugging { | ||
| 126 | my $self = shift; | ||
| 127 | my $pkg = ref $self || $self; | ||
| 128 | no strict REFS; | ||
| 129 | |||
| 130 | # return current $DEBUG value when called without args | ||
| 131 | return ${ $pkg.PKG.DEBUG } || 0 | ||
| 132 | unless @_; | ||
| 133 | |||
| 134 | # set new debug value when called with an argument | ||
| 135 | my $debug = shift; | ||
| 136 | $debug = 0 if $debug =~ /^off$/i; | ||
| 137 | |||
| 138 | # TODO: consider setting different parts of the flag, like TT2, | ||
| 139 | |||
| 140 | $self->debug("debugging() Setting $pkg debug to $debug\n") if $DEBUG; | ||
| 141 | |||
| 142 | if (defined ${ $pkg.PKG.DEBUG }) { | ||
| 143 | # update existing variable | ||
| 144 | ${ $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 | *{ $pkg.PKG.DEBUG } = \$debug; | ||
| 151 | } | ||
| 152 | return $debug; | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | sub debug { | ||
| 157 | my $self = shift; | ||
| 158 | my $msg = join('', @_), | ||
| 159 | my $class = ref $self || $self; | ||
| 160 | my $format = $CALLER_AT->{ format } || $FORMAT; | ||
| 161 | my ($pkg, $file, $line) = caller($CALLER_UP); | ||
| 162 | my (undef, undef, undef, $sub) = caller($CALLER_UP + 1); | ||
| 163 | if (defined $sub) { | ||
| 164 | $sub =~ s/.*?([^:]+)$/::$1()/; | ||
| 165 | } | ||
| 166 | else { | ||
| 167 | $sub = ''; | ||
| 168 | } | ||
| 169 | my $where = ($class eq $pkg) | ||
| 170 | ? $class . $sub | ||
| 171 | : $pkg . $sub . " ($class)"; | ||
| 172 | |||
| 173 | $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 | require Badger::Timestamp; | ||
| 179 | my $now = Badger::Timestamp->now; | ||
| 180 | 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 | $format =~ s/<(\w+)>/defined $data->{ $1 } ? $data->{ $1 } : "<$1 undef>"/eg; | ||
| 194 | $format .= "\n" unless $format =~ /\n$/; | ||
| 195 | |||
| 196 | print STDERR $format; | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | sub debugf { | ||
| 201 | local $CALLER_UP = 1; | ||
| 202 | shift->debug( sprintf(shift, @_) ); | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | sub debug_up { | ||
| 207 | my $self = shift; | ||
| 208 | local $CALLER_UP = shift; | ||
| 209 | $self->debug(@_); | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | sub debug_at { | ||
| 214 | my $self = shift; | ||
| 215 | local $CALLER_AT = shift; | ||
| 216 | local $CALLER_UP = 1; | ||
| 217 | $self->debug(@_); | ||
| 218 | } | ||
| 219 | |||
| 220 | |||
| 221 | sub debug_caller { | ||
| 222 | my $self = shift; | ||
| 223 | my ($pkg, $file, $line, $sub) = caller(1); | ||
| 224 | my $msg = "$sub called from "; | ||
| 225 | ($pkg, undef, undef, $sub) = caller(2); | ||
| 226 | $msg .= "$sub in $file at line $line\n"; | ||
| 227 | $self->debug($msg); | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | sub debug_callers { | ||
| 232 | my $self = shift; | ||
| 233 | my $msg = ''; | ||
| 234 | my $i = 1; | ||
| 235 | |||
| 236 | while (1) { | ||
| 237 | my @info = caller($i); | ||
| 238 | last unless @info; | ||
| 239 | my ($pkg, $file, $line, $sub) = @info; | ||
| 240 | $msg .= sprintf( | ||
| 241 | "%4s: Called from %s in %s at line %s\n", | ||
| 242 | '#' . $i++, $sub, $file, $line | ||
| 243 | ); | ||
| 244 | } | ||
| 245 | $self->debug($msg); | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | sub debug_args { | ||
| 250 | my $self = shift; | ||
| 251 | $self->debug_up( | ||
| 252 | 2, "args: ", | ||
| 253 | join(', ', map { $self->dump_data_inline($_) } @_), | ||
| 254 | "\n" | ||
| 255 | ); | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | sub debug_modules { | ||
| 260 | my $self = shift; | ||
| 261 | my $modules = @_ == 1 ? shift : [ @_ ]; | ||
| 262 | my $debug = 1; | ||
| 263 | |||
| 264 | $modules = [ split(DELIMITER, $modules) ] | ||
| 265 | unless ref $modules eq ARRAY; | ||
| 266 | |||
| 267 | # TODO: handle other refs? | ||
| 268 | |||
| 269 | foreach my $pkg (@$modules) { | ||
| 270 | no strict REFS; | ||
| 271 | *{ $pkg.PKG.DEBUG } = \$debug; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | #----------------------------------------------------------------------- | ||
| 277 | # data dumping methods | ||
| 278 | #----------------------------------------------------------------------- | ||
| 279 | |||
| 280 | sub dump { | ||
| 281 | my $self = shift; | ||
| 282 | my $code = $self->can('dumper'); | ||
| 283 | return $code | ||
| 284 | ? $code->($self, @_) | ||
| 285 | : $self->dump_ref($self, @_); | ||
| 286 | } | ||
| 287 | |||
| 288 | |||
| 289 | sub dump_data { | ||
| 290 | local $DUMPING = { }; | ||
| 291 | _dump_data(@_); | ||
| 292 | } | ||
| 293 | |||
| 294 | |||
| 295 | sub _dump_data { | ||
| 296 | if (! defined $_[1]) { | ||
| 297 | return UNDEF; | ||
| 298 | } | ||
| 299 | elsif (! ref $_[1]) { | ||
| 300 | return $_[1]; | ||
| 301 | } | ||
| 302 | elsif (blessed($_[1]) && (my $code = $_[1]->can($DUMP_METHOD))) { | ||
| 303 | shift; # remove $self object, leave target object first | ||
| 304 | return $code->(@_); | ||
| 305 | } | ||
| 306 | else { | ||
| 307 | goto &dump_ref; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | sub dump_ref { | ||
| 313 | my ($self, $data, $indent) = @_; | ||
| 314 | return "<$data>" if $DUMPING->{ $data }++; | ||
| 315 | |||
| 316 | # TODO: change these to reftype | ||
| 317 | if (UNIVERSAL::isa($data, HASH)) { | ||
| 318 | return dump_hash($self, $data, $indent); | ||
| 319 | } | ||
| 320 | elsif (UNIVERSAL::isa($data, ARRAY)) { | ||
| 321 | return dump_list($self, $data, $indent); | ||
| 322 | } | ||
| 323 | elsif (UNIVERSAL::isa($data, REGEX)) { | ||
| 324 | return dump_text($self, $data); | ||
| 325 | } | ||
| 326 | elsif (UNIVERSAL::isa($data, SCALAR)) { | ||
| 327 | return dump_text($self, $$data); | ||
| 328 | } | ||
| 329 | else { | ||
| 330 | return $data; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | |||
| 335 | sub dump_data_inline { | ||
| 336 | local $PAD = ''; | ||
| 337 | my $text = shift->dump_data(@_); | ||
| 338 | $text =~ s/\n/ /g; | ||
| 339 | return $text; | ||
| 340 | } | ||
| 341 | |||
| 342 | |||
| 343 | sub dump_hash { | ||
| 344 | my ($self, $hash, $indent, $keys) = @_; | ||
| 345 | $indent ||= 0; | ||
| 346 | return "..." if $indent > $MAX_DEPTH; | ||
| 347 | my $pad = $PAD x $indent; | ||
| 348 | |||
| 349 | return '{ }' unless $hash && %$hash; | ||
| 350 | |||
| 351 | if ($keys) { | ||
| 352 | $keys = [ split(DELIMITER, $keys) ] | ||
| 353 | unless ref $keys; | ||
| 354 | $keys = { map { $_ => 1 } @$keys } | ||
| 355 | if ref $keys eq ARRAY; | ||
| 356 | return $self->error("Invalid keys passed to dump_hash(): $keys") | ||
| 357 | unless ref $keys eq HASH; | ||
| 358 | |||
| 359 | $self->debug("constructed hash keys: ", join(', ', %$keys)) if $DEBUG; | ||
| 360 | } | ||
| 361 | |||
| 362 | return "\{\n" | ||
| 363 | . join( ",\n", | ||
| 364 | map { "$pad$PAD$_ => " . _dump_data($self, $hash->{$_}, $indent + 1) } | ||
| 365 | sort | ||
| 366 | grep { $keys ? $keys->{ $_ } : 1 } | ||
| 367 | grep { (/^_/ && $HIDE_UNDER) ? 0 : 1 } | ||
| 368 | keys %$hash | ||
| 369 | ) | ||
| 370 | . "\n$pad}"; | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | sub dump_list { | ||
| 375 | my ($self, $list, $indent) = @_; | ||
| 376 | $indent ||= 0; | ||
| 377 | my $pad = $PAD x $indent; | ||
| 378 | |||
| 379 | return '[ ]' unless @$list; | ||
| 380 | return "\[\n$pad$PAD" | ||
| 381 | . ( @$list | ||
| 382 | ? join(",\n$pad$PAD", map { _dump_data($self, $_, $indent + 1) } @$list) | ||
| 383 | : '' ) | ||
| 384 | . "\n$pad]"; | ||
| 385 | } | ||
| 386 | |||
| 387 | |||
| 388 | sub dump_text { | ||
| 389 | my ($self, $text, $length) = @_; | ||
| 390 | $text = $$text if ref $text; | ||
| 391 | $length ||= $MAX_TEXT; | ||
| 392 | my $snippet = substr($text, 0, $length); | ||
| 393 | $snippet .= '...' if length $text > $length; | ||
| 394 | $snippet =~ s/\n/\\n/g; | ||
| 395 | 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 | my ($class, $target, $symbol) = @_; | ||
| 411 | $target ||= (caller())[0]; | ||
| 412 | $symbol ||= 'colour'; | ||
| 413 | |||
| 414 | print bold green "Enabling debug in $symbol from $target\n"; | ||
| 415 | |||
| 416 | # colour the debug format | ||
| 417 | $MESSAGE = cyan($PROMPT) . yellow('%s'); | ||
| 418 | $FORMAT | ||
| 419 | = cyan('[<where> line <line>]') | ||
| 420 | . "\n<msg>"; | ||
| 421 | |||
| 422 | # exceptions are in red | ||
| 423 | $Badger::Exception::FORMAT | ||
| 424 | = bold red $Badger::Exception::FORMAT; | ||
| 425 | |||
| 426 | $Badger::Exception::MESSAGES->{ caller } | ||
| 427 | = 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 | 1; | ||
| 436 | |||
| 437 | __END__ |