Sha256: 521756e775cbe605241799c105853d9c5e1ea0ac82a3e7b9cc96e717bcc599ac

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

require "trace_tree/version"
require 'binding_of_callers/pry'
require 'trace_tree/node'
require 'trace_tree/short_gem_path'
require 'trace_tree/color'
require 'trace_tree/tmp_file'

class Binding
  def trace_tree *log, **opt, &to_do
    TraceTree.new(self).generate *log, **opt, &to_do
  end
end

class TraceTree

  def initialize bi
    @bi = bi
    @trace_points = []
  end

  def generate *log, **opt, &to_do
    @log = dump_location *log, **opt
    node_class = optional_node opt
    @build_command = opt[:html] ? :tree_html_full : :tree_graph

    tp = TracePoint.trace(:call, :b_call, :raise, :c_call) do |point|
      trace_points << node_class.new(point) if wanted? point
    end

    bi.eval('self').instance_eval &to_do
  ensure
    tp.disable
    dump_trace_tree
  end

  private

  attr_reader :bi, :trace_points, :log, :build_command

  def dump_location *log, **opt
    return TmpFile.new opt[:tmp] if opt[:tmp]
    log.empty? ? STDOUT : log[0]
  end

  def optional_node opt
    Class.new TraceTree::Node do
      prepend TraceTree::ShortGemPath unless opt[:gem] == false
      prepend TraceTree::Color unless opt[:color] == false
    end
  end

  def dump_trace_tree
    tree = sort(trace_points).send build_command
    log.puts tree
  end

  def wanted? trace_point
    trace_point.event != :c_call or trace_point.method_id == :throw
  end

  def sort stack
    hash = {}
    stack.each do |call|
      unless hash.empty?
        parent = hash[call.parent_stack]
        parent << call if parent
      end
      hash[call.whole_stack] = call
    end
    stack[0]
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trace_tree-0.1.3 lib/trace_tree.rb