Skip to content

Commit 73ec071

Browse files
committed
feat(tracer): inject debug mode via constructor
1 parent d1d19b6 commit 73ec071

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

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

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

55
require 'fileutils'
66

7-
DEBUG_TRACER = ENV['CODETRACER_RUBY_TRACER_DEBUG'] == '1'
8-
97
CallRecord = Struct.new(:function_id, :args) do
108
def to_data_for_json
119
res = to_h
@@ -115,6 +113,7 @@ class TraceRecord
115113
attr_accessor :stack, :step_stack, :exprs, :tracing
116114
attr_accessor :t1, :t2, :t3, :t4 # tracepoints
117115
attr_accessor :codetracer_id
116+
attr_accessor :debug
118117

119118
def initialize
120119
@events = []
@@ -139,6 +138,7 @@ def initialize
139138
@t2 = nil
140139
@t3 = nil
141140
@codetracer_id = 0
141+
@debug = false
142142
end
143143

144144
def load_flow(path, line, binding)
@@ -248,7 +248,7 @@ def load_exprs(path)
248248
end
249249

250250
def serialize(program, out_dir = nil)
251-
pp @events if DEBUG_TRACER
251+
pp @events if @debug
252252

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

@@ -277,7 +277,7 @@ 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-
if DEBUG_TRACER
280+
if @debug
281281
$stderr.write("=================================================\n")
282282
$stderr.write("codetracer ruby tracer: saved trace to #{trace_folder}\n")
283283
end
@@ -355,7 +355,7 @@ def to_value(v, depth=10)
355355
end
356356
$VALUE_COUNT += 1
357357
if $VALUE_COUNT % 10_000 == 0
358-
$stderr.write("value #{$VALUE_COUNT}\n") if DEBUG_TRACER
358+
$stderr.write("value #{$VALUE_COUNT}\n") if $codetracer_record.debug
359359
end
360360
case v
361361
when Integer

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

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

9-
DEBUG_TRACER = ENV['CODETRACER_RUBY_TRACER_DEBUG'] == '1'
109

1110
# Warning:
1211
# probably related to our development env:
@@ -71,13 +70,15 @@ class Tracer
7170
attr_accessor :calls_tracepoint, :return_tracepoint,
7271
:line_tracepoint, :raise_tracepoint, :tracing
7372

74-
attr_reader :ignore_list, :record
73+
attr_reader :ignore_list, :record, :debug
7574

76-
def initialize(record)
75+
def initialize(record, debug: ENV['CODETRACER_RUBY_TRACER_DEBUG'] == '1')
7776
@tracing = false
7877
@trace_stopped = false
7978
@record = record
8079
@ignore_list = []
80+
@debug = debug
81+
@record.debug = debug if @record.respond_to?(:debug=)
8182
setup_tracepoints
8283
end
8384

@@ -160,7 +161,7 @@ def record_call(tp)
160161
method_name_prefix = module_name == 'Object' ? '' : "#{module_name}#"
161162
method_name = "#{method_name_prefix}#{tp.method_id}"
162163

163-
old_puts "call #{method_name} with #{tp.parameters}" if DEBUG_TRACER
164+
old_puts "call #{method_name} with #{tp.parameters}" if @debug
164165

165166
arg_records = prepare_args(tp)
166167

@@ -172,7 +173,7 @@ def record_call(tp)
172173

173174
def record_return(tp)
174175
if self.tracks_call?(tp)
175-
old_puts "return" if DEBUG_TRACER
176+
old_puts "return" if @debug
176177
return_value = to_value(tp.return_value)
177178
@record.register_step(tp.path, tp.lineno)
178179
# return value support inspired by existing IDE-s/envs like
@@ -283,7 +284,7 @@ def load_variables(binding)
283284
begin
284285
Kernel.load(program)
285286
rescue Exception => e
286-
if DEBUG_TRACER
287+
if @debug
287288
old_puts ''
288289
old_puts '==== trace.rb error while tracing program ==='
289290
old_puts 'ERROR'

0 commit comments

Comments
 (0)