class Mamiya::Logger::LogDev

Public Class Methods

new(outputs) click to toggle source
# File lib/mamiya/logger.rb, line 135
def initialize(outputs)
  @outputs = normalize_outputs(outputs)
  @mutex = Mutex.new
end

Public Instance Methods

add(*outputs) click to toggle source
# File lib/mamiya/logger.rb, line 167
def add(*outputs)
  @mutex.synchronize do
    @outputs.push(*normalize_outputs(outputs))
  end
  self
end
close() click to toggle source
# File lib/mamiya/logger.rb, line 151
def close
  @outputs.each do |output|
    output.close unless output.respond_to?(:closed?) && output.closed?
  end
  self
end
remove(*removing_outputs) click to toggle source
# File lib/mamiya/logger.rb, line 174
def remove(*removing_outputs)
  @mutex.synchronize do
    removing_outputs.each do |removing|
      case removing
      when File
        @outputs.reject! { |out| out.kind_of?(File) && out.path == removing.path }
      when IO
        @outputs.reject! { |out| out.kind_of?(IO) && out.fileno == removing.fileno }
      when String
        @outputs.reject! { |out| out.kind_of?(File) && out.path == removing }
      when Integer
        @outputs.reject! { |out| out.kind_of?(IO) && out.fileno == removing }
      else
        @outputs.reject! { |out| out == removing }
      end
    end
  end
  self
end
reopen() click to toggle source
# File lib/mamiya/logger.rb, line 158
def reopen
  @outputs.select { |io| io.respond_to?(:path) }.each do |io|
    sync = io.sync
    io.reopen(io.path, 'a')
    io.sync = sync
    io.puts("Log reopened")
  end
end
tty?() click to toggle source
# File lib/mamiya/logger.rb, line 140
def tty?
  @outputs.all?(&:tty?)
end
write(*args) click to toggle source
# File lib/mamiya/logger.rb, line 144
def write(*args)
  @outputs.each do |output|
    output.write(*args) unless output.respond_to?(:closed?) && output.closed?
  end
  self
end

Private Instance Methods

normalize_outputs(ary) click to toggle source
# File lib/mamiya/logger.rb, line 196
def normalize_outputs(ary)
  ary.map do |out|
    case
    when out.respond_to?(:write)
      out
    when out.kind_of?(String)
      File.open(out, 'a').tap{ |_| _.sync = true }
    else
      raise ArgumentError, 'output should able to respond to :write or be String'
    end
  end
end