ruby-changes:40541
From: sonots <ko1@a...>
Date: Tue, 17 Nov 2015 22:59:16 +0900 (JST)
Subject: [ruby-changes:40541] sonots:r52620 (trunk): lib/logger.rb: Add Logger#reopen
sonots 2015-11-17 22:58:57 +0900 (Tue, 17 Nov 2015) New Revision: 52620 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52620 Log: lib/logger.rb: Add Logger#reopen Modified files: trunk/ChangeLog trunk/NEWS trunk/lib/logger.rb trunk/test/logger/test_logdevice.rb trunk/test/logger/test_logger.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 52619) +++ ChangeLog (revision 52620) @@ -121,6 +121,10 @@ Mon Nov 16 21:27:54 2015 Naohisa Goto https://github.com/ruby/ruby/blob/trunk/ChangeLog#L121 * 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@g...> + + * lib/logger.rb: Add Logger#reopen + Mon Nov 16 18:21:52 2015 Nobuyoshi Nakada <nobu@r...> * object.c (rb_obj_dig): dig in nested structs too. Index: lib/logger.rb =================================================================== --- lib/logger.rb (revision 52619) +++ lib/logger.rb (revision 52620) @@ -322,6 +322,26 @@ class Logger https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L322 # # :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) { ... } # # === Args @@ -580,12 +600,8 @@ private https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L600 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 https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L639 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)) Index: NEWS =================================================================== --- NEWS (revision 52619) +++ NEWS (revision 52620) @@ -77,6 +77,9 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L77 * 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] Index: test/logger/test_logger.rb =================================================================== --- test/logger/test_logger.rb (revision 52619) +++ test/logger/test_logger.rb (revision 52620) @@ -121,6 +121,12 @@ class TestLogger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/logger/test_logger.rb#L121 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" Index: test/logger/test_logdevice.rb =================================================================== --- test/logger/test_logdevice.rb (revision 52619) +++ test/logger/test_logdevice.rb (revision 52620) @@ -99,6 +99,72 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L99 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/