Module: Lich::Common::Log

Defined in:
documented/common/log.rb

Overview

Provides contextual logging functionality This module allows enabling and disabling logging, setting filters, and outputting log messages.

Examples:

Enabling logging with a filter

Log.on(/error/) 
Log.out("This is an error message", label: :error)

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:

Formatting a log message

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

Parameters:

  • msg (String)

    The message to format.

  • label (Symbol)

    The label to prepend to the message.

Returns:

  • (String)

    The formatted log message.



133
134
135
136
137
138
# File 'documented/common/log.rb', line 133

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

Examples:

Writing a line to output

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

Parameters:

  • line (String)

    The line to write to output.

Returns:

  • (nil)

    Returns nil after writing the line.



117
118
119
120
121
122
123
124
125
# File 'documented/common/log.rb', line 117

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 log message, alias for pp.

Examples:

Dumping a log message

Log.dump("This is a dump message")

Parameters:

  • args (*Object)

    The arguments to log.

Returns:

  • (nil)

    Returns nil after logging the message.



155
156
157
# File 'documented/common/log.rb', line 155

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

.filterRegexp

Retrieves the current log filter.

Examples:

Getting the current log filter

current_filter = Log.filter

Returns:

  • (Regexp)

    The current log filter as a Regexp.

Raises:

  • SQLite3::BusyException if the database is busy.



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'documented/common/log.rb', line 80

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:

Disabling logging

Log.off

Returns:

  • (nil)

    Returns nil after disabling logging.

Raises:

  • SQLite3::BusyException if the database is busy.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'documented/common/log.rb', line 41

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:

Enabling logging with a specific filter

Log.on(/warning/)

Parameters:

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

    The filter to apply to log messages.

Returns:

  • (nil)

    Returns nil after enabling logging.

Raises:

  • SQLite3::BusyException if the database is busy.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'documented/common/log.rb', line 23

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 currently enabled.

Examples:

Checking if logging is enabled

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

Returns:

  • (Boolean)

    Returns 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 'documented/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:

Logging a message

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

Parameters:

  • msg (String, Exception)

    The message or exception to log.

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

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

Returns:

  • (nil)

    Returns nil after attempting to log the message.



100
101
102
103
104
105
106
107
108
109
110
# File 'documented/common/log.rb', line 100

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

Outputs a pretty-printed log message.

Examples:

Pretty-printing a log message

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)

    Returns nil after logging the message.



146
147
148
# File 'documented/common/log.rb', line 146

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