lib/trace_location/collector.rb in trace_location-0.4.0 vs lib/trace_location/collector.rb in trace_location-0.9.0

- old
+ new

@@ -1,36 +1,54 @@ # frozen_string_literal: true +require 'binding_of_caller' + module TraceLocation module Collector # :nodoc: require_relative 'event' Result = Struct.new(:events, :return_value) - def self.collect(&block) + def self.collect(match:, ignore:, &block) events = [] hierarchy = 0 + id = 0 + cache = {} + tracer = TracePoint.new(:call, :return) do |trace_point| - next unless trace_point.path.include?(::TraceLocation.config.gems_dir) + next if match && !trace_point.path.to_s.match?(/#{match}/) + next if ignore && trace_point.path.to_s.match?(/#{ignore}/) + id += 1 + caller_path, caller_lineno = trace_point.binding.of_caller(2).source_location + location_cache_key = "#{caller_path}:#{caller_lineno}" + case trace_point.event when :call + cache[location_cache_key] = hierarchy + events << Event.new( + id: id, event: trace_point.event, path: trace_point.path, lineno: trace_point.lineno, + caller_path: caller_path, + caller_lineno: caller_lineno, method_id: trace_point.method_id, defined_class: trace_point.defined_class, hierarchy: hierarchy ) hierarchy += 1 when :return - hierarchy -= 1 + hierarchy = cache[location_cache_key] || hierarchy events << Event.new( + id: id, event: trace_point.event, path: trace_point.path, lineno: trace_point.lineno, + caller_path: caller_path, + caller_lineno: caller_lineno, method_id: trace_point.method_id, defined_class: trace_point.defined_class, hierarchy: hierarchy ) end