diff options
author | sonots <sonots@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-11-17 13:58:57 +0000 |
---|---|---|
committer | sonots <sonots@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-11-17 13:58:57 +0000 |
commit | f952570bddc07e87b7c5a3f62dd5fbe390b7fe48 (patch) | |
tree | d1a0325bbead7687c7955aca6173ffb29d59a247 /lib | |
parent | 6a533a3ecc23e6cb90e2616231f2b67d15ff3239 (diff) | |
download | ruby-f952570bddc07e87b7c5a3f62dd5fbe390b7fe48.tar.gz |
lib/logger.rb: Add Logger#reopen
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52620 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/logger.rb | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/lib/logger.rb b/lib/logger.rb index f15a85077e..475ec2a9bf 100644 --- a/lib/logger.rb +++ b/lib/logger.rb @@ -322,6 +322,26 @@ class Logger # # :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 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)) |