module Cmds::Debug

Constants

THREAD_COLORS

change the color of debug output by thread name (if present)

Public Class Methods

configure(dest = $stdout) click to toggle source

configure the Logger with optional destination

# File lib/cmds/debug.rb, line 42
def self.configure dest = $stdout
  require 'pastel'
  @@pastel = Pastel.new

  @@logger = Logger.new dest
  @@logger.level = Logger::DEBUG
  @@logger.formatter = proc do |severity, datetime, progname, msg|
    if Thread.current[:name]
      msg = "[Cmds #{ severity } - #{ Thread.current[:name ] }] #{msg}\n"

      if color = THREAD_COLORS[Thread.current[:name]]
        msg = @@pastel.method(color).call msg
      end

      msg
    else
      "[Cmds #{ severity }] #{msg}\n"
    end
  end
end
configured?() click to toggle source

test if the logger is configured.

# File lib/cmds/debug.rb, line 37
def self.configured?
  !! @@logger
end
format(msg, values = {}) click to toggle source

format a debug message with optional key / values to print

# File lib/cmds/debug.rb, line 85
def self.format msg, values = {}
  if values.empty?
    msg
  else
    msg + "\n" + values.map {|k, v| "  #{ k }: #{ v.inspect }" }.join("\n")
  end
end
logger() click to toggle source

get the Logger instance. may be `nil`.

# File lib/cmds/debug.rb, line 32
def self.logger
  @@logger
end
off() click to toggle source

turn debug logging off.

# File lib/cmds/debug.rb, line 75
def self.off
  @@on = false
end
on() { || ... } click to toggle source

turn debug logging on. if you provide a block it will turn debug logging on for that block and off at the end.

# File lib/cmds/debug.rb, line 65
def self.on &block
  configure unless configured?
  @@on = true
  if block
    yield
    off
  end
end
on?() click to toggle source

test if debug logging is on.

# File lib/cmds/debug.rb, line 80
def self.on?
  @@on
end