Module: Cmds::Debug

Defined in:
lib/cmds/debug.rb

Constant Summary collapse

THREAD_COLORS =

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

{
  'INPUT' => :cyan,
  'OUTPUT' => :green,
  'ERROR' => :red,
}
@@on =

class variables

false
@@logger =
nil

Class Method Summary collapse

Class Method Details

.configure(dest = $stdout) ⇒ Object

configure the Logger with optional destination



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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?Boolean

test if the logger is configured.

Returns:

  • (Boolean)


37
38
39
# File 'lib/cmds/debug.rb', line 37

def self.configured?
  !! @@logger
end

.format(msg, values = {}) ⇒ Object

format a debug message with optional key / values to print



85
86
87
88
89
90
91
# 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

.loggerObject

get the Logger instance. may be nil.



32
33
34
# File 'lib/cmds/debug.rb', line 32

def self.logger
  @@logger
end

.offObject

turn debug logging off.



75
76
77
# File 'lib/cmds/debug.rb', line 75

def self.off
  @@on = false
end

.on(&block) ⇒ Object

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



65
66
67
68
69
70
71
72
# File 'lib/cmds/debug.rb', line 65

def self.on &block
  configure unless configured?
  @@on = true
  if block
    yield
    off
  end
end

.on?Boolean

test if debug logging is on.

Returns:

  • (Boolean)


80
81
82
# File 'lib/cmds/debug.rb', line 80

def self.on?
  @@on
end