Device used for logging messages.

Namespace
Methods
A
C
E
N
O
P
S
W
Constants
SiD = 24 * 60 * 60
 
Attributes
[R] dev
[R] filename
Class Public methods
new(log = nil, opt = {})
# File ../ruby/lib/logger.rb, line 543
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
    @shift_age = opt[:shift_age] || 7
    @shift_size = opt[:shift_size] || 1048576
  end
end
Instance Public methods
close()
# File ../ruby/lib/logger.rb, line 578
def close
  begin
    @mutex.synchronize do
      @dev.close rescue nil
    end
  rescue Exception
    @dev.close rescue nil
  end
end
write(message)
# File ../ruby/lib/logger.rb, line 557
def write(message)
  begin
    @mutex.synchronize do
      if @shift_age and @dev.respond_to?(:stat)
        begin
          check_shift_log
        rescue
          warn("log shifting failed. #{$!}")
        end
      end
      begin
        @dev.write(message)
      rescue
        warn("log writing failed. #{$!}")
      end
    end
  rescue Exception => ignored
    warn("log writing failed. #{ignored}")
  end
end
Instance Private methods
add_log_header(file)
# File ../ruby/lib/logger.rb, line 605
def add_log_header(file)
  file.write(
    "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName]
  )
end
check_shift_log()
# File ../ruby/lib/logger.rb, line 613
def check_shift_log
  if @shift_age.is_a?(Integer)
    # Note: always returns false if '0'.
    if @filename && (@shift_age > 0) && (@dev.stat.size > @shift_size)
      shift_log_age
    end
  else
    now = Time.now
    period_end = previous_period_end(now)
    if @dev.stat.mtime <= period_end
      shift_log_period(period_end)
    end
  end
end
create_logfile(filename)
# File ../ruby/lib/logger.rb, line 598
def create_logfile(filename)
  logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT))
  logdev.sync = true
  add_log_header(logdev)
  logdev
end
eod(t)
# File ../ruby/lib/logger.rb, line 672
def eod(t)
  Time.mktime(t.year, t.month, t.mday, 23, 59, 59)
end
open_logfile(filename)
# File ../ruby/lib/logger.rb, line 590
def open_logfile(filename)
  if (FileTest.exist?(filename))
    open(filename, (File::WRONLY | File::APPEND))
  else
    create_logfile(filename)
  end
end
previous_period_end(now)
# File ../ruby/lib/logger.rb, line 659
def previous_period_end(now)
  case @shift_age
  when /^daily$/
    eod(now - 1 * SiD)
  when /^weekly$/
    eod(now - ((now.wday + 1) * SiD))
  when /^monthly$/
    eod(now - now.mday * SiD)
  else
    now
  end
end
shift_log_age()
# File ../ruby/lib/logger.rb, line 628
def shift_log_age
  (@shift_age-3).downto(0) do |i|
    if FileTest.exist?("#{@filename}.#{i}")
      File.rename("#{@filename}.#{i}", "#{@filename}.#{i+1}")
    end
  end
  @dev.close rescue nil
  File.rename("#{@filename}", "#{@filename}.0")
  @dev = create_logfile(@filename)
  return true
end
shift_log_period(period_end)
# File ../ruby/lib/logger.rb, line 640
def shift_log_period(period_end)
  postfix = period_end.strftime("%Y%m%d") # YYYYMMDD
  age_file = "#{@filename}.#{postfix}"
  if FileTest.exist?(age_file)
    # try to avoid filename crash caused by Timestamp change.
    idx = 0
    # .99 can be overridden; avoid too much file search with 'loop do'
    while idx < 100
      idx += 1
      age_file = "#{@filename}.#{postfix}.#{idx}"
      break unless FileTest.exist?(age_file)
    end
  end
  @dev.close rescue nil
  File.rename("#{@filename}", age_file)
  @dev = create_logfile(@filename)
  return true
end