Class: RemoteSyslog::EventMachineReader

Inherits:
EventMachine::FileTail
  • Object
show all
Defined in:
lib/remote_syslog/eventmachine_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}, &block) ⇒ EventMachineReader

Returns a new instance of EventMachineReader.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/remote_syslog/eventmachine_reader.rb', line 13

def initialize(path, options = {}, &block)
  @callback = options[:callback] || block
  @buffer = BufferedTokenizer.new
  @logger = options[:logger] || Logger.new(STDERR)

  @tag = options[:program] || File.basename(path)

  # Remove characters that can't be in a tag
  @tag = @tag.gsub(%r{[: \]\[\\]+}, '-')

  # Make sure the tag isn't too long
  if @tag.length > 32
    @tag = @tag[0..31]
  end

  @logger.debug "Watching #{path} with EventMachineReader"

  super(path, -1)
end

Instance Method Details

#on_exception(exception) ⇒ Object



39
40
41
# File 'lib/remote_syslog/eventmachine_reader.rb', line 39

def on_exception(exception)
  @logger.error "Exception: #{exception.class}: #{exception.message}\n\t#{exception.backtrace.join("\n\t")}"
end

#receive_data(data) ⇒ Object



33
34
35
36
37
# File 'lib/remote_syslog/eventmachine_reader.rb', line 33

def receive_data(data)
  @buffer.extract(data).each do |line|
    @callback.call(@tag, line)
  end
end