fix process#alive? to not raise on no such file
[god.git] / lib / god / logger.rb
blobd2d9b98c9845fedfb8ae02ad7ffc8dadb18bf11b
1 module God
2   
3   class Logger < ::Logger
4     attr_accessor :logs
5     
6     def initialize
7       super(STDOUT)
8       self.logs = {}
9       @mutex = Mutex.new
10     end
11     
12     def log(watch, level, text)
13       # initialize watch log if necessary
14       self.logs[watch.name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT)
15       
16       # push onto timeline for the given watch
17       buf = StringIO.new
18       templog = ::Logger.new(buf)
19       templog.send(level, text)
20       @mutex.synchronize do
21         self.logs[watch.name] << [Time.now, buf.string]
22       end
23       templog.close
24       
25       # send to regular logger
26       self.send(level, text)
27     end
28     
29     def watch_log_since(watch_name, since)
30       # initialize watch log if necessary
31       self.logs[watch_name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT)
32       
33       # get and join lines since given time
34       @mutex.synchronize do
35         self.logs[watch_name].select do |x|
36           x.first > since
37         end.map do |x|
38           x[1]
39         end.join
40       end
41     end
42   end
43   
44 end