class RouteCounter::FileRecorder::LogDevice

Attributes

dev[R]
filename[R]

Public Class Methods

new(log = nil, opt = {}) click to toggle source
# File lib/route_counter/file_recorder.rb, line 72
def initialize(log = nil, opt = {})
  @dev = @filename = 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
  end
end

Public Instance Methods

close() click to toggle source
# File lib/route_counter/file_recorder.rb, line 90
def close
  begin
    @mutex.synchronize do
      @dev.close rescue nil
    end
  rescue Exception
    @dev.close rescue nil
  end
end
write(message) click to toggle source
# File lib/route_counter/file_recorder.rb, line 84
def write(message)
  @mutex.synchronize do
    @dev.write(message)
  end
end

Private Instance Methods

create_logfile(filename) click to toggle source
# File lib/route_counter/file_recorder.rb, line 110
def create_logfile(filename)
  begin
    FileUtils.mkdir_p(File.dirname(filename))

    logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT | File::EXCL))
    logdev.flock(File::LOCK_EX)
    logdev.sync = true
    logdev.flock(File::LOCK_UN)
  rescue Errno::EEXIST
    # file is created by another process
    logdev = open_logfile(filename)
    logdev.sync = true
  end
  logdev
end
open_logfile(filename) click to toggle source
# File lib/route_counter/file_recorder.rb, line 102
def open_logfile(filename)
  begin
    open(filename, (File::WRONLY | File::APPEND))
  rescue Errno::ENOENT
    create_logfile(filename)
  end
end