Class: RubyMethodTracer::EnhancedTracer
- Inherits:
-
SimpleTracer
- Object
- SimpleTracer
- RubyMethodTracer::EnhancedTracer
- Defined in:
- lib/ruby_method_tracer/enhanced_tracer.rb
Overview
EnhancedTracer extends SimpleTracer with hierarchical call tracking
In addition to the basic tracing functionality, this tracer maintains a call tree that captures parent-child relationships between method calls, enabling visualization of complex call hierarchies.
Options:
-
All options from SimpleTracer
-
:track_hierarchy (Boolean): Enable call tree tracking; defaults to true
Usage:
tracer = RubyMethodTracer::EnhancedTracer.new(MyClass, threshold: 0.005)
tracer.trace_method(:expensive_call)
tracer.print_tree
Instance Attribute Summary collapse
-
#call_tree ⇒ Object
readonly
Returns the value of attribute call_tree.
Instance Method Summary collapse
-
#clear_results ⇒ Object
Clear both simple tracer results and call tree.
-
#fetch_enhanced_results ⇒ Hash
Get enhanced results including both flat list and hierarchy.
-
#format_tree(options = {}) ⇒ String
Get call tree as string without printing.
-
#initialize(target_class, **options) ⇒ EnhancedTracer
constructor
A new instance of EnhancedTracer.
-
#print_tree(options = {}) ⇒ Object
Print the call tree visualization.
- #trace_method(name) ⇒ Object
Methods inherited from SimpleTracer
Constructor Details
#initialize(target_class, **options) ⇒ EnhancedTracer
Returns a new instance of EnhancedTracer.
25 26 27 28 29 30 |
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 25 def initialize(target_class, **) super @call_tree = CallTree.new @track_hierarchy = @options.fetch(:track_hierarchy, true) @formatter = Formatters::TreeFormatter.new end |
Instance Attribute Details
#call_tree ⇒ Object (readonly)
Returns the value of attribute call_tree.
23 24 25 |
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 23 def call_tree @call_tree end |
Instance Method Details
#clear_results ⇒ Object
Clear both simple tracer results and call tree
79 80 81 82 |
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 79 def clear_results super @call_tree.clear end |
#fetch_enhanced_results ⇒ Hash
Get enhanced results including both flat list and hierarchy
70 71 72 73 74 75 76 |
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 70 def fetch_enhanced_results { flat_calls: fetch_results, call_hierarchy: @call_tree.call_hierarchy, statistics: @call_tree.statistics } end |
#format_tree(options = {}) ⇒ String
Get call tree as string without printing
63 64 65 |
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 63 def format_tree( = {}) @formatter.format(@call_tree, ) end |
#print_tree(options = {}) ⇒ Object
Print the call tree visualization
55 56 57 |
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 55 def print_tree( = {}) puts @formatter.format(@call_tree, ) end |
#trace_method(name) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ruby_method_tracer/enhanced_tracer.rb', line 32 def trace_method(name) method_name = name.to_sym visibility = method_visibility(method_name) return unless visibility return unless mark_wrapped?(method_name) aliased = alias_for(method_name) @target_class.send(:alias_method, aliased, method_name) tracer = self key = @tracer_key # unique per tracer instance; prevents cross-tracer interference # Build wrapper that tracks hierarchy @target_class.define_method(method_name, &build_enhanced_wrapper(aliased, method_name, key, tracer)) @target_class.send(visibility, method_name) end |