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

Class Method Details

._view(msg, label) ⇒ String

Formats a message for logging with a label.

Examples:

formatted_message = Log._view("An error occurred", :error)

Parameters:

  • msg (String)

    the message to format

  • label (Symbol)

    the label for the message

Returns:

  • (String)

    the formatted log message



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("<", "&lt;").gsub(">", "&gt;") if safe.include?("<") and safe.include?(">")
  "[#{label}] #{safe}"
end

._write(line) ⇒ nil

Writes a line to the appropriate output based on the current environment.

Examples:

Log._write("This is a log line")

Parameters:

  • line (String)

    the line to write

Returns:

  • (nil)

    always returns nil



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.

Examples:

Log.dump("Dumping this message")

Parameters:

  • args (*Object)

    the messages to log

Returns:

  • (nil)

    always returns nil



167
168
169
# File 'lib/common/log.rb', line 167

def self.dump(*args)
  pp(*args)
end

.filterRegexp

Retrieves the current log filter.

Examples:

filter = Log.filter

Returns:

  • (Regexp)

    the current log filter

Raises:

  • (SQLite3::BusyException)

    if the database is busy



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

.offnil

Disables logging.

Examples:

Log.off

Returns:

  • (nil)

    always returns nil

Raises:

  • (SQLite3::BusyException)

    if the database is busy



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.

Examples:

Log.on(/error/)

Parameters:

  • filter (Regexp) (defaults to: //)

    the filter to apply to log messages (default: //)

Returns:

  • (nil)

    always returns nil

Raises:

  • (SQLite3::BusyException)

    if the database is busy



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.

Examples:

if Log.on?
  puts "Logging is enabled"
end

Returns:

  • (Boolean)

    true if logging is enabled, false otherwise

Raises:

  • (SQLite3::BusyException)

    if the database is busy



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.

Examples:

Log.out("This is a debug message")

Parameters:

  • msg (String, Exception)

    the message to log

  • label (Symbol) (defaults to: :debug)

    the label for the log message (default: :debug)

Returns:

  • (nil)

    always returns nil



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.message, 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.

Examples:

Log.pp("This is a pretty printed message")

Parameters:

  • msg (String)

    the message to log

  • label (Symbol) (defaults to: :debug)

    the label for the log message (default: :debug)

Returns:

  • (nil)

    always returns nil



156
157
158
# File 'lib/common/log.rb', line 156

def self.pp(msg, label = :debug)
  respond _view(msg, label)
end