set_trace_func and filter on regex
Wednesday, August 29th, 2007I needed to use the trace_func to figure out a bug, and I came up with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Tracer class << self def on(io = STDERR, regex = nil) set_trace_func proc { |event, file, line, id, binding, classname| if !regex.blank? s = format("%8s %s:%-2d %10s %8s\n", event, file, line, id, classname) io.printf(s) if s =~ regex else io.printf("%8s %s:%-2d %10s %8s\n", event, file, line, id, classname) end } end def off set_trace_func nil end def trace(io = STDERR, regex = nil, &block) on(io, regex) retval = yield if block_given? off retval end end end end |
Then Tracer.trace { 1 + 1 } or specify a regex filter:
1 2 3 4 5 |
>> Tracer.trace(STDERR, /Fixnum/) { 1 + 1 } c-call (irb):2 + Fixnum c-return (irb):2 + Fixnum => 2 >> |
