Sha256: e9fcd89d5246710348247611754f606557296ab71fd0d25566abc1bc12f3e64a

Contents?: true

Size: 819 Bytes

Versions: 1

Compression:

Stored size: 819 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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remote_syslog-1.6.6.rc1 lib/remote_syslog/file_tail_reader.rb