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
-
#clear_messages ⇒ void
private
Clears the seen state of debug and warning messages.
-
#debug(msg) ⇒ void
Prints a debug message if debugging is turned on.
-
#debugging(bool) ⇒ void
private
Enable or disable logging of debug messages.
-
#debugging? ⇒ true, false
private
Is debugging enabled?.
-
#debugonce(msg) ⇒ void
Prints a debug message only once.
- #format_exception(exception, message, trace) ⇒ Object
- #log_exception(exception, message = :default) ⇒ Object
-
#on_message(&block) ⇒ Object
Used to register a callback that is called when a message is logged.
-
#show_time(string) ⇒ void
private
Print timing information.
-
#timing(bool) ⇒ void
private
Enable or disable logging of timing information.
-
#timing? ⇒ Boolean
private
Returns whether timing output is turned on.
- #trace(bool) ⇒ Object
- #trace? ⇒ Boolean
-
#warn(msg) ⇒ void
Prints a warning message.
-
#warnonce(msg) ⇒ void
Prints a warning message only once per process.
Instance Method Details
#clear_messages ⇒ 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.
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 @@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
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
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?
163 164 165 |
# File 'lib/facter/custom_facts/core/logging.rb', line 163 def debugging? @@debug end |
#debugonce(msg) ⇒ void
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.
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, , trace) arr = [] if == :default arr << exception. elsif arr << 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, = :default) self.warn(format_exception(exception, , @@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.
39 40 41 |
# File 'lib/facter/custom_facts/core/logging.rb', line 39 def (&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
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
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
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
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.
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
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.
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 |