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)
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2010-10-16 23:12:19 -04:00
|
|
|
unless @file.eof?
|
2008-12-18 17:42:06 -05:00
|
|
|
contents = @file.read
|
2010-07-28 20:34:04 -04:00
|
|
|
@cursor = @file.tell
|
2008-12-18 17:42:06 -05:00
|
|
|
$stdout.print contents
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|