Class: RemoteSyslog::FileTailReader

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_syslog/file_tail_reader.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FileTailReader.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/remote_syslog/file_tail_reader.rb', line 5

def initialize(path, options = {}, &block)
  @path = path
  @callback = options[:callback] || block
  @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 FileTailReader"

  start
end

Instance Method Details

#runObject



30
31
32
33
34
35
36
37
38
# File 'lib/remote_syslog/file_tail_reader.rb', line 30

def run
  File::Tail::Logfile.tail(@path) do |line|
    EventMachine.schedule do
      @callback.call(@tag, line)
    end
  end
rescue => e
  @logger.error "Unhandled FileTailReader Exception: #{e.class}: #{e.message}:\n\t#{e.backtrace.join("\n\t")}"
end

#startObject



24
25
26
27
28
# File 'lib/remote_syslog/file_tail_reader.rb', line 24

def start
  @thread = Thread.new do
    run
  end
end