in reply to Re: Parse Log File As Written
in thread Parse Log File As Written
Found it (p.779 of Programming Perl, in case you want to play at home), but it's not actually that robust in that it has the problem I mentioned earlier with partially-written lines.
So give this a try:
The mucking about with $logpos/last is to keep track of the last line break you saw so that the next iteration of the outer while will always pick up from there instead of in the middle of a line, even if the file is being written to at the same time as you read the last line and get a partial line because of it.open $logfh, '<', $logfile; my $logpos; while (1) { while (my $line = <$logfh>) { last unless substr($line, -1) eq "\n"; # May need to tweak for Win +32 print $line; $logpos = tell $logfh; } sleep 1; seek $logfh, $logpos, 0; }
You might also want to store $logpos somewhere nonvolatile (config file, database, etc.) so that you don't have to restart from the beginning of the file when this program eventually exits/dies and has to be restarted for whatever reason.
And, yeah, I'm not surprised by the locking issues you ran into. Windows has always enforced (IMO ridiculous) restrictions on using locked files. It's the major reason for all the "you changed something - please reboot to continue" nonsense that people make jokes about. If it's locked, Windows won't allow it to be changed and, based on my reading of the Tie::File docs, it only updates the file's size and array when it is tied or flocked (which is perfectly reasonable, since it shouldn't be spending all its time constantly re-reading the file, just in case it might have changed).
(Note that I have not actually run the code posted above, but I recently did something extremely similar, so it should work, just so long as Win32 Perl supports seek/tell properly.)
|
|---|