Closed kk1987 closed 12 years ago
Maybe you're right. Can you describe what kind of problems not rewinding caused you?
I use file-tail to read some log4j-generated logs, which are automatically rotated each day. In the meantime, my script writes the line number already read to a file when interrupted - in order to resume from that exact location when restarted (via a log_file.forward(saved_lineno)
).
The lack of rewinding causes this mechanism to fail after a log rotation, as the lineno value is incorrect.
Can you try the new 1.0.9 version and see if it behaves better?
No the new version isn't doing better... Seems like IO#seek (at file/tail.rb#L100) won't reset lineno; it have to be a IO#rewind or IO#lineno=.
BTW here's how I tested:
require 'file/tail'
File.open('test') do |log|
log.extend(File::Tail); log.interval = 10
log.tail { |line| puts "#{log.lineno} #{line}" }
end
then
mv test tmp; cp tmp test
Ah, I have now rewound it, but actually lineno returns the number of gets calls, and not necessarily the number of lines (in the case if you pass another delimiter to gets.) It's actually really difficult to set the correct values for the forward and backward cases after reopening, usually files are moved or truncated to empty, but it would be possible to just shrink them instead. Can you try the newest 1.0.10 version?
Thanks for the update! The 1.0.10 version serve my need very well.
You're right that it's hard to correlate lineno to actual line number, but even a semi-correct value is better than none :).
Great, at least semi-correct values don't hurt.
via a
rewind
orself.lineno=0
.For example, I'm using the following monkey patch now