Module: Lich::Common::Log
- Defined in:
- lib/common/log.rb
Defined Under Namespace
Modules: Preset
Constant Summary collapse
- @@log_enabled =
nil
- @@log_filter =
nil
Class Method Summary collapse
-
._view(msg, label) ⇒ String
Formats a message for logging with a label.
-
._write(line) ⇒ nil
Writes a line to the appropriate output based on the current environment.
-
.dump(*args) ⇒ nil
Dumps a message to the log.
-
.filter ⇒ Regexp
Retrieves the current log filter.
-
.off ⇒ nil
Disables logging.
-
.on(filter = //) ⇒ nil
Enables logging with an optional filter.
-
.on? ⇒ Boolean
Checks if logging is enabled.
-
.out(msg, label: :debug) ⇒ nil
Outputs a log message if logging is enabled and the message matches the filter.
-
.pp(msg, label = :debug) ⇒ nil
Responds with a formatted log message.
Class Method Details
._view(msg, label) ⇒ String
Formats a message for logging with a label.
141 142 143 144 145 146 |
# File 'lib/common/log.rb', line 141 def self._view(msg, label) label = [Script.current.name, label].flatten.compact.join(".") safe = msg.inspect # safe = safe.gsub("<", "<").gsub(">", ">") if safe.include?("<") and safe.include?(">") "[#{label}] #{safe}" end |
._write(line) ⇒ nil
Writes a line to the appropriate output based on the current environment.
123 124 125 126 127 128 129 130 131 |
# File 'lib/common/log.rb', line 123 def self._write(line) if Script.current.vars.include?("--headless") or not defined?(:_respond) $stdout.write(line + "\n") elsif line.include?("<") and line.include?(">") respond(line) else _respond Preset.as(:debug, line) end end |
.dump(*args) ⇒ nil
Dumps a message to the log.
167 168 169 |
# File 'lib/common/log.rb', line 167 def self.dump(*args) pp(*args) end |
.filter ⇒ Regexp
Retrieves the current log filter.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/common/log.rb', line 82 def self.filter if @@log_filter.nil? begin val = Lich.db.get_first_value("SELECT value FROM lich_settings WHERE name='log_filter';") rescue SQLite3::BusyException sleep 0.1 retry end val = // if val.nil? @@log_filter = Regexp.new(val) end return @@log_filter end |
.off ⇒ nil
Disables logging.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/common/log.rb', line 39 def self.off @@log_enabled = false @@log_filter = // begin Lich.db.execute("INSERT OR REPLACE INTO lich_settings(name,value) values('log_enabled',?);", [@@log_enabled.to_s.encode('UTF-8')]) Lich.db.execute("INSERT OR REPLACE INTO lich_settings(name,value) values('log_filter',?);", [@@log_filter.to_s.encode('UTF-8')]) rescue SQLite3::BusyException sleep 0.1 retry end return nil end |
.on(filter = //) ⇒ nil
Enables logging with an optional filter.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/common/log.rb', line 19 def self.on(filter = //) @@log_enabled = true @@log_filter = filter begin Lich.db.execute("INSERT OR REPLACE INTO lich_settings(name,value) values('log_enabled',?);", [@@log_enabled.to_s.encode('UTF-8')]) Lich.db.execute("INSERT OR REPLACE INTO lich_settings(name,value) values('log_filter',?);", [@@log_filter.to_s.encode('UTF-8')]) rescue SQLite3::BusyException sleep 0.1 retry end return nil end |
.on? ⇒ Boolean
Checks if logging is enabled.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/common/log.rb', line 61 def self.on? if @@log_enabled.nil? begin val = Lich.db.get_first_value("SELECT value FROM lich_settings WHERE name='log_enabled';") rescue SQLite3::BusyException sleep 0.1 retry end val = false if val.nil? @@log_enabled = (val.to_s =~ /on|true|yes/ ? true : false) if !val.nil? end return @@log_enabled end |
.out(msg, label: :debug) ⇒ nil
Outputs a log message if logging is enabled and the message matches the filter.
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/common/log.rb', line 104 def self.out(msg, label: :debug) return unless Script.current.vars.include?("--debug") || Log.on? return if msg.to_s !~ Log.filter if msg.is_a?(Exception) ## pretty-print exception _write _view(msg., label) msg.backtrace.to_a.slice(0..5).each do |frame| _write _view(frame, label) end else self._write _view(msg, label) # if Script.current.vars.include?("--debug") end end |
.pp(msg, label = :debug) ⇒ nil
Responds with a formatted log message.
156 157 158 |
# File 'lib/common/log.rb', line 156 def self.pp(msg, label = :debug) respond _view(msg, label) end |