Logging Errors (Instead of Just Dying)

Discussion in 'Perl Misc' started by Hal Vaughan, Nov 26, 2005.

  1. Hal Vaughan

    Hal Vaughan Guest

    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 week. 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
     
    Hal Vaughan, Nov 26, 2005
    #1
    1. Advertising

  2. 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 week. 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
     
    Mark Clements, Nov 26, 2005
    #2
    1. Advertising

  3. Hal Vaughan <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 26, 2005
    #3
  4. Hal Vaughan

    Hal Vaughan Guest

    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 week. 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
     
    Hal Vaughan, Nov 27, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Letford

    session keeps dying?

    Steve Letford, Aug 5, 2003, in forum: ASP .Net
    Replies:
    8
    Views:
    426
  2. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    965
    Mark Goldin
    Jan 17, 2004
  3. James Kimble
    Replies:
    36
    Views:
    1,111
    unbending
    Jan 26, 2005
  4. Replies:
    1
    Views:
    377
  5. Bruno Boettcher

    problem with XML::Parser dying on errors...

    Bruno Boettcher, Feb 7, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    120
    Tad McClellan
    Feb 8, 2006
Loading...

Share This Page