advice for tailing a file
Scott Wiersdorf
scott at ipartner.net
Mon Dec 14 18:32:26 CET 2015
Hello,
First, thanks Marc for AE and AE::*--they've made non-trivial portions
of my career excellent! :)
I am trying to collect the tail of a log file, process n (e.g. 10)
seconds of lines and get some basic stats from them. This is sort of a
time-series problem I suppose.
I started using File::Tail, which worked really well for tailing the
file (it handles cases where the file is rotated, etc. and re-opens
the new file of the same name), but when I got to time-slicing, I hit
a wall. I'm pretty sure now that File::Tail won't work for me since I
will need to be doing things every n seconds and I can't be blocking
on disk I/O.
I'd like some advice from anyone who has done something similar: how
can you tail a file and be interrupted every n seconds? I thought of
something along these lines (syntax only for illustration):
my @lines = my @stack = ();
open my $log, "<", $log_file;
seek $fh, 0, 2; ## find EOF
my $wio = AnyEvent->io(fh => $log, poll => 'r', cb => sub {
push @lines for <$fh>; ## read until EOF
});
my $wt = AnyEvent->timer(after => 10, interval => 10, cb => sub {
## save all the lines we found in the past 10 seconds
push @stack, [ @lines ];
@lines = (); ## reset the lines buffer
});
EV::run;
I know that the `io` method probably isn't the right thing here, nor
would AnyEvent::Handle work since I'm working with disk IO. I've
looked at IO::AIO but couldn't find a line-oriented read (only
aio_read into a buffer).
Does anyone have an idea of how I can accomplish tailing a file
without blocking?
Scott
--
Scott Wiersdorf
<scott at perlcode.org>
More information about the anyevent
mailing list