aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/buffered_logger.rb
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2008-01-13 20:39:51 +0000
committerMichael Koziarski <michael@koziarski.com>2008-01-13 20:39:51 +0000
commit4b7091f32af7841b2d6d7cbef47d3849f48786c0 (patch)
treee5a6cd335eadc9f090164b154e3e5cffa8ad6890 /activesupport/lib/active_support/buffered_logger.rb
parent7e1c04d86691075d172bccb8bc1c7df2e71383c3 (diff)
downloadrails-4b7091f32af7841b2d6d7cbef47d3849f48786c0.tar.gz
rails-4b7091f32af7841b2d6d7cbef47d3849f48786c0.tar.bz2
rails-4b7091f32af7841b2d6d7cbef47d3849f48786c0.zip
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
Diffstat (limited to 'activesupport/lib/active_support/buffered_logger.rb')
-rw-r--r--activesupport/lib/active_support/buffered_logger.rb19
1 files changed, 16 insertions, 3 deletions
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