Sha256: dd8803b6a7a023c16abd434e4f9a8717c16641cd5885e40cfd776ac689775230

Contents?: true

Size: 1.36 KB

Versions: 31

Compression:

Stored size: 1.36 KB

Contents

module Logging::Appenders

  # This class provides an Appender that can write to a StringIO instance.
  # This is very useful for testing log message output.
  #
  class StringIo < ::Logging::Appenders::IO

    # The StringIO instance the appender is writing to.
    attr_reader :sio

    # call-seq:
    #    StringIo.new( name, opts = {} )
    #
    # Creates a new StrinIo appender that will append log messages to a
    # StringIO instance.
    #
    def initialize( name, opts = {} )
      @sio = StringIO.new
      @sio.extend IoToS
      @pos = 0
      super(name, @sio, opts)
    end

    # Clears the internal StringIO instance. All log messages are removed
    # from the buffer.
    #
    def clear
      sync {
        @pos = 0
        @sio.seek 0
        @sio.truncate 0
      }
    end
    alias :reset :clear

    %w[read readline readlines].each do|m|
      class_eval <<-CODE
        def #{m}( *args )
          sync {
            begin
              @sio.seek @pos
              rv = @sio.#{m}(*args)
              @pos = @sio.tell
              rv
            rescue EOFError
              nil
            end
          }
        end
      CODE
    end

    # :stopdoc:
    module IoToS
      def to_s
        seek 0
        str = read
        seek 0
        return str
      end
    end
    # :startdoc:

  end  # class StringIo
end  # module Logging::Appenders

# EOF

Version data entries

31 entries across 31 versions & 5 rubygems

Version Path
TwP-logging-0.9.8.1 lib/logging/appenders/string_io.rb
TwP-logging-0.9.8.2 lib/logging/appenders/string_io.rb
TwP-logging-1.0.0 lib/logging/appenders/string_io.rb
TwP-logging-1.1.0 lib/logging/appenders/string_io.rb
TwP-logging-1.1.1 lib/logging/appenders/string_io.rb
TwP-logging-1.1.2 lib/logging/appenders/string_io.rb
TwP-logging-1.1.3 lib/logging/appenders/string_io.rb
TwP-logging-1.1.4 lib/logging/appenders/string_io.rb
TwP-logging-1.2.0 lib/logging/appenders/string_io.rb
TwP-logging-1.2.2 lib/logging/appenders/string_io.rb
pjstadig-logging-1.1.4.1 lib/logging/appenders/string_io.rb
logging-1.4.3 lib/logging/appenders/string_io.rb
sgeorgi-logging-1.4.2 lib/logging/appenders/string_io.rb
logging-1.4.2 lib/logging/appenders/string_io.rb
logging-1.4.1 lib/logging/appenders/string_io.rb
logging-1.4.0 lib/logging/appenders/string_io.rb
redcar-0.3.1dev lib/logging/lib/logging/appenders/string_io.rb
redcar-0.3.0dev lib/logging/lib/logging/appenders/string_io.rb
logging-1.3.0 lib/logging/appenders/string_io.rb
redcar-0.2.9dev lib/logging/lib/logging/appenders/string_io.rb