Module File::Tail
In: lib/file/tail.rb
lib/file/tail/version.rb

File::Tail - Tailing files in Ruby

Description

This is a small ruby library that allows it to "tail" files in Ruby, including following a file, that still is growing like the unix command ‘tail -f’ can.

Author

Florian Frank flori@ping.de

License

This is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License Version 2 as published by the Free Software Foundation: www.gnu.org/copyleft/gpl.html

Download

The latest version of File::Tail (file-tail) can be found at

rubyforge.org/frs/?group_id=393

Online Documentation should be located at

file-tail.rubyforge.org

Usage

File::Tail is a module in the File class. A lightweight class interface for logfiles can be seen under File::Tail::Logfile.

Direct extension of File objects with File::Tail works like that:

 File.open(filename) do |log|
   log.extend(File::Tail)
   log.interval = 10
   log.backward(10)
   log.tail { |line| puts line }
 end

It‘s also possible to mix File::Tail in your own File classes (see also File::Tail::Logfile):

 class MyFile < File
   include File::Tail
 end
 log = MyFile.new("myfile")
 log.interval = 10
 log.backward(10)
 log.tail { |line| print line }

The forward/backward method returns self, so it‘s possible to chain methods together like that:

 log.backward(10).tail { |line| puts line }

Methods

after_reopen   backward   forward   tail  

Classes and Modules

Class File::Tail::BreakException
Class File::Tail::DeletedException
Class File::Tail::Logfile
Class File::Tail::ReopenException
Class File::Tail::ReturnException
Class File::Tail::TailException

Constants

VERSION = '0.1.5'   File::Tail version

Attributes

break_if_eof  [RW]  If this attribute is set to a true value, File::Fail‘s tail method raises a BreakException if the end of the file is reached.
interval  [RW]  The start value of the sleep interval. This value goes against max_interval if the tailed file is silent for a sufficient time.
max_interval  [RW]  The maximum interval File::Tail sleeps, before it tries to take some action like reading the next few lines or reopening the file.
reopen_deleted  [RW]  If this attribute is set to a true value, File::Tail persists on reopening a deleted file waiting max_interval seconds between the attempts. This is useful if logfiles are moved away while rotation occurs but are recreated at the same place after a while. It defaults to true.
reopen_suspicious  [RW]  If this attribute is set to a true value, File::Tail attempts to reopen it‘s tailed file after suspicious_interval seconds of silence.
return_if_eof  [RW]  If this attribute is set to a true value, File::Fail‘s tail method just returns if the end of the file is reached.
suspicious_interval  [RW]  This attribute is the invterval in seconds before File::Tail gets suspicious that something has happend to it‘s tailed file and an attempt to reopen it is made.

If the attribute reopen_suspicious is set to a non true value, suspicious_interval is meaningless. It defaults to 60 seconds.

Public Instance methods

The callback is called with self as an argument after a reopen has occured. This allows a tailing script to find out, if a logfile has been rotated.

Rewind the last n lines of this file, starting from the end. The default is to start tailing directly from the end of the file.

The additional argument bufsiz is used to determine the buffer size that is used to step through the file backwards. It defaults to the block size of the filesystem this file belongs to or 8192 bytes if this cannot be determined.

Skip the first n lines of this file. The default is to don‘t skip any lines at all and start at the beginning of this file.

This method tails this file and yields to the given block for every new line that is read. If no block is given an array of those lines is returned instead. (In this case it‘s better to use a reasonable value for n or set the return_if_eof or break_if_eof attribute to a true value to stop the method call from blocking.)

If the argument n is given, only the next n lines are read and the method call returns. Otherwise this method call doesn‘t return, but yields to block for every new line read from this file for ever.

[Validate]