Skip to content

Commit 54bf47c

Browse files
committed
refactor: cache debug env var and restore native warning
1 parent 27bd2cb commit 54bf47c

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

gems/pure-ruby-tracer/lib/recorder.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
require 'fileutils'
66

7+
DEBUG_TRACER = ENV['CODETRACER_RUBY_TRACER_DEBUG'] == '1'
8+
79
CallRecord = Struct.new(:function_id, :args) do
810
def to_data_for_json
911
res = to_h
@@ -246,9 +248,7 @@ def load_exprs(path)
246248
end
247249

248250
def serialize(program, out_dir = nil)
249-
if ENV["CODETRACER_RUBY_TRACER_DEBUG"] == "1"
250-
pp @events
251-
end
251+
pp @events if DEBUG_TRACER
252252

253253
output = @events.map { |kind, event| [[kind, event.to_data_for_json]].to_h }
254254

@@ -277,8 +277,10 @@ def serialize(program, out_dir = nil)
277277
File.write(trace_metadata_path, metadata_json_output)
278278
File.write(trace_paths_path, paths_json_output)
279279

280-
$stderr.write("=================================================\n")
281-
$stderr.write("codetracer ruby tracer: saved trace to #{trace_folder}\n")
280+
if DEBUG_TRACER
281+
$stderr.write("=================================================\n")
282+
$stderr.write("codetracer ruby tracer: saved trace to #{trace_folder}\n")
283+
end
282284
end
283285
end
284286

@@ -353,7 +355,7 @@ def to_value(v, depth=10)
353355
end
354356
$VALUE_COUNT += 1
355357
if $VALUE_COUNT % 10_000 == 0
356-
$stderr.write("value #{$VALUE_COUNT}\n")
358+
$stderr.write("value #{$VALUE_COUNT}\n") if DEBUG_TRACER
357359
end
358360
case v
359361
when Integer

gems/pure-ruby-tracer/lib/trace.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
require 'optparse'
77
require_relative 'recorder'
88

9+
DEBUG_TRACER = ENV['CODETRACER_RUBY_TRACER_DEBUG'] == '1'
10+
911
# Warning:
1012
# probably related to our development env:
1113
# if we hit an `incompatible library version` error, like
@@ -158,7 +160,7 @@ def record_call(tp)
158160
method_name_prefix = module_name == 'Object' ? '' : "#{module_name}#"
159161
method_name = "#{method_name_prefix}#{tp.method_id}"
160162

161-
old_puts "call #{method_name} with #{tp.parameters}"
163+
old_puts "call #{method_name} with #{tp.parameters}" if DEBUG_TRACER
162164

163165
arg_records = prepare_args(tp)
164166

@@ -170,7 +172,7 @@ def record_call(tp)
170172

171173
def record_return(tp)
172174
if self.tracks_call?(tp)
173-
old_puts "return"
175+
old_puts "return" if DEBUG_TRACER
174176
return_value = to_value(tp.return_value)
175177
@record.register_step(tp.path, tp.lineno)
176178
# return value support inspired by existing IDE-s/envs like
@@ -281,13 +283,15 @@ def load_variables(binding)
281283
begin
282284
Kernel.load(program)
283285
rescue Exception => e
284-
old_puts ''
285-
old_puts '==== trace.rb error while tracing program ==='
286-
old_puts 'ERROR'
287-
old_puts e
288-
old_puts e.backtrace
289-
old_puts '====================='
290-
old_puts ''
286+
if DEBUG_TRACER
287+
old_puts ''
288+
old_puts '==== trace.rb error while tracing program ==='
289+
old_puts 'ERROR'
290+
old_puts e
291+
old_puts e.backtrace
292+
old_puts '====================='
293+
old_puts ''
294+
end
291295
end
292296

293297
$tracer.stop_tracing

0 commit comments

Comments
 (0)