Skip to content

Commit

Permalink
fix mixed encoding logs can't be logged.
Browse files Browse the repository at this point in the history
[#4807 state:committed]

Signed-off-by: Kouhei Sutou <kou@cozmixng.org>
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
kou authored and jeremy committed Jul 19, 2010
1 parent e466354 commit a6e95ba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 5 additions & 1 deletion activesupport/lib/active_support/buffered_logger.rb
Expand Up @@ -101,7 +101,11 @@ def flush
@guard.synchronize do
unless buffer.empty?
old_buffer = buffer
@log.write(old_buffer.join)
all_content = StringIO.new
old_buffer.each do |content|
all_content << content
end
@log.write(all_content.string)
end

# Important to do this even if buffer was empty or else @buffer will
Expand Down
15 changes: 15 additions & 0 deletions activesupport/test/buffered_logger_test.rb
@@ -1,9 +1,12 @@
require 'abstract_unit'
require 'multibyte_test_helpers'
require 'stringio'
require 'fileutils'
require 'active_support/buffered_logger'

class BufferedLoggerTest < Test::Unit::TestCase
include MultibyteTestHelpers

Logger = ActiveSupport::BufferedLogger

def setup
Expand Down Expand Up @@ -146,4 +149,16 @@ def test_flush_should_remove_empty_buffers
@logger.expects :clear_buffer
@logger.flush
end

def test_buffer_multibyte
@logger.auto_flushing = 2
@logger.info(UNICODE_STRING)
@logger.info(BYTE_STRING)
assert @output.string.include?(UNICODE_STRING)
byte_string = @output.string.dup
if byte_string.respond_to?(:force_encoding)
byte_string.force_encoding("ASCII-8BIT")
end
assert byte_string.include?(BYTE_STRING)
end
end
5 changes: 4 additions & 1 deletion activesupport/test/multibyte_test_helpers.rb
Expand Up @@ -4,6 +4,9 @@ module MultibyteTestHelpers
UNICODE_STRING = 'こにちわ'
ASCII_STRING = 'ohayo'
BYTE_STRING = "\270\236\010\210\245"
if BYTE_STRING.respond_to?(:force_encoding)
BYTE_STRING.force_encoding("ASCII-8BIT")
end

def chars(str)
ActiveSupport::Multibyte::Chars.new(str)
Expand All @@ -16,4 +19,4 @@ def inspect_codepoints(str)
def assert_equal_codepoints(expected, actual, message=nil)
assert_equal(inspect_codepoints(expected), inspect_codepoints(actual), message)
end
end
end

0 comments on commit a6e95ba

Please sign in to comment.