Module: DebugMe

Defined in:
lib/debug_me.rb,
lib/debug_me/version.rb

Constant Summary collapse

DEBUG_ME_VERSION =

I know. Its weird to duplicate the name space; but, doing this allows you to include DebugMe in your code to get direct access to the debug_me method without polluting your code with an object name that is so common.

'1.1.1'

Instance Method Summary collapse

Instance Method Details

#debug_me(options = {}, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/debug_me.rb', line 24

def debug_me(options = {}, &block)
  return unless $DEBUG_ME

  if 'Hash' == options.class.to_s
    options = DebugMeDefaultOptions.merge(options)
  else
    options = DebugMeDefaultOptions.merge(tag: options)
  end

  skip        = ''
  skip        = "\n"    if options[:skip1]
  skip        = "\n\n"  if options[:skip2]
  out_string  = ''

  f = options[:file]
  l = options[:logger]
  s = ''
  s += Time.now.strftime(options[:strftime])+' ' if options[:time]
  s += "#{options[:tag]}"
  bt = caller # where_from under 1.8.6 its a stack trace array under 1.8.7+ as a string
  bt_out = []

  if options[:header]
    cf = bt.is_a?(Array) ? bt[0] : bt
    out_string = sprintf("%s Source:  %s\n", skip+s, cf)
    if options[:levels] > 0
      levels = options[:levels].to_i
      bt[1..levels].each_with_index do |cff, level |
        bt_out << [level, cff]
      end
    end
  end

  if block_given? || bt_out.size > 0
    block_value = []
    block_value << 'backtrace' if bt_out.size > 0
    if block_given?
      [block.call].flatten.compact.each do |v|
        block_value << v
      end
    end

    if block_value.empty?
      block_value += [eval('local_variables', block.binding)] if options[:lvar]
      block_value += [eval('instance_variables', block.binding)] if options[:ivar]
      block_value += [self.class.send('class_variables')] if options[:cvar]
      block_value += [self.class.constants] if options[:cconst]
      block_value = block_value.flatten.compact
    end

    block_value.map!(&:to_s)

    block_value.each do |v|

      ev  = if 'backtrace' == v
              bt_out.size > 0 ? bt_out : bt[1..10000]
            else
              eval("defined?(#{v})",block.binding).nil? ? '<undefined>' : eval(v, block.binding)
            end
      out_string += sprintf( "%s %s -=> %s", s,v,ev.pretty_inspect)
    end

  end ## if block_given?

  unless f.nil?
    f.puts out_string
    f.flush
  end

  unless l.nil?
    l.debug(out_string) if l.respond_to? :debug
  end

  return out_string
end