Last active
February 27, 2018 14:07
-
-
Save cheerfulstoic/29966e9f1dc3ef94bf0d to your computer and use it in GitHub Desktop.
trace_execution
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
def trace_execution(levels = 4) | |
cyan = "\e[36m" | |
clear = "\e[0m" | |
green = "\e[32m" | |
indent = 0 | |
output = '' | |
trace = TracePoint.new(:call, :c_call, :return, :c_return) do |tp| | |
if [:return, :c_return].include?(tp.event) && indent.nonzero? | |
indent -= 1 | |
else | |
if indent <= levels | |
parts = [] | |
parts << "#{'| ' * indent}" | |
parts << "#{cyan if tp.event == :call}%-8s#{clear}" | |
parts << "%s:%-4d %-18s\n" | |
output << parts.join(' ') % [tp.event, tp.path, tp.lineno, tp.defined_class.to_s + '#' + green + tp.method_id.to_s + clear] | |
end | |
indent += 1 | |
end | |
end | |
trace.enable | |
yield | |
ensure | |
trace.disable | |
puts output | |
end | |
trace_execution do | |
# Code you want to trace goes here | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment