Permalink
Browse files

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
  • Loading branch information...
1 parent 7e1c04d commit 4b7091f32af7841b2d6d7cbef47d3849f48786c0 @NZKoz NZKoz committed Jan 13, 2008
Showing with 20 additions and 4 deletions.
  1. +16 −3 activesupport/lib/active_support/buffered_logger.rb
  2. +4 −1 railties/lib/initializer.rb
@@ -39,6 +39,7 @@ def initialize(log, level = DEBUG)
@level = level
@buffer = []
@auto_flushing = 1
+ @no_block = false
if log.respond_to?(:write)
@log = log
@@ -52,13 +53,19 @@ def initialize(log, level = DEBUG)
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 @@ def auto_flushing=(period)
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 @@ def close
protected
def auto_flush
- flush if @buffer.size >= @auto_flushing
+ flush if buffer.size >= @auto_flushing
end
end
end
@@ -250,7 +250,10 @@ def initialize_logger
begin
logger = ActiveSupport::BufferedLogger.new(configuration.log_path)
logger.level = ActiveSupport::BufferedLogger.const_get(configuration.log_level.to_s.upcase)
- logger.auto_flushing = false if configuration.environment == "production"
+ if configuration.environment == "production"
+ logger.auto_flushing = false
+ logger.set_non_blocking_io
+ end
rescue StandardError =>e
logger = ActiveSupport::BufferedLogger.new(STDERR)
logger.level = ActiveSupport::BufferedLogger::WARN

0 comments on commit 4b7091f

Please sign in to comment.