module LegacyFacter::Core::Logging

Constants

GREEN

@api private

RED
RESET

@api private

Public Instance Methods

clear_messages() click to toggle source

Clears the seen state of debug and warning messages. See {debugonce} and {warnonce}.

@return [void]

@api private

# File lib/facter/custom_facts/core/logging.rb, line 196
def clear_messages
  @@debug_messages.clear
  @@warn_messages.clear
end
debug(msg) click to toggle source

Prints a debug message if debugging is turned on

@param msg [String] the debug message @return [void]

# File lib/facter/custom_facts/core/logging.rb, line 46
def debug(msg)
  return unless debugging?

  if msg.nil? || msg.empty?
    invoker = caller(1..1).first.slice(/.*:\d+/)
    self.warn "#{self.class}#debug invoked with invalid message #{msg.inspect}:#{msg.class} at #{invoker}"
  elsif @@message_callback
    @@message_callback.call(:debug, msg)
  else
    puts GREEN + msg + RESET
  end
end
debugging(bool) click to toggle source

Enable or disable logging of debug messages

@param bool [true, false] @return [void]

@api private

# File lib/facter/custom_facts/core/logging.rb, line 153
def debugging(bool)
  @@debug = bool
end
debugging?() click to toggle source

Is debugging enabled?

@return [true, false]

@api private

# File lib/facter/custom_facts/core/logging.rb, line 162
def debugging?
  @@debug
end
debugonce(msg) click to toggle source

Prints a debug message only once.

@note Uniqueness is based on the string, not the specific location

of the method call.

@param msg [String] the debug message @return [void]

# File lib/facter/custom_facts/core/logging.rb, line 66
def debugonce(msg)
  return unless msg && !msg.empty? && @@debug_messages[msg].nil?

  @@debug_messages[msg] = true
  debug(msg)
end
format_exception(exception, message, trace) click to toggle source
# File lib/facter/custom_facts/core/logging.rb, line 112
def format_exception(exception, message, trace)
  arr = []

  if message == :default
    arr << exception.message
  elsif message
    arr << message
  end

  if trace
    arr << 'backtrace:'
    arr.concat(exception.backtrace)
  end

  "#{RED}#{arr.flatten.join("\n")}#{RESET}"
end
log_exception(exception, message = :default) click to toggle source
# File lib/facter/custom_facts/core/logging.rb, line 108
def log_exception(exception, message = :default)
  self.warn(format_exception(exception, message, @@trace))
end
on_message(&block) click to toggle source

Used to register a callback that is called when a message is logged. If a block is given, Facter will not log messages. If a block is not given, Facter will resume logging messages. @param block [Proc] the callback to call when a message is logged.

The first argument to the callback will be a symbol representing a level. The supported
levels are: :trace, :debug, :info, :warn, :error, and :fatal.
The second argument to the callback will be a string containing the message
that was logged.

@api public

# File lib/facter/custom_facts/core/logging.rb, line 38
def on_message(&block)
  @@message_callback = block
end
show_time(string) click to toggle source

Print timing information

@param string [String] the time to print @return [void]

@api private

# File lib/facter/custom_facts/core/logging.rb, line 137
def show_time(string)
  return unless string && timing?

  if @@message_callback
    @@message_callback.call(:info, string)
  else
    $stderr.puts "#{GREEN}#{string}#{RESET}"
  end
end
timing(bool) click to toggle source

Enable or disable logging of timing information

@param bool [true, false] @return [void]

@api private

# File lib/facter/custom_facts/core/logging.rb, line 172
def timing(bool)
  @@timing = bool
end
timing?() click to toggle source

Returns whether timing output is turned on

@api private

# File lib/facter/custom_facts/core/logging.rb, line 179
def timing?
  @@timing
end
trace(bool) click to toggle source
# File lib/facter/custom_facts/core/logging.rb, line 183
def trace(bool)
  @@trace = bool
end
trace?() click to toggle source
# File lib/facter/custom_facts/core/logging.rb, line 187
def trace?
  @@trace
end
warn(msg) click to toggle source

Prints a warning message. The message is only printed if debugging is enabled.

@param msg [String] the warning message to be printed

@return [void]

# File lib/facter/custom_facts/core/logging.rb, line 79
def warn(msg)
  if msg.nil? || msg.empty?
    invoker = caller(1..1).first.slice(/.*:\d+/)
    msg = "#{self.class}#debug invoked with invalid message #{msg.inspect}:#{msg.class} at #{invoker}"
  end
  if @@message_callback
    @@message_callback.call(:warn, msg)
  else
    Kernel.warn msg
  end
end
warnonce(msg) click to toggle source

Prints a warning message only once per process. Each unique string is printed once.

@note Unlike {warn} the message will be printed even if debugging is

not turned on. This behavior is likely to change and should not be
relied on.

@param msg [String] the warning message to be printed

@return [void]

# File lib/facter/custom_facts/core/logging.rb, line 101
def warnonce(msg)
  return unless @@warn_messages[msg].nil?

  self.warn(msg)
  @@warn_messages[msg] = true
end