For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > November 2005 > Logging Errors (Instead of Just Dying)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Logging Errors (Instead of Just Dying)
Hal Vaughan

2005-11-26, 6:58 pm

I have several Perl daemons running on a particular system. For the most
part, they are behaving well, but I notice that two need to be restarted a
few times a w. I have a lot of logging statements saying what the
program is doing at different points. What I'd like to do is log the
actual errors before the program dies (and is then restarted by it's
monitor). In the routine, I want to be able to print the line number and
type of error (as given in the normal error messages) to my log file.

Is there any way, inside a Perl module, to do something like "If there are
any errors, don't die, call this function, THEN die"? (Or just "call this
function" and I can always make the function itself terminate the program.)

I know the FAQ mentions using eval, but they reference Larry Wall's
"&realcode", which, when I Google it, comes up with references to the same
FAQ, where it is not explained. I'm trying to understand using eval, but I
don't see how I could put an entire module in an eval block -- I would
expect that to only evaluate it when the module is loaded. The few
examples of this (that I can find) are, at least to me, hard to follow, so
clearer examples would help a LOT. (I'm wondering if, since it's in the
FAQ, nobody's bothered to cover this in depth or with more examples.)

For example, if most of what the program does is in the module, how would I
use eval around the subroutine calls from the main program? Can I get away
with having one call to the mainloop (which is in a subroutine by itself)
in it and then using eval only around that one call to the main loop?

Any help in clarifying this is appreciated!

Hal
xhoster@gmail.com

2005-11-26, 6:58 pm

Hal Vaughan <hal@thresholddigital.com> wrote:
> I have several Perl daemons running on a particular system. For the most
> part, they are behaving well, but I notice that two need to be restarted
> a few times a w. I have a lot of logging statements saying what the
> program is doing at different points. What I'd like to do is log the
> actual errors before the program dies (and is then restarted by it's
> monitor). In the routine, I want to be able to print the line number and
> type of error (as given in the normal error messages) to my log file.


Why not just direct STDERR to our log file?

> Is there any way, inside a Perl module, to do something like "If there
> are any errors, don't die, call this function, THEN die"? (Or just "call
> this function" and I can always make the function itself terminate the
> program.)


see $SIG{__DIE__} in perldoc perlvar and perldoc -f die.

>
> I know the FAQ mentions using eval, but they reference Larry Wall's
> "&realcode", which,


How old is this FAQ? How old is the version of Perl you are using?

> when I Google it, comes up with references to the
> same FAQ, where it is not explained.


&realcode is a stand-in for whatever your real code is. Since Larry
doesn't know what your code is that you want plugged into the example, he
has to use a stand-in. (&realcode is an old, mostly deprecated, way of
executing a subroutine named "realcode".)

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Mark Clements

2005-11-26, 6:58 pm

Hal Vaughan wrote:
> I have several Perl daemons running on a particular system. For the most
> part, they are behaving well, but I notice that two need to be restarted a
> few times a w. I have a lot of logging statements saying what the
> program is doing at different points. What I'd like to do is log the
> actual errors before the program dies (and is then restarted by it's
> monitor). In the routine, I want to be able to print the line number and
> type of error (as given in the normal error messages) to my log file.
>
> Is there any way, inside a Perl module, to do something like "If there are
> any errors, don't die, call this function, THEN die"? (Or just "call this
> function" and I can always make the function itself terminate the program.)


You could define an END block and put some logging in there (see perldoc
perlmod), and/or reinvoke your program via exec. You may have difficulty
accessing error messages etc at this stage though.

<snip>

> For example, if most of what the program does is in the module, how

would I
> use eval around the subroutine calls from the main program? Can I get away
> with having one call to the mainloop (which is in a subroutine by itself)
> in it and then using eval only around that one call to the main loop?


not entirely sure what you mean, but you could try:

eval {
my $serviceLoop = ServiceLoop->new();
$serviceLoop->run();
};

if($@){
print "error in serviceloop => $@";
}else{
print "exited serviceloop cleanly";
}

I also suggest checking out Log::Log4perl (or similar). Note this can be
set up to display the location at which a message is output.

Mark
Tad McClellan

2005-11-26, 6:58 pm

Hal Vaughan <hal@thresholddigital.com> wrote:

> What I'd like to do is log the
> actual errors before the program dies (and is then restarted by it's
> monitor). In the routine, I want to be able to print the line number and
> type of error (as given in the normal error messages) to my log file.
>
> Is there any way, inside a Perl module, to do something like "If there are
> any errors, don't die, call this function, THEN die"? (Or just "call this
> function" and I can always make the function itself terminate the program.)



> For example, if most of what the program does is in the module, how would I
> use eval around the subroutine calls from the main program?


eval { subroutine_call( $arg1, $arg2) };
exit_gracefully($@) if $@; # call exit_gracefully() if error was trapped


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Hal Vaughan

2005-11-27, 3:56 am

Just a quick 'thanks!' to those who replied. When I was reading the FAQ, it
looked more complex, and the examples here showed me just how easy this is!
I wish I had known about this a long time ago. I've already set it up,
roughly like this:

eval {
mainloop();
}
if ($@) {
serverlog("error Perl has reported an error: $@");
serverlog("exiting so program will restart");
exit();
}

There was just enough in the faq (and enough left out) to make it seem like
it was more complex than it is.

Your help is appreciated!

Hal

Hal Vaughan wrote:

> I have several Perl daemons running on a particular system. For the most
> part, they are behaving well, but I notice that two need to be restarted a
> few times a w. I have a lot of logging statements saying what the
> program is doing at different points. What I'd like to do is log the
> actual errors before the program dies (and is then restarted by it's
> monitor). In the routine, I want to be able to print the line number and
> type of error (as given in the normal error messages) to my log file.
>
> Is there any way, inside a Perl module, to do something like "If there are
> any errors, don't die, call this function, THEN die"? (Or just "call this
> function" and I can always make the function itself terminate the
> program.)
>
> I know the FAQ mentions using eval, but they reference Larry Wall's
> "&realcode", which, when I Google it, comes up with references to the same
> FAQ, where it is not explained. I'm trying to understand using eval, but
> I don't see how I could put an entire module in an eval block -- I would
> expect that to only evaluate it when the module is loaded. The few
> examples of this (that I can find) are, at least to me, hard to follow, so
> clearer examples would help a LOT. (I'm wondering if, since it's in the
> FAQ, nobody's bothered to cover this in depth or with more examples.)
>
> For example, if most of what the program does is in the module, how would
> I
> use eval around the subroutine calls from the main program? Can I get
> away with having one call to the mainloop (which is in a subroutine by
> itself) in it and then using eval only around that one call to the main
> loop?
>
> Any help in clarifying this is appreciated!
>
> Hal


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com