Sha256: e55400639c23290a6250265d0a5b8371e73f1c8cbbb4ee1b3512873dfe3cbdc7

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'stringio'

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
      super(name, @sio, opts)
      clear
    end

    # Read a single line of text from the internal StringIO instance. +nil+
    # is returned if the StringIO buffer is empty.
    #
    def readline
      sync {
        begin
          @sio.seek @pos
          line = @sio.readline
          @pos = @sio.tell
          line
        rescue EOFError
          nil
        end
      }
    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

    # :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

1 entries across 1 versions & 1 rubygems

Version Path
TwP-logging-0.9.8 lib/logging/appenders/string_io.rb