lib/logging/appenders/string_io.rb in logging-0.9.7 vs lib/logging/appenders/string_io.rb in logging-0.9.8

- old
+ new

@@ -17,30 +17,15 @@ # 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 { @@ -48,9 +33,37 @@ @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