Module: Grueserve::Debuggable
Constant Summary
collapse
- LEVELS =
[:trace, :debug, :info, :warn, :fatal]
- @@counters =
{}
- @@timers =
{}
- @@maxes =
{}
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/grueserve/debuggable.rb', line 80
def method_missing(meth, *args)
if match = meth.to_s.match(/log_(.*)$/)
level = match[1].to_sym
if LEVELS.include?(level) && args.size == 1
Debuggable.output(level, *args)
else
super
end
else
super
end
end
|
Class Method Details
97
98
99
|
# File 'lib/grueserve/debuggable.rb', line 97
def self.format(s, level)
STDERR.puts("#{Time.now}\t#{level}\t#{source_part(caller)}\t#{s}")
end
|
.level ⇒ Object
37
38
39
|
# File 'lib/grueserve/debuggable.rb', line 37
def self.level
@level
end
|
.level=(level) ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/grueserve/debuggable.rb', line 29
def self.level=(level)
if LEVELS.include?(level)
@level = level
else
raise "The only debug levels allowed are '#{LEVELS.join(", ")}'."
end
end
|
.output(level, m) ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/grueserve/debuggable.rb', line 101
def self.output(level, m)
if LEVELS.index(@level) <= LEVELS.index(level)
format(m, level)
if m.respond_to?(:backtrace)
m.backtrace.each do |line|
format(line, level)
end
end
end
end
|
.source_part(caller_ary) ⇒ Object
93
94
95
|
# File 'lib/grueserve/debuggable.rb', line 93
def self.source_part(caller_ary)
caller_ary[2].gsub(/^.*\/(grueserve\/.*\.rb.*)$/, '\1')
end
|
Instance Method Details
#report ⇒ Object
74
75
76
77
78
|
# File 'lib/grueserve/debuggable.rb', line 74
def report
@@timers.keys.each do |k|
report_for(k)
end
end
|
#report_for(name) ⇒ Object
67
68
69
70
71
72
|
# File 'lib/grueserve/debuggable.rb', line 67
def report_for(name)
@@timers[name] ||= 0
@@counters[name] ||= 0
@@maxes[name] ||= 0
log_info("#{name}: total: #{@@counters[name]} in #{@@timers[name]}, avg: #{@@counters[name] == 0 ? "0" : @@timers[name] / @@counters[name]}, max: #{@@maxes[name]}")
end
|
#reset ⇒ Object
61
62
63
64
65
|
# File 'lib/grueserve/debuggable.rb', line 61
def reset
@@timers = {}
@@counters = {}
@@maxes = {}
end
|
#reset_for(name) ⇒ Object
55
56
57
58
59
|
# File 'lib/grueserve/debuggable.rb', line 55
def reset_for(name)
@@counters[name] = 0
@@timers[name] = 0
@@maxes[name] = 0
end
|
#time(name, &block) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/grueserve/debuggable.rb', line 41
def time(name, &block)
@@counters[name] ||= 0
@@timers[name] ||= 0
@@maxes[name] ||= 0
t = Time.now.to_f
@@timers[name] -= t
rval = yield
t2 = Time.now.to_f
@@timers[name] += t2
@@counters[name] += 1
@@maxes[name] = t2 - t if t2 - t > @@maxes[name]
return rval
end
|