2008-12-18 17:42:06 -05:00
|
|
|
module Rails
|
|
|
|
module Rack
|
|
|
|
class LogTailer
|
|
|
|
def initialize(app, log = nil)
|
|
|
|
@app = app
|
|
|
|
|
2009-11-24 16:03:24 -05:00
|
|
|
path = Pathname.new(log || "#{File.expand_path(Rails.root)}/log/#{Rails.env}.log").cleanpath
|
2008-12-18 17:42:06 -05:00
|
|
|
@cursor = ::File.size(path)
|
|
|
|
@last_checked = Time.now.to_f
|
|
|
|
|
|
|
|
@file = ::File.open(path, 'r')
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
response = @app.call(env)
|
2010-01-15 04:46:30 -05:00
|
|
|
tail!
|
2008-12-18 17:42:06 -05:00
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2010-01-15 04:46:30 -05:00
|
|
|
def tail!
|
2008-12-18 17:42:06 -05:00
|
|
|
@file.seek @cursor
|
|
|
|
|
|
|
|
mod = @file.mtime.to_f
|
|
|
|
if mod > @last_checked
|
|
|
|
contents = @file.read
|
|
|
|
@last_checked = mod
|
|
|
|
@cursor += contents.size
|
|
|
|
$stdout.print contents
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|