1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

lib/logger.rb: Add Logger#reopen

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
sonots 2015-11-17 13:58:57 +00:00
parent 6a533a3ecc
commit f952570bdd
5 changed files with 126 additions and 6 deletions

View file

@ -121,6 +121,10 @@ Mon Nov 16 21:27:54 2015 Naohisa Goto <ngotogenome@gmail.com>
* test/dtrace/helper.rb (Dtrace::TestCase#trap_probe): dtrace buffer
size is set as 8m on Solaris (default 4m). [Bug #11697]
Mon Nov 16 20:03:14 2015 Naotoshi Seo <sonots@gmail.com>
* lib/logger.rb: Add Logger#reopen
Mon Nov 16 18:21:52 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* object.c (rb_obj_dig): dig in nested structs too.

3
NEWS
View file

@ -77,6 +77,9 @@ with all sufficient information, see the ChangeLog file.
* Kernel#loop, when stopped by a StopIteration exception, returns
what the enumerator has returned instead of nil. [Feature #11498]
* Logger
* Logger#reopen is added to reopen a log device. [Feature #11696]
* Module
* Module#deprecate_constant [Feature #11398]

View file

@ -320,6 +320,26 @@ class Logger
end
end
#
# :call-seq:
# Logger#reopen
# Logger#reopen(logdev)
#
# === Args
#
# +logdev+::
# The log device. This is a filename (String) or IO object (typically
# +STDOUT+, +STDERR+, or an open file).
#
# === Description
#
# Reopen a log device.
#
def reopen(logdev = nil)
@logdev.reopen(logdev)
self
end
#
# :call-seq:
# Logger#add(severity, message = nil, progname = nil) { ... }
@ -580,12 +600,8 @@ private
def initialize(log = nil, opt = {})
@dev = @filename = @shift_age = @shift_size = nil
@mutex = LogDeviceMutex.new
if log.respond_to?(:write) and log.respond_to?(:close)
@dev = log
else
@dev = open_logfile(log)
@dev.sync = true
@filename = log
set_dev(log)
if @filename
@shift_age = opt[:shift_age] || 7
@shift_size = opt[:shift_size] || 1048576
@next_rotate_time = next_rotate_time(Time.now, @shift_age) unless @shift_age.is_a?(Integer)
@ -623,8 +639,33 @@ private
end
end
def reopen(log = nil)
# reopen the same filename if no argument, do nothing for IO
log ||= @filename if @filename
if log
@mutex.synchronize do
if @filename and @dev
@dev.close rescue nil # close only file opened by Logger
@filename = nil
end
set_dev(log)
end
end
self
end
private
def set_dev(log)
if log.respond_to?(:write) and log.respond_to?(:close)
@dev = log
else
@dev = open_logfile(log)
@dev.sync = true
@filename = log
end
end
def open_logfile(filename)
begin
open(filename, (File::WRONLY | File::APPEND))

View file

@ -99,6 +99,72 @@ class TestLogDevice < Test::Unit::TestCase
r.close
end
def test_reopen_io
logdev = d(STDERR)
old_dev = logdev.dev
logdev.reopen
assert_equal(STDERR, logdev.dev)
assert(!old_dev.closed?)
end
def test_reopen_io_by_io
logdev = d(STDERR)
old_dev = logdev.dev
logdev.reopen(STDOUT)
assert_equal(STDOUT, logdev.dev)
assert(!old_dev.closed?)
end
def test_reopen_io_by_file
logdev = d(STDERR)
old_dev = logdev.dev
logdev.reopen(@filename)
begin
assert(File.exist?(@filename))
assert_equal(@filename, logdev.filename)
assert(!old_dev.closed?)
ensure
logdev.close
end
end
def test_reopen_file
logdev = d(@filename)
old_dev = logdev.dev
File.unlink(@filename) if File.exist?(@filename) # remove once, then reopen
logdev.reopen
begin
assert(File.exist?(@filename))
assert_equal(@filename, logdev.filename)
assert(old_dev.closed?)
ensure
logdev.close
end
end
def test_reopen_file_by_io
logdev = d(@filename)
old_dev = logdev.dev
logdev.reopen(STDOUT)
assert_equal(STDOUT, logdev.dev)
assert_nil(logdev.filename)
assert(old_dev.closed?)
end
def test_reopen_file_by_file
logdev = d(@filename)
old_dev = logdev.dev
File.unlink(@filename) if File.exist?(@filename) # remove once, then reopen
logdev.reopen(@filename)
begin
assert(File.exist?(@filename))
assert_equal(@filename, logdev.filename)
assert(old_dev.closed?)
ensure
logdev.close
end
end
def test_shifting_size
tmpfile = Tempfile.new([File.basename(__FILE__, '.*'), '_1.log'])
logfile = tmpfile.path

View file

@ -121,6 +121,12 @@ class TestLogger < Test::Unit::TestCase
assert_nil(logger.datetime_format)
end
def test_reopen
logger = Logger.new(STDERR)
logger.reopen(STDOUT)
assert_equal(STDOUT, logger.instance_variable_get(:@logdev).dev)
end
def test_add
logger = Logger.new(nil)
logger.progname = "my_progname"