On December 4, 2006, Mike Schilli wrote:
> > wrapped, the exception that I wanted to ignore is now getting
> > logged. I don't want that. I want it to be completely silent.
> > (Failing that, I'd like to log it at a very low level.)
>
> Hmm, but if you want it to be completely silent, why aren't you simply
> resetting those handlers and then putting them back in place afterwards?
>
> eval {
> local $SIG{__WARN__} = 'IGNORE';
> local $SIG{__DIE__} = 'IGNORE';
> # ... your code
> };
This doesn't help in the event that the eval is in a module I installed off
of CPAN, which I can't practically modify to add that to the eval block. (I
could modify it, but it'd be wiped out at the next update, leading to a
maintenance nightmare.)
In this case I was dealing with evals in both places: my code, and a module.
I tried using local and setting the handler to 'DEFAULT', which seemed to lead
to more complex issues, and before I could fully trace through and find out
what was going on I found out about the eval in the module and decided this was
an untenable situation to try to fix. So I gave up trying to do a
quick-and-dirty instrumentation of my code by using these handlers, and
instrumented my code more directly. Which is probably a good thing, all in all.
> Alternatively, you could check if you're inside an eval{} (at some
> sublevel) by using code like
>
> sub burried_in_eval {
> my $i;
>
> while(my ($pack, $file, $line, $sub) = caller($i++)) {
> return 1 if $sub eq "(eval)";
> }
>
> return 0;
> }
>
> and act accordingly in your sig handlers.
Turns out there's an even easier way to do that. $^S can be checked to see
if code is in an eval. Thinking through this yesterday afternoon I finally
decided this wasn't a Log4perl issue, but a general Perl issue, because really
I think you'd almost never want those SIG handlers to fire when you're in an
eval, trying to trap things before they become warnings and errors. So I asked
at use Perl; , and somebody pointed me to that variable.
Since I wound up not using those handlers yesterday, it was a little too
late, but I do still have a module called by this program and by others, and
I'll have to either instrument it as I did the main program, or go back and use
the handlers. (I'm thinking about doing both, for good measure.)
Looks to me like the FAQ might want to change the text of those signal
handlers to include a check of $^S to decide whether to log or not. And
somebody might like to package it all up in a module, which is what I was
trying to do yesterday. :)
Thanks,
jdb
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
|