Module: File::Tail
- Included in:
- Logfile
- Defined in:
- lib/file/tail.rb
Overview
The File::Tail module can be included in File objects and mixes in the forward, backward and tail methods.
Defined Under Namespace
Classes: BreakException, DeletedException, Logfile, ReopenException, ReturnException, TailException
Instance Attribute Summary collapse
-
#break_if_eof ⇒ Object
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 ⇒ Object
The start value of the sleep interval.
-
#max_interval ⇒ Object
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 ⇒ Object
If this attribute is set to a true value, File::Tail persists on reopening a deleted file waiting
max_intervalseconds between the attempts. -
#reopen_suspicious ⇒ Object
If this attribute is set to a true value, File::Tail attempts to reopen it’s tailed file after
suspicious_intervalseconds of silence. -
#return_if_eof ⇒ Object
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 ⇒ Object
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.
Instance Method Summary collapse
-
#backward(n = 0, bufsiz = nil) ⇒ Object
Rewind the last
nlines of this file, starting from the end. -
#forward(n = 0) ⇒ Object
Skip the first
nlines of this file. -
#tail(n = nil, &block) ⇒ Object
This method tails this file and yields to the given block for every new line that is read.
-
#wind(*args) ⇒ Object
The wind method is deprecated, use forward instead.
Instance Attribute Details
#break_if_eof ⇒ Object
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.
201 202 203 |
# File 'lib/file/tail.rb', line 201 def break_if_eof @break_if_eof end |
#interval ⇒ Object
The start value of the sleep interval. This value goes against max_interval if the tailed file is silent for a sufficient time.
176 177 178 |
# File 'lib/file/tail.rb', line 176 def interval @interval end |
#max_interval ⇒ Object
The maximum interval File::Tail sleeps, before it tries to take some action like reading the next few lines or reopening the file.
171 172 173 |
# File 'lib/file/tail.rb', line 171 def max_interval @max_interval end |
#reopen_deleted ⇒ Object
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.
183 184 185 |
# File 'lib/file/tail.rb', line 183 def reopen_deleted @reopen_deleted end |
#reopen_suspicious ⇒ Object
If this attribute is set to a true value, File::Tail attempts to reopen it’s tailed file after suspicious_interval seconds of silence.
188 189 190 |
# File 'lib/file/tail.rb', line 188 def reopen_suspicious @reopen_suspicious end |
#return_if_eof ⇒ Object
If this attribute is set to a true value, File::Fail’s tail method just returns if the end of the file is reached.
205 206 207 |
# File 'lib/file/tail.rb', line 205 def return_if_eof @return_if_eof end |
#suspicious_interval ⇒ Object
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.
197 198 199 |
# File 'lib/file/tail.rb', line 197 def suspicious_interval @suspicious_interval end |
Instance Method Details
#backward(n = 0, bufsiz = nil) ⇒ Object
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.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/file/tail.rb', line 233 def backward(n = 0, bufsiz = nil) if n <= 0 seek(0, File::SEEK_END) return self end bufsiz ||= stat.blksize || 8192 size = stat.size begin if bufsiz < size seek(0, File::SEEK_END) while n > 0 and tell > 0 do start = tell seek(-bufsiz, File::SEEK_CUR) buffer = read(bufsiz) n -= buffer.count("\n") seek(-bufsiz, File::SEEK_CUR) end else seek(0, File::SEEK_SET) buffer = read(size) n -= buffer.count("\n") seek(0, File::SEEK_SET) end rescue Errno::EINVAL size = tell retry end pos = -1 while n < 0 # forward if we are too far back pos = buffer.index("\n", pos + 1) n += 1 end seek(pos + 1, File::SEEK_CUR) self end |
#forward(n = 0) ⇒ Object
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.
209 210 211 212 213 214 215 216 |
# File 'lib/file/tail.rb', line 209 def forward(n = 0) seek(0, File::SEEK_SET) while n > 0 and not eof? readline n -= 1 end self end |
#tail(n = nil, &block) ⇒ Object
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.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/file/tail.rb', line 281 def tail(n = nil, &block) # :yields: line result = [] array_result = false unless block block = lambda { |line| result << line } array_result = true end preset_atts unless @lines loop do begin restat read_line(n, &block) redo rescue ReopenException => e reopen_file(e.mode) rescue ReturnException return array_result ? result : nil end end end |
#wind(*args) ⇒ Object
The wind method is deprecated, use forward instead.
219 220 221 222 |
# File 'lib/file/tail.rb', line 219 def wind(*args) warn "File::Tail#wind method is deprecated, use forward instead" forward *args end |