Module: Lich::Common::Log
- Defined in:
- documented/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 with a label for logging.
-
._write(line) ⇒ void
Writes a line to the output based on the current environment settings.
-
.dump(*args) ⇒ void
Outputs a log message, alias for pp.
-
.filter ⇒ Regexp
Retrieves the current log filter.
-
.off ⇒ nil
Disables logging and resets the filter.
-
.on(filter = //) ⇒ nil
Enables logging with an optional filter.
-
.on? ⇒ Boolean
Checks if logging is enabled.
-
.out(msg, label: :debug) ⇒ void
Outputs a log message if logging is enabled and the message matches the filter.
-
.pp(msg, label = :debug) ⇒ void
Outputs a formatted log message using the respond method.
Class Method Details
._view(msg, label) ⇒ String
Formats a message with a label for logging.
110 111 112 113 114 115 |
# File 'documented/common/log.rb', line 110 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) ⇒ void
This method returns an undefined value.
Writes a line to the output based on the current environment settings.
96 97 98 99 100 101 102 103 104 |
# File 'documented/common/log.rb', line 96 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) ⇒ void
This method returns an undefined value.
Outputs a log message, alias for pp.
128 129 130 |
# File 'documented/common/log.rb', line 128 def self.dump(*args) pp(*args) end |
.filter ⇒ Regexp
Retrieves the current log filter.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'documented/common/log.rb', line 63 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 and resets the filter.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'documented/common/log.rb', line 32 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.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'documented/common/log.rb', line 17 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'documented/common/log.rb', line 47 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) ⇒ void
This method returns an undefined value.
Outputs a log message if logging is enabled and the message matches the filter.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'documented/common/log.rb', line 81 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) ⇒ void
This method returns an undefined value.
Outputs a formatted log message using the respond method.
121 122 123 |
# File 'documented/common/log.rb', line 121 def self.pp(msg, label = :debug) respond _view(msg, label) end |