a237cee6bc68f2c1226d37f63e17020712c0fed7
[feedcatcher.git] / vendor / rails / railties / lib / rails / rack / log_tailer.rb
1 module Rails
2 module Rack
3 class LogTailer
4 EnvironmentLog = "#{File.expand_path(Rails.root)}/log/#{Rails.env}.log"
5
6 def initialize(app, log = nil)
7 @app = app
8
9 path = Pathname.new(log || EnvironmentLog).cleanpath
10 @cursor = ::File.size(path)
11 @last_checked = Time.now.to_f
12
13 @file = ::File.open(path, 'r')
14 end
15
16 def call(env)
17 response = @app.call(env)
18 tail_log
19 response
20 end
21
22 def tail_log
23 @file.seek @cursor
24
25 mod = @file.mtime.to_f
26 if mod > @last_checked
27 contents = @file.read
28 @last_checked = mod
29 @cursor += contents.size
30 $stdout.print contents
31 end
32 end
33 end
34 end
35 end