in reply to Logging question

In addition to what Roy Johnson said, change your call to can_read to include the timeout rather then doing a sleep at the bottom of the if block. The complete code could look something like this:

for (;;) { if (@ready = $select->can_read(10)) { # input waiting on the filehandles in @ready foreach $filehandle (@ready) { while (<$filehandle>) { chomp(); if (m/^Hello/) { $log->error("This is a hello msg"); } else { $log->error("message was not recieved"); } } } $cycles = 0; } elsif (++$cycles % 6 == 0) { $log->error("No new messages in the last 60 seconds"); } }

Replies are listed 'Best First'.
Re: Re: Logging question
by fizbin (Chaplain) on May 12, 2004 at 13:16 UTC

    Note that having the timeout in can_read is actually essential, or else when nothing is writing to the file you'll never return from the can_read call - this means you won't generate the message unless you follow this advice and add a timeout to can_read.

    Of course, if you're already putting a timeout in the can_read call anyway, why not go all the way and just put the 60 second timeout in there? Something like this:

    for (;;) { if (@ready = $select->can_read(60)) { # input waiting on the filehandles in @ready foreach $filehandle (@ready) { while (<$filehandle>) { chomp(); if (m/^Hello/) { $log->error("This is a hello msg"); } else { $log->error("message was not recieved"); } } } } else { $log->error("No new messages in the last 60 seconds"); } }

    Now, this does mean that your "every 10 seconds" idea has gone away, but it was never really there in the first place - as I mentioned before, can_read will block forever if you don't give it a timeout, and this code won't be running your cpu time through the roof because can_read will block, up to the given timeout interval.

    As someone else mentioned, though, look at File::Tail and see if it can do the hard stuff for you.

    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/