Module: Facter::Core::Logging

Extended by:
Logging
Included in:
Facter, Logging
Defined in:
lib/facter/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.

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

""
@@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.



193
194
195
196
# File 'lib/facter/core/logging.rb', line 193

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



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/facter/core/logging.rb', line 44

def debug(msg)
  if self.debugging?
    if msg.nil? or msg.empty?
      invoker = caller[0].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
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



150
151
152
# File 'lib/facter/core/logging.rb', line 150

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?



159
160
161
# File 'lib/facter/core/logging.rb', line 159

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.



64
65
66
67
68
69
# File 'lib/facter/core/logging.rb', line 64

def debugonce(msg)
  if msg and not msg.empty? and @@debug_messages[msg].nil?
    @@debug_messages[msg] = true
    debug(msg)
  end
end

#format_exception(exception, message, trace) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/facter/core/logging.rb', line 110

def format_exception(exception, message, trace)
  arr = []

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

  if trace
    arr.concat(exception.backtrace)
  end

  arr.flatten.join("\n")
end

#log_exception(exception, message = :default) ⇒ Object



106
107
108
# File 'lib/facter/core/logging.rb', line 106

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.



36
37
38
# File 'lib/facter/core/logging.rb', line 36

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



134
135
136
137
138
139
140
141
142
# File 'lib/facter/core/logging.rb', line 134

def show_time(string)
  return unless string && self.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



169
170
171
# File 'lib/facter/core/logging.rb', line 169

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



176
177
178
# File 'lib/facter/core/logging.rb', line 176

def timing?
  @@timing
end

#trace(bool) ⇒ Object



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

def trace(bool)
  @@trace = bool
end

#trace?Boolean



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

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.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/facter/core/logging.rb', line 77

def warn(msg)
  if msg.nil? or msg.empty?
    invoker = caller[0].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.



99
100
101
102
103
104
# File 'lib/facter/core/logging.rb', line 99

def warnonce(msg)
  if @@warn_messages[msg].nil?
    self.warn(msg)
    @@warn_messages[msg] = true
  end
end