Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@txus
Last active December 18, 2015 17:59
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save txus/5822580 to your computer and use it in GitHub Desktop.
Save txus/5822580 to your computer and use it in GitHub Desktop.
Pry command to trace a method invocation and log backtraces of each invocation to a file.
def write_backtrace name, backtrace, filename
exceptions = Regexp.union([
/\.bundle/,
/spec/,
/test/,
/lib\/ruby\/1.9.1/
])
backtrace.reject! { |line| line =~ exceptions }
File.open(filename, 'a') do |f|
f.write "* #{name}\n\n" + backtrace.join("\n") + "\n\n"
end
end
# Example usage:
#
# trace Redis::Client#process my_redis_calls.log
#
Pry.commands.block_command(/trace ([^ ]+) ([^ ]+)$/, "Trace a method invocation and log it to a file", :listing => "trace") do |bp, filename|
run "breakpoint #{bp} if (write_backtrace(%{#{bp}}, caller, %{#{filename}}); false)"
run "continue"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment