Sha256: cd0c65d7e14750c387885891675ad05ca14d7667ba34eb2e996e106a1e2bf718

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

# $Id: io.rb 96 2008-02-10 18:18:10Z tim_pease $

module Logging::Appenders

  # This class provides an Appender that can write to any IO stream
  # configured for writing.
  #
  class IO < ::Logging::Appender

    # call-seq:
    #    IO.new( name, io )
    #    IO.new( name, io, :layout => layout )
    #
    # Creates a new IO Appender using the given name that will use the _io_
    # stream as the logging destination.
    #
    def initialize( name, io, opts = {} )
      unless io.respond_to? :print
        raise TypeError, "expecting an IO object but got '#{io.class.name}'"
      end

      @io = io
      @io.sync = true
      super(name, opts)
    end

    # call-seq:
    #    close( footer = true )
    #
    # Close the appender and writes the layout footer to the logging
    # destination if the _footer_ flag is set to +true+. Log events will
    # no longer be written to the logging destination after the appender
    # is closed.
    #
    def close( *args )
      return self if @io.nil?
      super(*args)
      io, @io = @io, nil
      io.close unless [STDIN, STDERR, STDOUT].include?(io)
    rescue IOError => err
    ensure
      return self
    end

    # call-seq:
    #    flush
    #
    # Call +flush+ to force an appender to write out any buffered log events.
    # Similar to IO#flush, so use in a similar fashion.
    #
    def flush
      return self if @io.nil?
      @io.flush
      self
    end


    private

    # call-seq:
    #    write( event )
    #
    # Writes the given _event_ to the IO stream. If an +IOError+ is detected,
    # than this appender will be turned off and the error reported.
    #
    def write( event )
      begin
        str = event.instance_of?(::Logging::LogEvent) ?
              @layout.format(event) : event.to_s
        return if str.empty?
        @io.print str
      rescue IOError
        self.level = :off
        raise
      end
    end

  end  # class IO
end  # module Logging::Appenders

# EOF

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
logging-0.7.0 lib/logging/appenders/io.rb
logging-0.7.1 lib/logging/appenders/io.rb