perlquestion
frd1963
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.
<P>
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.
<P>
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.
<P>
I will paste the applicable code from both methods I have tried below.<br>
<P>Any enlightenment would be greatly appreciated,<br>
-Frd<BR>
<P>
<B><U>File::Tail code:</U></B>
<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'");
}
</code>
<P>
<B><U>POE code:</U></B>
<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");
},
}
);
</code>