Class: Grok::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/grok/watcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&b) ⇒ Watcher

Returns a new instance of Watcher.



10
11
12
13
14
15
# File 'lib/grok/watcher.rb', line 10

def initialize(&b)
  @events = {}
  @config = Config.new("/var/log/messages", 10)

  #instance_eval(&b) if block_given?
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/grok/watcher.rb', line 8

def config
  @config
end

#fileObject

Returns the value of attribute file.



8
9
10
# File 'lib/grok/watcher.rb', line 8

def file
  @file
end

#intervalObject

Returns the value of attribute interval.



8
9
10
# File 'lib/grok/watcher.rb', line 8

def interval
  @interval
end

#matchObject

Returns the value of attribute match.



8
9
10
# File 'lib/grok/watcher.rb', line 8

def match
  @match
end

#replayObject

Returns the value of attribute replay.



8
9
10
# File 'lib/grok/watcher.rb', line 8

def replay
  @replay
end

Instance Method Details

#configure(&b) ⇒ Object



17
18
19
# File 'lib/grok/watcher.rb', line 17

def configure(&b)
  b.call(@config)
end

#on(match, opts = {}, &block) ⇒ Object



21
22
23
24
25
# File 'lib/grok/watcher.rb', line 21

def on(match, opts={}, &block)
  event = :log
  match = match.to_s if match.is_a? Integer
  (@events[event] ||= []) << [Regexp.new(match), block]
end

#startObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/grok/watcher.rb', line 27

def start
  File.open(@config.file) do |log|
    log.extend(File::Tail)
    log.interval = @config.interval
    log.backward(@config.replay)
    log.tail { |line|
      dispatch(:log, line)
    }
  end
end