Class: Output::Writer

Inherits:
Object
  • Object
show all
Includes:
Initializer, BuildLogger
Defined in:
lib/output/writer.rb,
lib/output/build_logger.rb

Defined Under Namespace

Modules: BuildLogger, Naming Classes: Attribute, DeviceSuspension

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BuildLogger

included

Instance Attribute Details

#devicesObject



20
21
22
# File 'lib/output/writer.rb', line 20

def devices
  @devices ||= []
end

#enabledObject (readonly)

Returns the value of attribute enabled.



9
10
11
# File 'lib/output/writer.rb', line 9

def enabled
  @enabled
end

#levelObject

Returns the value of attribute level.



7
8
9
# File 'lib/output/writer.rb', line 7

def level
  @level
end

#message_transformerObject (readonly)

Returns the value of attribute message_transformer.



8
9
10
# File 'lib/output/writer.rb', line 8

def message_transformer
  @message_transformer
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/output/writer.rb', line 6

def name
  @name
end

Class Method Details

.build(writer_name, level = Output::DEFAULT_LOGGER_LEVEL, message_transformer = nil, logger_level = Output::DEFAULT_LOGGER_LEVEL, logger_name = nil, device_options) ⇒ Object



14
15
16
17
18
# 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

Instance Method Details

#add_device(device) ⇒ Object



62
63
64
65
# File 'lib/output/writer.rb', line 62

def add_device(device)
  @logger.add_appenders device
  device
end

#device(name) ⇒ Object



67
68
69
70
71
72
73
# 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) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
181
182
# File 'lib/output/writer.rb', line 178

def device?(device)
  return false if device.nil?
  
  devices.include?(device) || devices.any? { |dvc| dvc.name == device.name }
end

#disableObject



24
25
26
# File 'lib/output/writer.rb', line 24

def disable
  @enabled = false
end

#enableObject



28
29
30
# File 'lib/output/writer.rb', line 28

def enable
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/output/writer.rb', line 32

def enabled?
  @enabled = true if @enabled.nil?
  @enabled
end

#initial_logger_level?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/output/writer.rb', line 49

def initial_logger_level?
  logger_level == level
end

#logger_device?(device) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/output/writer.rb', line 79

def logger_device?(device)
  logger.appenders.include? device
end

#logger_levelObject



37
38
39
# File 'lib/output/writer.rb', line 37

def logger_level
  @logger.level_name
end

#logger_level=(level) ⇒ Object



41
42
43
# File 'lib/output/writer.rb', line 41

def logger_level=(level)
  @logger.level = level
end

#logger_nameObject



53
54
55
# File 'lib/output/writer.rb', line 53

def logger_name
  @logger.name
end

#logging_appender?(arg) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/output/writer.rb', line 90

def logging_appender?(arg)
  arg.is_a? Logging::Appender
end

#number_of_stack_devicesObject



75
76
77
# File 'lib/output/writer.rb', line 75

def number_of_stack_devices
  devices.count
end

#pop_deviceObject



172
173
174
175
176
# File 'lib/output/writer.rb', line 172

def pop_device
  return if devices.count == 0
  device = devices.pop
  remove_device device
end

#push_device(device, options = {}, &block) ⇒ Object



155
156
157
158
159
160
# File 'lib/output/writer.rb', line 155

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, &block) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/output/writer.rb', line 141

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) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/output/writer.rb', line 162

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) ⇒ Object



85
86
87
88
# File 'lib/output/writer.rb', line 85

def remove_device(device)
  @logger.remove_appenders device
  device
end

#reset_levelObject



45
46
47
# File 'lib/output/writer.rb', line 45

def reset_level
  self.logger_level = level
end

#suspend_device(device, &block) ⇒ Object



94
95
96
97
98
# File 'lib/output/writer.rb', line 94

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) ⇒ Object



100
101
102
103
# File 'lib/output/writer.rb', line 100

def suspend_device__name(name, &block)
  dvc = device name
  suspend_device__obj dvc, &block
end

#suspend_device__obj(device, &block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/output/writer.rb', line 130

def suspend_device__obj(device, &block)
  suspension = DeviceSuspension.new self, device
  suspension.suspend

  if block_given?
    yield
    suspension.restore
  end
  device
end

#to_sObject



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/output/writer.rb', line 184

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) ⇒ Object



57
58
59
60
# 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