Module: Legion::Logging::MethodTracer
- Defined in:
- lib/legion/logging/method_tracer.rb
Constant Summary collapse
- ENABLED =
false- ATTACHED =
rubocop:disable Style/MutableConstant
{}
Class Method Summary collapse
- .attach(base, match_singleton: false) ⇒ Object
- .detach(base) ⇒ Object
- .detach_all ⇒ Object
- .format_params(trace_point) ⇒ Object
Class Method Details
.attach(base, match_singleton: false) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/legion/logging/method_tracer.rb', line 11 def self.attach(base, match_singleton: false) return unless ENABLED ATTACHED_MUTEX.synchronize do return if ATTACHED.key?(base) base_name = base.to_s tp = TracePoint.new(:call, :return) do |trace| next unless trace.defined_class == base || (match_singleton && trace.defined_class == base.singleton_class) stack = (Thread.current[:_legion_trace_stack] ||= []) case trace.event when :call params = format_params(trace) params_segment = params.empty? ? '' : ", #{params.join(', ')}" indent = ' ' * stack.size puts "#{indent}-> #{trace.method_id}, #{base_name}#{params_segment}" stack.push(trace.method_id) when :return stack.pop indent = ' ' * stack.size puts "#{indent}<- #{trace.method_id}, #{base_name}" end end tp.enable ATTACHED[base] = tp end end |
.detach(base) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/legion/logging/method_tracer.rb', line 41 def self.detach(base) ATTACHED_MUTEX.synchronize do tp = ATTACHED.delete(base) tp&.disable end end |
.detach_all ⇒ Object
48 49 50 51 52 53 |
# File 'lib/legion/logging/method_tracer.rb', line 48 def self.detach_all ATTACHED_MUTEX.synchronize do ATTACHED.each_value(&:disable) ATTACHED.clear end end |
.format_params(trace_point) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/legion/logging/method_tracer.rb', line 55 def self.format_params(trace_point) trace_point.parameters.filter_map do |type, name| next unless name val = begin trace_point.binding.local_variable_get(name) rescue StandardError '?' end case type when :req, :opt then "#{name}=#{val.inspect}" when :keyreq, :key then "#{name}: #{val.inspect}" when :rest then "*#{name}" when :keyrest then "**#{name}" end end end |