class Rack::Logs::Viewer::JoinedFiles

Public Class Methods

new(filenames, lines) click to toggle source
# File lib/rack/logs/viewer.rb, line 30
def initialize filenames, lines
  @filenames = filenames
  @lines = lines
end

Public Instance Methods

each(&block) click to toggle source
# File lib/rack/logs/viewer.rb, line 39
def each &block
  @filenames.each do |filename|
    block.call "## #{filename}\n\n"
    ::File.open(filename) do |file|
      tail(file, @lines).each do |line|
        block.call line
      end
    end
  end
end
empty?() click to toggle source
# File lib/rack/logs/viewer.rb, line 35
def empty?
  @filenames.empty?
end

Private Instance Methods

tail(file, n) click to toggle source
# File lib/rack/logs/viewer.rb, line 52
def tail file, n
  index = 1
  lines = 0
  buffer = ""

  if file.size > 1024
    begin
      file.seek(index * (1024 * -1), IO::SEEK_END)
      chunk = file.read(1024)
      lines += chunk.count("\n")
      buffer.prepend chunk
      index += 1
    end while lines < n && file.pos >= 0
  else
    buffer = file.read
  end

  buffer.lines.to_a.pop n
end