Module: Lumberjack::DeviceRegistry

Defined in:
lib/lumberjack/device_registry.rb

Overview

The device registry is used for setting up names to represent Device classes. It is used in the constructor for Lumberjack::Logger and allows passing in a symbol to reference a device.

Devices must have a constructor that accepts the options hash as its sole argument in order to use the device registry.

The values :stdout and :stderr are registered by default and map to the standard output and standard error streams, respectively.

Examples:


Lumberjack::Device.register(:my_device, MyDevice)
logger = Lumberjack::Logger.new(:my_device)

Class Method Summary collapse

Class Method Details

.add(name, klass) ⇒ void

This method returns an undefined value.

Register a device name. Device names can be used to associate a symbol with a device class. The symbol can then be passed to Logger as the device argument.

Registered devices must take only one argument and that is the options hash for the device options.

Parameters:

  • name (Symbol)

    The name of the device

  • klass (Class)

    The device class to register

Raises:

  • (ArgumentError)


32
33
34
35
36
# File 'lib/lumberjack/device_registry.rb', line 32

def add(name, klass)
  raise ArgumentError.new("name must be a symbol") unless name.is_a?(Symbol)

  @lock.synchronize { @registry[name] = klass }
end

.device_class(name) ⇒ Class?

Retrieve the class registered with the given name or nil if the name is not defined.

Parameters:

  • name (Symbol)

    The name of the device

Returns:

  • (Class, nil)

    The registered device class or nil if not found



79
80
81
# File 'lib/lumberjack/device_registry.rb', line 79

def device_class(name)
  @lock.synchronize { @registry[name] }
end

.new_device(name, options) ⇒ Lumberjack::Device

Instantiate a new device with the specified options from the device registry.

Parameters:

  • name (Symbol)

    The name of the device

  • options (Hash)

    The device options

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lumberjack/device_registry.rb', line 59

def new_device(name, options)
  klass = device_class(name)
  unless klass
    valid_names = registered_devices.keys.map(&:inspect).join(", ")
    raise ArgumentError.new("#{name.inspect} is not registered as a device name; valid names are: #{valid_names}")
  end

  if klass == :stdout
    Device::Writer.new($stdout, options)
  elsif klass == :stderr
    Device::Writer.new($stderr, options)
  else
    klass.new(options)
  end
end

.registered?(name) ⇒ Boolean

Check if a device is registered.

Parameters:

  • name (Symbol)

    The name of the device

Returns:

  • (Boolean)

    True if the device is registered, false otherwise



50
51
52
# File 'lib/lumberjack/device_registry.rb', line 50

def registered?(name)
  @lock.synchronize { @registry.include?(name) }
end

.registered_devicesHash

Return the map of registered device class names.

Returns:

  • (Hash)


86
87
88
# File 'lib/lumberjack/device_registry.rb', line 86

def registered_devices
  @lock.synchronize { @registry.dup }
end

.remove(name) ⇒ void

This method returns an undefined value.

Remove a device from the registry.

Parameters:

  • name (Symbol)

    The name of the device to remove



42
43
44
# File 'lib/lumberjack/device_registry.rb', line 42

def remove(name)
  @lock.synchronize { @registry.delete(name) }
end