http://www.perlmonks.org?node_id=735039

frd1963 has asked for the wisdom of the Perl Monks concerning the following question:

I have written a script to look for a specific string occurring within a log on a windows system. The log fills up very quickly and therefore rolls over (rotates) often. For some reason though, while my script is running, the file will not roll-over, presumably because the rolling mechanism cannot get a suitable lock on the file. I have tried using File::Tail and POE::Wheel::FollowTail and both work fine except for not allowing the file to roll-over.

As an experiment, I have tried using SFU's "tail -f" and that doesn't keep the rollover from occurring, but of course also doesn't follow the new file when a roll-over does occur.

I can think of several ways to periodically release and re-open the file, but it is too easy to miss an occurrance of the issue that way.

I will paste the applicable code from both methods I have tried below.

Any enlightenment would be greatly appreciated,
-Frd

File::Tail code:

my $file=File::Tail->new(name=>"$SEARCH_LOG", maxinterval=>10); LogIt("Initiated 'tail' ... reading file '$SEARCH_LOG':\n"); LogIt(" ... Looking for '$STRING'\n"); while (defined(my $line=$file->read)) { LogIt("Read line: $line"); next unless ( $line =~ /$STRING/ ); LogIt("That Line MATCHES SEARCH STRING: '$STRING'\n"); lock($occurred); $occurred ++; LogIt("$line"); LogIt("Increasing OCCURRED flag to '$occurred'"); }

POE code:

POE::Session->create( inline_states => { _start => sub { $_[HEAP]{tailor} = POE::Wheel::FollowTail->new( Filename => "$SEARCH_LOG", InputEvent => "got_log_line", ResetEvent => "got_log_rollover", ); }, got_log_line => sub { my $line = $_[ARG0]; LogIt("Read line: $line"); return unless ( $line =~ /$STRING/ ); LogIt("That Line MATCHES SEARCH STRING: '$STRING'\n"); lock($occurred); $occurred++; LogIt("$line"); LogIt("Increasing OCCURRED flag to '$occurred'"); }, got_log_rollover => sub { LogIt("Log rolled over.\n"); }, } );