vendor/rails/activesupport/lib/active_support/buffered_logger.rb in radiant-0.6.9 vs vendor/rails/activesupport/lib/active_support/buffered_logger.rb in radiant-0.7.0

- old
+ new

@@ -37,30 +37,38 @@ def initialize(log, level = DEBUG) @level = level @buffer = [] @auto_flushing = 1 + @no_block = false if log.respond_to?(:write) @log = log elsif File.exist?(log) @log = open(log, (File::WRONLY | File::APPEND)) @log.sync = true else + FileUtils.mkdir_p(File.dirname(log)) @log = open(log, (File::WRONLY | File::APPEND | File::CREAT)) @log.sync = true @log.write("# Logfile created on %s" % [Time.now.to_s]) 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 for severity in Severity.constants @@ -88,20 +96,26 @@ else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}" end end def flush - @log.write(@buffer.slice!(0..-1).to_s) 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 flush @log.close if @log.respond_to?(:close) @log = nil end protected def auto_flush - flush if @buffer.size >= @auto_flushing + flush if buffer.size >= @auto_flushing end end end