From 4b7091f32af7841b2d6d7cbef47d3849f48786c0 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Sun, 13 Jan 2008 20:39:51 +0000 Subject: Use non-blocking writing if available. Closes #10794 [lifofifo] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8638 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/lib/active_support/buffered_logger.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'activesupport/lib/active_support/buffered_logger.rb') diff --git a/activesupport/lib/active_support/buffered_logger.rb b/activesupport/lib/active_support/buffered_logger.rb index c56dccc1f7..c46386c6b0 100644 --- a/activesupport/lib/active_support/buffered_logger.rb +++ b/activesupport/lib/active_support/buffered_logger.rb @@ -39,6 +39,7 @@ module ActiveSupport @level = level @buffer = [] @auto_flushing = 1 + @no_block = false if log.respond_to?(:write) @log = log @@ -52,13 +53,19 @@ module ActiveSupport end end + def set_non_blocking_io + if !RUBY_PLATFORM.match(/java|mswin/) && !(@log == STDOUT) && @log.respond_to?(:write_nonblock) + @no_block = true + end + end + def add(severity, message = nil, progname = nil, &block) return if @level > severity message = (message || (block && block.call) || progname).to_s # If a newline is necessary then create a new message ending with a newline. # Ensures that the original message is not mutated. message = "#{message}\n" unless message[-1] == ?\n - @buffer << message + buffer << message auto_flush message end @@ -90,7 +97,13 @@ module ActiveSupport end def flush - @log.write(@buffer.slice!(0..-1).join) unless @buffer.empty? + unless buffer.empty? + if @no_block + @log.write_nonblock(buffer.slice!(0..-1).join) + else + @log.write(buffer.slice!(0..-1).join) + end + end end def close @@ -101,7 +114,7 @@ module ActiveSupport protected def auto_flush - flush if @buffer.size >= @auto_flushing + flush if buffer.size >= @auto_flushing end end end -- cgit v1.2.3