class Output::Writer

Attributes

devices[W]
enabled[R]
level[RW]
message_transformer[R]
name[R]

Public Class Methods

build(writer_name, level=Output::DEFAULT_LOGGER_LEVEL, message_transformer=nil, logger_level=Output::DEFAULT_LOGGER_LEVEL, logger_name=nil, device_options) click to toggle source
# File lib/output/writer.rb, line 14
def self.build(writer_name, level=Output::DEFAULT_LOGGER_LEVEL, message_transformer=nil, logger_level=Output::DEFAULT_LOGGER_LEVEL, logger_name=nil, device_options)
  logger_name ||= writer_name
  logger = build_logger(logger_name, logger_level, device_options)
  writer = new(writer_name, level, message_transformer, logger, device_options)
end

Public Instance Methods

add_device(device) click to toggle source
# File lib/output/writer.rb, line 62
def add_device(device)
  @logger.add_appenders device
  device
end
device(name) click to toggle source
# File lib/output/writer.rb, line 67
def device(name)
  result = nil
  logger.appenders.each do|device|
    result = device if device.name == name.to_s
  end
  result
end
device?(device) click to toggle source
# File lib/output/writer.rb, line 176
def device?(device)
  return false if device.nil?
  
  devices.include?(device) || devices.any? { |dvc| dvc.name == device.name }
end
devices() click to toggle source
# File lib/output/writer.rb, line 20
def devices
  @devices ||= []
end
disable() click to toggle source
# File lib/output/writer.rb, line 24
def disable
  @enabled = false
end
enable() click to toggle source
# File lib/output/writer.rb, line 28
def enable
  @enabled = true
end
enabled?() click to toggle source
# File lib/output/writer.rb, line 32
def enabled?
  @enabled = true if @enabled.nil?
  @enabled
end
initial_logger_level?() click to toggle source
# File lib/output/writer.rb, line 49
def initial_logger_level?
  logger_level == level
end
logger_device?(device) click to toggle source
# File lib/output/writer.rb, line 79
def logger_device?(device)
  logger.appenders.include? device
end
logger_level() click to toggle source
# File lib/output/writer.rb, line 37
def logger_level
  @logger.level_name
end
logger_level=(level) click to toggle source
# File lib/output/writer.rb, line 41
def logger_level=(level)
  @logger.level = level
end
logger_name() click to toggle source
# File lib/output/writer.rb, line 53
def logger_name
  @logger.name
end
logging_appender?(arg) click to toggle source
# File lib/output/writer.rb, line 88
def logging_appender?(arg)
  arg.is_a? Logging::Appender
end
number_of_stack_devices() click to toggle source
# File lib/output/writer.rb, line 75
def number_of_stack_devices
  devices.count
end
pop_device() click to toggle source
# File lib/output/writer.rb, line 170
def pop_device
  return if devices.count == 0
  device = devices.pop
  remove_device device
end
push_device(device, options = {}, &block) click to toggle source
# File lib/output/writer.rb, line 139
def push_device(device, options = {},  &block)
  return device if device.nil?
  return push_device__obj(device, &block) if device.is_a? Logging::Appender

  push_device__opts(type = device, options, &block)
end
push_device__obj(device) { || ... } click to toggle source
# File lib/output/writer.rb, line 146
def push_device__obj(device, &block)
  raise "The writer:[#{self.name}] already has a device named #{device.name}:[#{device.class}]" if device?(device)

  devices.push device

  add_device device

  if block_given?
    yield
    pop_device
  end
  device
end
push_device__opts(type, options = {}, &block) click to toggle source
# File lib/output/writer.rb, line 160
def push_device__opts(type, options = {}, &block)
  options = self.device_options.merge(options)
  name = options[:name] || type

  raise "Writer:[#{self.name}] - already has a device named [#{name}]. It cannot be pushed the device again" unless device(name).nil?

  device = Output::Devices.build_device(type, options)
  push_device__obj device, &block
end
remove_device(device) click to toggle source
# File lib/output/writer.rb, line 83
def remove_device(device)
  @logger.remove_appenders device
  device
end
reset_level() click to toggle source
# File lib/output/writer.rb, line 45
def reset_level
  self.logger_level = level
end
suspend_device(device, &block) click to toggle source
# File lib/output/writer.rb, line 92
def suspend_device(device, &block)
  return suspend_device__obj device, &block if logging_appender?(device)

  suspend_device__name device, &block
end
suspend_device__name(name, &block) click to toggle source
# File lib/output/writer.rb, line 98
def suspend_device__name(name, &block)
  dvc = device name
  suspend_device__obj dvc, &block
end
suspend_device__obj(device) { || ... } click to toggle source
# File lib/output/writer.rb, line 128
def suspend_device__obj(device, &block)
  suspension = DeviceSuspension.new self, device
  suspension.suspend

  if block_given?
    yield
    suspension.restore
  end
  device
end
to_s() click to toggle source
# File lib/output/writer.rb, line 182
def to_s
  details = "Writer : #{self.name}\n"
  details << "\tLevel : #{self.level}\n"
  details << "\tDevice Stack : \n"
  devices.each do |device|
    details << "\t\tDevice : #{device.name} - #{device.class}\n"
  end
  details << "\tLogger Appenders : \n"
  logger.appenders.each do |appender|
    details << "\t\t\Appender : #{appender.name} - #{appender.class}\n"
  end
  details
end
write(message) click to toggle source
# File lib/output/writer.rb, line 57
def write(message)
  message = message_transformer.call message if message_transformer
  @logger.send level, message if self.enabled?
end