Sha256: 32eafe5d010be04883539bca98c216b86801e7eada481a09712d54c7c09202e1

Contents?: true

Size: 950 Bytes

Versions: 15

Compression:

Stored size: 950 Bytes

Contents

require 'file/tail'

module RemoteSyslog
  class FileTailReader
    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

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

    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
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
remote_syslog-1.6.15 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.14 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.13 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.12 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.11 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.10 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.9 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.8 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.7.2 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.7.1 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.7 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.7.pre1 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.6.1 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.6 lib/remote_syslog/file_tail_reader.rb
remote_syslog-1.6.6.rc2 lib/remote_syslog/file_tail_reader.rb