55require 'json'
66require 'optparse'
77require_relative 'recorder'
8- require_relative '../../../codetracer/kernel_patches'
8+ require_relative 'codetracer/kernel_patches'
9+
10+ # Helper to access the original +puts+ implementation when kernel
11+ # methods are patched by {Codetracer::KernelPatches}. This avoids
12+ # tracing debug output while still functioning even if the patches
13+ # are not installed.
14+ def codetracer_puts_no_trace ( *args )
15+ if Kernel . private_method_defined? ( :codetracer_original_puts )
16+ Kernel . send ( :codetracer_original_puts , *args )
17+ else
18+ Kernel . puts ( *args )
19+ end
20+ end
921
1022
1123# Warning:
@@ -127,7 +139,7 @@ def record_call(tp)
127139 method_name_prefix = module_name == 'Object' ? '' : "#{ module_name } #"
128140 method_name = "#{ method_name_prefix } #{ tp . method_id } "
129141
130- old_puts "call #{ method_name } with #{ tp . parameters } " if $tracer. debug
142+ codetracer_puts_no_trace "call #{ method_name } with #{ tp . parameters } " if $tracer. debug
131143
132144 arg_records = prepare_args ( tp )
133145
@@ -139,7 +151,7 @@ def record_call(tp)
139151
140152 def record_return ( tp )
141153 if self . tracks_call? ( tp )
142- old_puts "return" if $tracer. debug
154+ codetracer_puts_no_trace "return" if $tracer. debug
143155 return_value = to_value ( tp . return_value )
144156 @record . register_step ( tp . path , tp . lineno )
145157 # return value support inspired by existing IDE-s/envs like
@@ -160,22 +172,23 @@ def record_step(tp)
160172 end
161173 end
162174
163- def record_event ( caller , content )
164- # reason/effect are on different steps:
165- # reason: before `p` is called;
166- # effect: now, when the args are evaluated
167- # which can happen after many calls/steps;
168- # maybe add a step for this call?
169- begin
170- location = caller [ 0 ] . split [ 0 ] . split ( ':' ) [ 0 ..1 ]
171- path , line = location [ 0 ] , location [ 1 ] . to_i
172- @record . register_step ( path , line )
173- rescue
174- # ignore for now: we'll just jump to last previous step
175- # which might be from args
175+ def record_event ( *args )
176+ if args . length == 2
177+ caller , content = args
178+ begin
179+ location = caller [ 0 ] . split [ 0 ] . split ( ':' ) [ 0 ..1 ]
180+ path , line = location [ 0 ] , location [ 1 ] . to_i
181+ @record . register_step ( path , line )
182+ rescue
183+ # ignore for now
184+ end
185+ @record . events << [ :Event , RecordEvent . new ( EVENT_KIND_WRITE , content , "" ) ]
186+ elsif args . length == 3
187+ path , line , content = args
188+ record_event ( [ "#{ path } :#{ line } " ] , content )
189+ else
190+ raise ArgumentError , "wrong number of arguments"
176191 end
177- # start is last step on this level: log for reason: the previous step on this level
178- @record . events << [ :Event , RecordEvent . new ( EVENT_KIND_WRITE , content , "" ) ]
179192 end
180193
181194 def record_exception ( tp )
@@ -254,13 +267,13 @@ def load_variables(binding)
254267 Kernel . load ( program )
255268 rescue Exception => e
256269 if $tracer. debug
257- old_puts ''
258- old_puts '==== trace.rb error while tracing program ==='
259- old_puts 'ERROR'
260- old_puts e
261- old_puts e . backtrace
262- old_puts '====================='
263- old_puts ''
270+ codetracer_puts_no_trace ''
271+ codetracer_puts_no_trace '==== trace.rb error while tracing program ==='
272+ codetracer_puts_no_trace 'ERROR'
273+ codetracer_puts_no_trace e
274+ codetracer_puts_no_trace e . backtrace
275+ codetracer_puts_no_trace '====================='
276+ codetracer_puts_no_trace ''
264277 end
265278 end
266279
0 commit comments