Class: Lug::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/lug/logger.rb

Direct Known Subclasses

TtyDevice

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = STDERR) ⇒ Device

Create a Device associated to an io instance

Parameters:

  • io (IO) (defaults to: STDERR)

    (default: STDERR)



116
117
118
119
120
121
122
# File 'lib/lug/logger.rb', line 116

def initialize(io = STDERR)
  @io = io
  @io.sync = true

  @enabled_namespaces = []
  enable(ENV['DEBUG'.freeze].to_s) if ENV['DEBUG']
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io



110
111
112
# File 'lib/lug/logger.rb', line 110

def io
  @io
end

Instance Method Details

#enable(filter) ⇒ Array<Regexp>

Updates list of enabled namespaces for this device based on filter

Parameters:

  • filter (String)

Returns:

  • (Array<Regexp>)

    list of namespace filter regexps



167
168
169
# File 'lib/lug/logger.rb', line 167

def enable(filter)
  @enabled_namespaces = Helpers.parse_namespace_filter(filter)
end

#enabled_for?(namespace) ⇒ Boolean

Decides whether namespace is enabled on this device

Parameters:

  • namespace (String, Symbol)

Returns:

  • (Boolean)


157
158
159
160
# File 'lib/lug/logger.rb', line 157

def enabled_for?(namespace)
  ns = namespace.to_s
  @enabled_namespaces.any? { |re| ns =~ re }
end

#log(message, namespace = nil) ⇒ NilClass Also known as: <<

Log a message to output device, within a namespace

Parameters:

  • message (String)
  • namespace (String, Symbol) (defaults to: nil)

    (default: nil)

Returns:

  • (NilClass)


130
131
132
133
134
135
136
137
138
139
140
# File 'lib/lug/logger.rb', line 130

def log(message, namespace = nil)
  line = [
    Time.now,
    $$,
    namespace && "[#{namespace}]",
    message
  ].compact.join(' '.freeze)

  @io.write("#{line}\n")
  nil
end

#on(namespace) ⇒ Lug::Logger

Clone logger with the same device and namespace appended

Parameters:

  • namespace (String, Symbol)

Returns:



148
149
150
# File 'lib/lug/logger.rb', line 148

def on(namespace)
  Logger.new(self, namespace)
end