flori / file-tail

File::Tail for Ruby
http://flori.github.com/file-tail
Apache License 2.0
160 stars 19 forks source link

reopen_file(:top) should reset lineno #3

Closed kk1987 closed 12 years ago

kk1987 commented 12 years ago

via a rewind or self.lineno=0.

For example, I'm using the following monkey patch now

class File; module Tail
  alias old_reopen_file reopen_file
  def reopen_file mode; old_reopen_file(mode); rewind if mode == :top; end
end; end
flori commented 12 years ago

Maybe you're right. Can you describe what kind of problems not rewinding caused you?

kk1987 commented 12 years ago

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.

flori commented 12 years ago

Can you try the new 1.0.9 version and see if it behaves better?

kk1987 commented 12 years ago

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

flori commented 12 years ago

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?

kk1987 commented 12 years ago

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 :).

flori commented 12 years ago

Great, at least semi-correct values don't hurt.