… | |
… | |
26 | |
26 | |
27 | package AnyEvent::Debug; |
27 | package AnyEvent::Debug; |
28 | |
28 | |
29 | use Carp (); |
29 | use Carp (); |
30 | use Errno (); |
30 | use Errno (); |
31 | use POSIX (); |
|
|
32 | |
31 | |
33 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
32 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
34 | use AnyEvent::Util (); |
33 | use AnyEvent::Util (); |
35 | use AnyEvent::Socket (); |
34 | use AnyEvent::Socket (); |
|
|
35 | use AnyEvent::Log (); |
36 | |
36 | |
37 | =item $shell = AnyEvent;::Debug::shell $host, $service |
37 | =item $shell = AnyEvent;::Debug::shell $host, $service |
38 | |
38 | |
39 | This function binds on the given host and service port and returns a |
39 | This function binds on the given host and service port and returns a |
40 | shell object, which determines the lifetime of the shell. Any number |
40 | shell object, which determines the lifetime of the shell. Any number |
… | |
… | |
136 | |
136 | |
137 | sub help() { |
137 | sub help() { |
138 | <<EOF |
138 | <<EOF |
139 | help this command |
139 | help this command |
140 | wr [level] sets wrap level to level (or toggles if missing) |
140 | wr [level] sets wrap level to level (or toggles if missing) |
141 | t [level] sets trace level (or toggles if missing) |
141 | v [level] sets verbosity (or toggles if missing) |
142 | wl 'regex' print wrapped watchers matching the regex (or all if missing) |
142 | wl 'regex' print wrapped watchers matching the regex (or all if missing) |
143 | w id,... prints the watcher with the given ids in more detail |
143 | w id,... prints the watcher with the given ids in more detail |
144 | EOF |
144 | EOF |
145 | } |
145 | } |
146 | |
146 | |
… | |
… | |
176 | AnyEvent::Debug::wrap (@_); |
176 | AnyEvent::Debug::wrap (@_); |
177 | |
177 | |
178 | "wrap level now $AnyEvent::Debug::WRAP_LEVEL" |
178 | "wrap level now $AnyEvent::Debug::WRAP_LEVEL" |
179 | } |
179 | } |
180 | |
180 | |
181 | sub t { |
181 | sub v { |
182 | $AnyEvent::Debug::TRACE_LEVEL = @_ ? shift : $AnyEvent::Debug::TRACE_LEVEL ? 0 : 9; |
182 | $AnyEvent::VERBOSE = @_ ? shift : $AnyEvent::VERBOSE ? 0 : 9; |
183 | |
183 | |
184 | "trace level now $AnyEvent::Debug::TRACE_LEVEL" |
184 | "verbosity level now $AnyEvent::VEBROSE" |
185 | } |
185 | } |
186 | } |
186 | } |
187 | |
187 | |
188 | =item AnyEvent::Debug::wrap [$level] |
188 | =item AnyEvent::Debug::wrap [$level] |
189 | |
189 | |
… | |
… | |
378 | @DB::args = (); |
378 | @DB::args = (); |
379 | |
379 | |
380 | bless \@bt, "AnyEvent::Debug::Backtrace" |
380 | bless \@bt, "AnyEvent::Debug::Backtrace" |
381 | } |
381 | } |
382 | |
382 | |
383 | # Format Time, not public - yet? |
|
|
384 | sub ft($) { |
|
|
385 | my $t = shift; |
|
|
386 | my $i = int $t; |
|
|
387 | my $f = sprintf "%06d", 1e6 * ($t - $i); |
|
|
388 | |
|
|
389 | POSIX::strftime "%Y-%m-%d %H:%M:%S.$f %z", localtime $i |
|
|
390 | } |
|
|
391 | |
|
|
392 | package AnyEvent::Debug::Wrap; |
383 | package AnyEvent::Debug::Wrap; |
393 | |
384 | |
394 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
385 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
395 | use Scalar::Util (); |
386 | use Scalar::Util (); |
396 | use Carp (); |
387 | use Carp (); |
… | |
… | |
419 | |
410 | |
420 | return &$cb |
411 | return &$cb |
421 | unless $TRACE_LEVEL; |
412 | unless $TRACE_LEVEL; |
422 | |
413 | |
423 | local $TRACE_CUR = "$w"; |
414 | local $TRACE_CUR = "$w"; |
424 | print AnyEvent::Debug::ft AE::now, " enter $TRACE_CUR\n" if $TRACE_LEVEL; |
415 | AE::log trace => "enter $TRACE_CUR"; |
425 | eval { |
416 | eval { |
426 | local $SIG{__DIE__} = sub { die $_[0] . AnyEvent::Debug::backtrace }; |
417 | local $SIG{__DIE__} = sub { die $_[0] . AnyEvent::Debug::backtrace }; |
427 | &$cb; |
418 | &$cb; |
428 | }; |
419 | }; |
429 | if ($@) { |
420 | if ($@) { |
430 | push @{ $w->{error} }, [AE::now, $@] |
421 | push @{ $w->{error} }, [AE::now, $@] |
431 | if @{ $w->{error} } < 10; |
422 | if @{ $w->{error} } < 10; |
432 | print AnyEvent::Debug::ft AE::now, " ERROR $TRACE_CUR $@"; |
423 | AE::log error => "$TRACE_CUR $@"; |
433 | } |
424 | } |
434 | print AnyEvent::Debug::ft AE::now, " leave $TRACE_CUR\n" if $TRACE_LEVEL; |
425 | AE::log trace => "leave $TRACE_CUR"; |
435 | }; |
426 | }; |
436 | |
427 | |
437 | $self = bless { |
428 | $self = bless { |
438 | type => $name, |
429 | type => $name, |
439 | w => $self->$super (%arg), |
430 | w => $self->$super (%arg), |
… | |
… | |
453 | if $WRAP_LEVEL >= 2; |
444 | if $WRAP_LEVEL >= 2; |
454 | |
445 | |
455 | Scalar::Util::weaken ($w = $self); |
446 | Scalar::Util::weaken ($w = $self); |
456 | Scalar::Util::weaken ($AnyEvent::Debug::Wrapped{Scalar::Util::refaddr $self} = $self); |
447 | Scalar::Util::weaken ($AnyEvent::Debug::Wrapped{Scalar::Util::refaddr $self} = $self); |
457 | |
448 | |
458 | print AnyEvent::Debug::ft AE::now, " creat $w\n" if $TRACE_LEVEL; |
449 | AE::log trace => "creat $w"; |
459 | |
450 | |
460 | $self |
451 | $self |
461 | }; |
452 | }; |
462 | } |
453 | } |
463 | } |
454 | } |
… | |
… | |
489 | sub verbose { |
480 | sub verbose { |
490 | my ($self) = @_; |
481 | my ($self) = @_; |
491 | |
482 | |
492 | my $res = "type: $self->{type} watcher\n" |
483 | my $res = "type: $self->{type} watcher\n" |
493 | . "args: " . (join " ", %{ $self->{arg} }) . "\n" # TODO: decode fh? |
484 | . "args: " . (join " ", %{ $self->{arg} }) . "\n" # TODO: decode fh? |
494 | . "created: " . (AnyEvent::Debug::ft $self->{now}) . " ($self->{now})\n" |
485 | . "created: " . (AnyEvent::Log::ft $self->{now}) . " ($self->{now})\n" |
495 | . "file: $self->{file}\n" |
486 | . "file: $self->{file}\n" |
496 | . "line: $self->{line}\n" |
487 | . "line: $self->{line}\n" |
497 | . "subname: $self->{sub}\n" |
488 | . "subname: $self->{sub}\n" |
498 | . "context: $self->{cur}\n" |
489 | . "context: $self->{cur}\n" |
499 | . "cb: $self->{cb} (" . (AnyEvent::Debug::cb2str $self->{cb}) . ")\n" |
490 | . "cb: $self->{cb} (" . (AnyEvent::Debug::cb2str $self->{cb}) . ")\n" |
… | |
… | |
504 | } |
495 | } |
505 | |
496 | |
506 | if (exists $self->{error}) { |
497 | if (exists $self->{error}) { |
507 | $res .= "errors: " . @{$self->{error}} . "\n"; |
498 | $res .= "errors: " . @{$self->{error}} . "\n"; |
508 | |
499 | |
509 | $res .= "error: " . (AnyEvent::Debug::ft $_->[0]) . " ($_->[0]) $_->[1]\n" |
500 | $res .= "error: " . (AnyEvent::Log::ft $_->[0]) . " ($_->[0]) $_->[1]\n" |
510 | for @{$self->{error}}; |
501 | for @{$self->{error}}; |
511 | } |
502 | } |
512 | |
503 | |
513 | $res |
504 | $res |
514 | } |
505 | } |
515 | |
506 | |
516 | sub DESTROY { |
507 | sub DESTROY { |
517 | print AnyEvent::Debug::ft AE::now, " dstry $_[0]\n" if $TRACE_LEVEL; |
508 | AE::log trace => "dstry $_[0]"; |
518 | |
509 | |
519 | delete $AnyEvent::Debug::Wrapped{Scalar::Util::refaddr $_[0]}; |
510 | delete $AnyEvent::Debug::Wrapped{Scalar::Util::refaddr $_[0]}; |
520 | } |
511 | } |
521 | |
512 | |
522 | package AnyEvent::Debug::Backtrace; |
513 | package AnyEvent::Debug::Backtrace; |