eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:
I have a requirement in my current project for a simple yet flexible logging facility. While die/croak are fine for exception handling, logging uncaught exceptions to stderr is often too inflexible -- you may want to write fatal errors to an arbitrary file handle/s and/or send an email, for example.
I'm thinking of rolling my own very simple logging/tracing/carping facility (I'm in good company judging by the very many Log and Carp modules on the CPAN ;-). Here's some sample code to show where I'm coming from.
and a test driver:package MyDie; my $die_handle = \*STDOUT; sub mydie { my $message = shift; # If in eval block, throw exception ... $^S and die $message; # ... otherwise print message to handle and exit. print {$die_handle} $message, "exiting now\n"; exit 1; } 1;
use MyDie; sub testfn { print "in testfn\n"; MyDie::mydie("whoops\n"); print "end testfn\n"; } eval { testfn() }; $@ and print "caught: '$@'\n"; testfn(); print "end test program\n";
Generally, I prefer functions to throw exceptions rather than return special values. The idea here is to simply call die (throw) if called inside an eval block, otherwise to log the fatal error message and exit.
Since there are many ways to skin this particular cat, I'm interested to hear people's ideas and opinions before charging ahead with this.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Rolling your own die/carp/croak/logging/exception facility
by rinceWind (Monsignor) on Mar 10, 2006 at 09:02 UTC | |
|
Re: Rolling your own die/carp/croak/logging/exception facility
by hv (Prior) on Mar 10, 2006 at 12:54 UTC | |
|
Re: Rolling your own die/carp/croak/logging/exception facility
by izut (Chaplain) on Mar 10, 2006 at 10:22 UTC |