Skip to content

Instantly share code, notes, and snippets.

@murdho
Last active October 24, 2018 12:40
Show Gist options
  • Save murdho/8486235f430b56d5f980817f33ee3cc0 to your computer and use it in GitHub Desktop.
Save murdho/8486235f430b56d5f980817f33ee3cc0 to your computer and use it in GitHub Desktop.
Show trace of method calls and returns (only project files)
@trace_depth = 0
@trace = TracePoint.new(:call, :return) do |tp|
if tp.path.include?("/murdho/Developer/APP_NAME_HERE")
is_a_call = tp.event == :call
is_a_call ? @trace_depth += 1 : @trace_depth -= 1
@trace_depth = 0 if @trace_depth < 0
puts [
" " * @trace_depth,
"\e[33m", # Yellow color start
tp.defined_class,
"\e[0m", # Custom color end
"#",
"\e[32m", # Green color start
tp.method_id,
"\e[0m", # Custom color end
" ",
is_a_call ? "got called" : "returned",
" (",
"\e[34m", # Blue color start
tp.path,
"\e[0m", # Custom color end
":",
"\e[31m", # Red color start
tp.lineno,
"\e[0m", # Custom color end
")"
].join
end
end
# @trace.enable
# @trace.disable
# https://ruby-doc.org/core-2.5.1/TracePoint.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment