Sha256: 1516950e85d45c3bf3510e56b2e0acae9da744adb45cf28b1ec49ff0a178fa8d

Contents?: true

Size: 1.46 KB

Versions: 17

Compression:

Stored size: 1.46 KB

Contents

require "cabin/namespace"

# This module provides a 'pipe' method which instructs cabin to pipe anything
# read from the IO given to be logged.
module Cabin::Mixins::Pipe

  # Pipe IO objects to method calls on a logger.
  #
  # The argument is a hash of IO to method symbols. 
  #
  #     logger.pipe(io => :the_method)
  #
  # For each line read from 'io', logger.the_method(the_line) will be called.
  #
  # Example:
  #
  #     cmd = "strace -e trace=write date"
  #     Open4::popen4(cmd) do |pid, stdin, stdout, stderr|
  #       stdin.close
  #
  #       # Make lines from stdout be logged as 'info'
  #       # Make lines from stderr be logged as 'error'
  #       logger.pipe(stdout => :info, stderr => :error)
  #     end
  #
  # Output:
  #
  #     write(1, "Fri Jan 11 22:49:42 PST 2013\n", 29) = 29 {"level":"error"}
  #     Fri Jan 11 22:49:42 PST 2013 {"level":"info"}
  #     +++ exited with 0 +++ {"level":"error"}
  def pipe(io_to_method_map, &block)
    fds = io_to_method_map.keys

    while !fds.empty?
      readers, _, _ = IO.select(fds, nil, nil, nil)
      readers.each do |fd|
        begin
          line = fd.readline.chomp
        rescue EOFError
          fd.close rescue nil
          fds.delete(fd)
          next
        end

        method_name = io_to_method_map[fd]
        block.call(line, method_name) if block_given?
        send(method_name, line)
      end # readers.each
    end # while !fds.empty?
  end # def pipe
end # module Cabin::Mixins::Logger

Version data entries

17 entries across 15 versions & 6 rubygems

Version Path
logstash-filter-zabbix-0.1.2 vendor/bundle/jruby/1.9/gems/cabin-0.8.1/lib/cabin/mixins/pipe.rb
logstash-filter-zabbix-0.1.1 vendor/bundle/jruby/1.9/gems/cabin-0.8.1/lib/cabin/mixins/pipe.rb
ivanvc-logstash-input-s3-3.1.1.4 vendor/local/gems/cabin-0.8.1/lib/cabin/mixins/pipe.rb
ivanvc-logstash-input-s3-3.1.1.3 vendor/local/gems/cabin-0.8.1/lib/cabin/mixins/pipe.rb
ivanvc-logstash-input-s3-3.1.1.2 vendor/local/gems/cabin-0.8.1/lib/cabin/mixins/pipe.rb
cabin-0.9.0 lib/cabin/mixins/pipe.rb
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/cabin-0.7.1/lib/cabin/mixins/pipe.rb
cabin-0.8.1 lib/cabin/mixins/pipe.rb
cabin-0.8.0 lib/cabin/mixins/pipe.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/cabin-0.7.1/lib/cabin/mixins/pipe.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/cabin-0.7.2/lib/cabin/mixins/pipe.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/cabin-0.7.2/lib/cabin/mixins/pipe.rb
logstash-codec-json-2.0.3 vendor/gems/cabin-0.7.2/lib/cabin/mixins/pipe.rb
cabin-0.7.2 lib/cabin/mixins/pipe.rb
logstash-input-beats-0.9.2 vendor/jruby/1.9/gems/cabin-0.7.1/lib/cabin/mixins/pipe.rb
logstash-input-beats-0.9.1 vendor/jruby/1.9/gems/cabin-0.7.1/lib/cabin/mixins/pipe.rb
cabin-0.7.1 lib/cabin/mixins/pipe.rb