Sha256: b7c72ceed47325ed7624cbf1603252240364e8195ad166c166a980725f23936f

Contents?: true

Size: 1.16 KB

Versions: 16

Compression:

Stored size: 1.16 KB

Contents

require 'eventmachine'
require 'eventmachine-tail'
require 'em-dns-resolver'

# Force eventmachine-tail not to change the encoding
# This will allow ruby 1.9 to deal with any file data
old_verbose, $VERBOSE = $VERBOSE, nil
EventMachine::FileTail::FORCE_ENCODING = false
$VERBOSE = old_verbose

module RemoteSyslog
  class EventMachineReader < EventMachine::FileTail
    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

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

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

Version data entries

16 entries across 16 versions & 1 rubygems

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