… | |
… | |
202 | |
202 | |
203 | A level of C<0> disables wrapping, i.e. AnyEvent works normally, and in |
203 | A level of C<0> disables wrapping, i.e. AnyEvent works normally, and in |
204 | its most efficient mode. |
204 | its most efficient mode. |
205 | |
205 | |
206 | A level of C<1> enables wrapping, which replaces all watchers by |
206 | A level of C<1> enables wrapping, which replaces all watchers by |
207 | AnyEvent::Debug::Wrapped objects, stores the location where a watcher was |
207 | AnyEvent::Debug::Wrapped objects, stores the location where a watcher |
208 | created and wraps the callback to log all invocations at "trace" loglevel |
208 | was created and wraps the callback to log all invocations at "trace" |
209 | (see L<AnyEvent::Log>). |
209 | loglevel. The wrapper will also count how many times the callback was |
|
|
210 | invoked and will record up to ten runtime errors with corresponding |
|
|
211 | backtraces. It will also log runtime errors at "error" loglevel. |
|
|
212 | |
|
|
213 | To see the trace messages, you can invoke your program with |
|
|
214 | C<PERL_ANYEVENT_VERBOSE=9>, or you can use AnyEvent::Log to divert |
|
|
215 | the trace messages in any way you like (the EXAMPLES section in |
|
|
216 | L<AnyEvent::Log> has some examples). |
210 | |
217 | |
211 | A level of C<2> does everything that level C<1> does, but also stores a |
218 | A level of C<2> does everything that level C<1> does, but also stores a |
212 | full backtrace of the location the watcher was created, which slows down |
219 | full backtrace of the location the watcher was created, which slows down |
213 | watcher creation considerably. |
220 | watcher creation considerably. |
214 | |
221 | |
… | |
… | |
228 | to check for common mistakes. |
235 | to check for common mistakes. |
229 | |
236 | |
230 | =cut |
237 | =cut |
231 | |
238 | |
232 | our $WRAP_LEVEL; |
239 | our $WRAP_LEVEL; |
233 | our $TRACE_LEVEL; |
|
|
234 | our $TRACE_CUR; |
240 | our $TRACE_CUR; |
235 | our $POST_DETECT; |
241 | our $POST_DETECT; |
236 | |
242 | |
237 | sub wrap(;$) { |
243 | sub 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 { |