Created
September 28, 2017 11:22
-
-
Save JesusTinoco/a6bce16c056ac35df1b8da883b694708 to your computer and use it in GitHub Desktop.
Tracer For Ruby (https://dalibornasevic.com/posts/51-tracing-ruby-code)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# trace the whatever_code_to_be_traced | |
trace(:call) do | |
whatever_code_to_be_traced | |
end | |
# filter by 'state_machine' files | |
trace(:call, 'state_machine') do | |
whatever_code_to_be_traced | |
end | |
def trace(event_type = :call, *matchers) | |
points = [] | |
tracer = TracePoint.new(event_type) do |trace_point| | |
if matchers.all? { |match| trace_point.path.match(match) } | |
points << { file: trace_point.path, line: trace_point.lineno, } | |
end | |
end | |
result = tracer.enable { yield } | |
File.open('/tmp/trace.txt', 'w') do |file| | |
points.each do |point| | |
file.puts "#{point[:file]}:#{point[:line]}" | |
end | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment