Skip to content

Commit 5f00514

Browse files
committed
don't create copies of STDOUT/STDERR
1 parent 39a69fb commit 5f00514

File tree

3 files changed

+14
-53
lines changed

3 files changed

+14
-53
lines changed

lib/logging/appenders/console.rb

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,7 @@ def initialize( *args )
3737
# is currently open then it will be closed and immediately reopened.
3838
def reopen
3939
@mutex.synchronize {
40-
if defined? @io && @io
41-
flush
42-
# JRuby will close the underlying file descriptor, so we don't want to
43-
# do that even though this is a copy of STDOUT / STDERR
44-
if !defined?(JRUBY_VERSION)
45-
@io.close rescue nil
46-
end
47-
end
40+
flush if defined? @io && @io
4841
@io = open_fd
4942
}
5043
super
@@ -55,20 +48,13 @@ def reopen
5548

5649
def open_fd
5750
case self.class.name
58-
when "Logging::Appenders::Stdout"
59-
fd = STDOUT.fileno
60-
encoding = STDOUT.external_encoding
61-
when "Logging::Appenders::Stderr"
62-
fd = STDERR.fileno
63-
encoding = STDERR.external_encoding
51+
when "Logging::Appenders::Stdout"; STDOUT
52+
when "Logging::Appenders::Stderr"; STDERR
6453
else
6554
raise RuntimeError, "Please do not use the `Logging::Appenders::Console` class directly - " +
6655
"use `Logging::Appenders::Stdout` and `Logging::Appenders::Stderr` instead" +
6756
" [class #{self.class.name}]"
6857
end
69-
70-
mode = ::File::WRONLY | ::File::APPEND
71-
::IO.for_fd(fd, mode: mode, encoding: encoding)
7258
end
7359
end
7460

lib/logging/appenders/io.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,9 @@ def close( *args )
4848
super
4949

5050
io, @io = @io, nil
51-
52-
# JRuby will always close the underlying file descriptor, so we need to
53-
# check the FD number and not just the Ruby object ID
54-
close_io = if defined?(JRUBY_VERSION)
55-
![STDIN.fileno, STDERR.fileno, STDOUT.fileno].include?(io.fileno)
56-
else
57-
![STDIN, STDERR, STDOUT].include?(io)
51+
if ![STDIN, STDERR, STDOUT].include?(io)
52+
io.send(@close_method) if @close_method && io.respond_to?(@close_method)
5853
end
59-
60-
io.send(@close_method) if close_io && @close_method && io.respond_to?(@close_method)
6154
rescue IOError
6255
ensure
6356
return self

test/appenders/test_console.rb

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_initialize
2222
assert_equal 'stdout', appender.name
2323

2424
io = appender.instance_variable_get(:@io)
25-
refute_same STDOUT, io
25+
assert_same STDOUT, io
2626
assert_equal STDOUT.fileno, io.fileno
2727

2828
appender = Logging.appenders.stdout('foo')
@@ -45,25 +45,16 @@ def test_reopen
4545

4646
appender.close
4747
assert appender.closed?
48-
# We will not close the IO stream in JRuby
49-
if defined?(JRUBY_VERSION)
50-
refute io.closed?
51-
else
52-
assert io.closed?
53-
end
48+
refute io.closed?
5449
refute STDOUT.closed?
5550

5651
appender.reopen
5752
refute appender.closed?
5853

5954
new_io = appender.instance_variable_get(:@io)
60-
refute_same io, new_io
55+
assert_same io, new_io
6156
refute new_io.closed?
62-
if defined?(JRUBY_VERSION)
63-
refute io.closed?
64-
else
65-
assert io.closed?
66-
end
57+
refute io.closed?
6758
end
6859
end
6960

@@ -77,8 +68,8 @@ def test_initialize
7768
assert_equal 'stderr', appender.name
7869

7970
io = appender.instance_variable_get(:@io)
80-
refute_same STDERR, io
81-
assert_same STDERR.fileno, io.fileno
71+
assert_same STDERR, io
72+
assert_equal STDERR.fileno, io.fileno
8273

8374
appender = Logging.appenders.stderr('foo')
8475
assert_equal 'foo', appender.name
@@ -100,25 +91,16 @@ def test_reopen
10091

10192
appender.close
10293
assert appender.closed?
103-
# We will not close the IO stream in JRuby
104-
if defined?(JRUBY_VERSION)
105-
refute io.closed?
106-
else
107-
assert io.closed?
108-
end
94+
refute io.closed?
10995
refute STDERR.closed?
11096

11197
appender.reopen
11298
refute appender.closed?
11399

114100
new_io = appender.instance_variable_get(:@io)
115-
refute_same io, new_io
101+
assert_same io, new_io
116102
refute new_io.closed?
117-
if defined?(JRUBY_VERSION)
118-
refute io.closed?
119-
else
120-
assert io.closed?
121-
end
103+
refute io.closed?
122104
end
123105
end
124106
end

0 commit comments

Comments
 (0)