Module: LegacyFacter::Core::Logging

Extended by:
Logging
Included in:
LegacyFacter, Logging
Defined in:
lib/facter/custom_facts/core/logging.rb

Constant Summary collapse

GREEN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"\e[0;32m"
RESET =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"\e[0m"
RED =
"\e[31m"
@@debug =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

false
@@timing =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

false
@@trace =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

false
@@warn_messages =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

{}
@@debug_messages =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

{}
@@message_callback =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

nil

Instance Method Summary collapse

Instance Method Details

#clear_messagesvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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



197
198
199
200
# File 'lib/facter/custom_facts/core/logging.rb', line 197

def clear_messages
  @@debug_messages.clear
  @@warn_messages.clear
end

#debug(msg) ⇒ void

This method returns an undefined value.

Prints a debug message if debugging is turned on

Parameters:

  • msg (String)

    the debug message



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/facter/custom_facts/core/logging.rb', line 47

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Enable or disable logging of debug messages

Parameters:

  • bool (true, false)


154
155
156
# File 'lib/facter/custom_facts/core/logging.rb', line 154

def debugging(bool)
  @@debug = bool
end

#debugging?true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is debugging enabled?

Returns:

  • (true, false)


163
164
165
# File 'lib/facter/custom_facts/core/logging.rb', line 163

def debugging?
  @@debug
end

#debugonce(msg) ⇒ void

Note:

Uniqueness is based on the string, not the specific location of the method call.

This method returns an undefined value.

Prints a debug message only once.

Parameters:

  • msg (String)

    the debug message



67
68
69
70
71
72
# File 'lib/facter/custom_facts/core/logging.rb', line 67

def debugonce(msg)
  return unless msg && !msg.empty? && @@debug_messages[msg].nil?

  @@debug_messages[msg] = true
  debug(msg)
end

#format_exception(exception, message, trace) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/facter/custom_facts/core/logging.rb', line 113

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



109
110
111
# File 'lib/facter/custom_facts/core/logging.rb', line 109

def log_exception(exception, message = :default)
  self.warn(format_exception(exception, message, @@trace))
end

#on_message(&block) ⇒ Object

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.

Parameters:

  • 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.



39
40
41
# File 'lib/facter/custom_facts/core/logging.rb', line 39

def on_message(&block)
  @@message_callback = block
end

#show_time(string) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Print timing information

Parameters:

  • string (String)

    the time to print



138
139
140
141
142
143
144
145
146
# File 'lib/facter/custom_facts/core/logging.rb', line 138

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Enable or disable logging of timing information

Parameters:

  • bool (true, false)


173
174
175
# File 'lib/facter/custom_facts/core/logging.rb', line 173

def timing(bool)
  @@timing = bool
end

#timing?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether timing output is turned on

Returns:

  • (Boolean)


180
181
182
# File 'lib/facter/custom_facts/core/logging.rb', line 180

def timing?
  @@timing
end

#trace(bool) ⇒ Object



184
185
186
# File 'lib/facter/custom_facts/core/logging.rb', line 184

def trace(bool)
  @@trace = bool
end

#trace?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/facter/custom_facts/core/logging.rb', line 188

def trace?
  @@trace
end

#warn(msg) ⇒ void

This method returns an undefined value.

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

Parameters:

  • msg (String)

    the warning message to be printed



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/facter/custom_facts/core/logging.rb', line 80

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

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.

This method returns an undefined value.

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

Parameters:

  • msg (String)

    the warning message to be printed



102
103
104
105
106
107
# File 'lib/facter/custom_facts/core/logging.rb', line 102

def warnonce(msg)
  return unless @@warn_messages[msg].nil?

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