ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Debug.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Debug.pm (file contents):
Revision 1.21 by root, Thu Aug 18 19:35:15 2011 UTC vs.
Revision 1.22 by root, Sat Aug 20 15:57:35 2011 UTC

202 202
203A level of C<0> disables wrapping, i.e. AnyEvent works normally, and in 203A level of C<0> disables wrapping, i.e. AnyEvent works normally, and in
204its most efficient mode. 204its most efficient mode.
205 205
206A level of C<1> enables wrapping, which replaces all watchers by 206A level of C<1> enables wrapping, which replaces all watchers by
207AnyEvent::Debug::Wrapped objects, stores the location where a watcher was 207AnyEvent::Debug::Wrapped objects, stores the location where a watcher
208created and wraps the callback to log all invocations at "trace" loglevel 208was created and wraps the callback to log all invocations at "trace"
209(see L<AnyEvent::Log>). 209loglevel. The wrapper will also count how many times the callback was
210invoked and will record up to ten runtime errors with corresponding
211backtraces. It will also log runtime errors at "error" loglevel.
212
213To see the trace messages, you can invoke your program with
214C<PERL_ANYEVENT_VERBOSE=9>, or you can use AnyEvent::Log to divert
215the trace messages in any way you like (the EXAMPLES section in
216L<AnyEvent::Log> has some examples).
210 217
211A level of C<2> does everything that level C<1> does, but also stores a 218A level of C<2> does everything that level C<1> does, but also stores a
212full backtrace of the location the watcher was created, which slows down 219full backtrace of the location the watcher was created, which slows down
213watcher creation considerably. 220watcher creation considerably.
214 221
228to check for common mistakes. 235to check for common mistakes.
229 236
230=cut 237=cut
231 238
232our $WRAP_LEVEL; 239our $WRAP_LEVEL;
233our $TRACE_LEVEL;
234our $TRACE_CUR; 240our $TRACE_CUR;
235our $POST_DETECT; 241our $POST_DETECT;
236 242
237sub wrap(;$) { 243sub wrap(;$) {
238 my $PREV_LEVEL = $WRAP_LEVEL; 244 my $PREV_LEVEL = $WRAP_LEVEL;
417 423
418 my $cb = $arg{cb}; 424 my $cb = $arg{cb};
419 $arg{cb} = sub { 425 $arg{cb} = sub {
420 ++$w->{called}; 426 ++$w->{called};
421 427
422 return &$cb
423 unless $TRACE_LEVEL;
424
425 local $TRACE_CUR = $w; 428 local $TRACE_CUR = $w;
429
426 $TRACE_LOGGER->("enter $TRACE_CUR") if $TRACE_ENABLED; 430 $TRACE_LOGGER->("enter $TRACE_CUR") if $TRACE_ENABLED;
427 eval { 431 eval {
428 local $SIG{__DIE__} = sub { die $_[0] . AnyEvent::Debug::backtrace }; 432 local $SIG{__DIE__} = sub { die $_[0] . AnyEvent::Debug::backtrace };
429 &$cb; 433 &$cb;
430 }; 434 };
431 if ($@) { 435 if ($@) {
432 push @{ $w->{error} }, [AE::now, $@] 436 push @{ $w->{error} }, [AE::now, $@]
433 if @{ $w->{error} } < 10; 437 if @{ $w->{error} } < 10;
434 AE::log error => "$TRACE_CUR $@"; 438 AE::log error => "($TRACE_CUR) $@";
435 } 439 }
436 $TRACE_LOGGER->("leave $TRACE_CUR") if $TRACE_ENABLED; 440 $TRACE_LOGGER->("leave $TRACE_CUR") if $TRACE_ENABLED;
437 }; 441 };
438 442
439 $self = bless { 443 $self = bless {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines