Skip to content

Instantly share code, notes, and snippets.

@brainopia
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainopia/806d4cc48b644ee941ef to your computer and use it in GitHub Desktop.
Save brainopia/806d4cc48b644ee941ef to your computer and use it in GitHub Desktop.
method_calls
def method_calls(detailed: false, thread: nil)
[].tap do |log|
ident = -1
trace = TracePoint.new(:call, :return, :c_call, :c_return) do |tp|
break if thread and thread != Thread.current
case tp.event
when :call, :c_call
if tp.event == :call or detailed
ident += 1
log << [' '*ident + tp.defined_class.inspect + '#' + tp.method_id.to_s]
end
when :return, :c_return
if tp.event == :return or detailed
ident -= 1
end
else
raise
end
end
trace.enable { yield }
end
end
@brainopia
Copy link
Author

def method_calls(detailed: false, thread: nil)
  [].tap do |log|
    ident = -1
    trace = TracePoint.new(:call, :return, :c_call, :c_return) do |tp|
      break if thread and thread != Thread.current

      case tp.event
      when :call, :c_call
        if tp.event == :call or detailed
          ident += 1
          log << ['  '*ident + tp.defined_class.inspect + '#' + tp.method_id.to_s]
        end
      when :return, :c_return
        if tp.event == :return or detailed
          ident -= 1
        end
      else
        raise
      end
    end
    trace.enable { yield }
  end
end

require 'bundler'
Bundler.require

app = Rack::Builder.app do
  use Rack::Runtime
  use Rack::Sendfile
  use Rack::Static
  use Rack::Cascade
  run [
      proc { [404, {}, []] },
      proc { [200, {}, ["hola"]] },
      proc { raise }
  ]
end

request = Rack::MockRequest.env_for '/path'

puts method_calls(detailed: ARGV.first) {
  app.call request
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment