Class: Tracer
- Inherits:
-
Object
- Object
- Tracer
- Defined in:
- lib/tracer.rb
Overview
tracer main class
Constant Summary collapse
- EVENT_SYMBOL =
{ "line" => "-", "call" => ">", "return" => "<", "class" => "C", "end" => "E", "c-call" => ">", "c-return" => "<", }
- Single =
new
Class Attribute Summary collapse
-
.stdout ⇒ Object
Returns the value of attribute stdout.
-
.verbose ⇒ Object
(also: verbose?)
Returns the value of attribute verbose.
Class Method Summary collapse
- .add_filter(p = proc) ⇒ Object
- .off ⇒ Object
- .on ⇒ Object
- .set_get_line_procs(file_name, p = proc) ⇒ Object
Instance Method Summary collapse
- #add_filter(p = proc) ⇒ Object
- #get_line(file, line) ⇒ Object
- #get_thread_no ⇒ Object
-
#initialize ⇒ Tracer
constructor
A new instance of Tracer.
- #off ⇒ Object
- #on ⇒ Object
- #set_get_line_procs(file, p = proc) ⇒ Object
- #stdout ⇒ Object
- #trace_func(event, file, line, id, binding, klass) ⇒ Object
Constructor Details
#initialize ⇒ Tracer
Returns a new instance of Tracer.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tracer.rb', line 37 def initialize @threads = Hash.new if defined? Thread.main @threads[Thread.main.object_id] = 0 else @threads[Thread.current.object_id] = 0 end @get_line_procs = {} @filters = [] end |
Class Attribute Details
.stdout ⇒ Object
Returns the value of attribute stdout.
24 25 26 |
# File 'lib/tracer.rb', line 24 def stdout @stdout end |
.verbose ⇒ Object Also known as: verbose?
Returns the value of attribute verbose.
22 23 24 |
# File 'lib/tracer.rb', line 22 def verbose @verbose end |
Class Method Details
.add_filter(p = proc) ⇒ Object
150 151 152 |
# File 'lib/tracer.rb', line 150 def Tracer.add_filter(p = proc) Single.add_filter(p) end |
Instance Method Details
#add_filter(p = proc) ⇒ Object
73 74 75 |
# File 'lib/tracer.rb', line 73 def add_filter(p = proc) @filters.push p end |
#get_line(file, line) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/tracer.rb', line 81 def get_line(file, line) if p = @get_line_procs[file] return p.call(line) end unless list = SCRIPT_LINES__[file] begin f = open(file) begin SCRIPT_LINES__[file] = list = f.readlines ensure f.close end rescue SCRIPT_LINES__[file] = list = [] end end if l = list[line - 1] l else "-\n" end end |
#get_thread_no ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/tracer.rb', line 106 def get_thread_no if no = @threads[Thread.current.object_id] no else @threads[Thread.current.object_id] = @threads.size end end |
#off ⇒ Object
68 69 70 71 |
# File 'lib/tracer.rb', line 68 def off set_trace_func nil stdout.print "Trace off\n" if Tracer.verbose? end |
#on ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tracer.rb', line 54 def on if block_given? on begin yield ensure off end else set_trace_func method(:trace_func).to_proc stdout.print "Trace on\n" if Tracer.verbose? end end |
#set_get_line_procs(file, p = proc) ⇒ Object
77 78 79 |
# File 'lib/tracer.rb', line 77 def set_get_line_procs(file, p = proc) @get_line_procs[file] = p end |
#trace_func(event, file, line, id, binding, klass) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/tracer.rb', line 114 def trace_func(event, file, line, id, binding, klass, *) return if file == __FILE__ for p in @filters return unless p.call event, file, line, id, binding, klass end saved_crit = Thread.critical Thread.critical = true stdout.printf("#%d:%s:%d:%s:%s: %s", get_thread_no, file, line, klass || '', EVENT_SYMBOL[event], get_line(file, line)) Thread.critical = saved_crit end |