Class: DEBUGGER__::Tracer

Inherits:
Object show all
Includes:
Color, SkipPathHelper
Defined in:
lib/debug/tracer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Color

#color_pp, #colored_inspect, #colorize_blue, #colorize_code, #colorize_cyan, #colorize_dim, #colorize_magenta, #irb_colorize, #with_inspection_error_guard

Methods included from SkipPathHelper

#skip_config_skip_path?, #skip_internal_path?, #skip_location?, #skip_path?

Constructor Details

#initialize(ui, pattern: nil, into: nil) ⇒ Tracer

Returns a new instance of Tracer.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/debug/tracer.rb', line 19

def initialize ui, pattern: nil, into: nil
  if /\ADEBUGGER__::(([A-Z][a-z]+?)[A-Z][a-z]+)/ =~ self.class.name
    @name = $1
    @type = $2.downcase
  end

  setup

  if pattern
    @pattern = Regexp.compile(pattern)
  else
    @pattern = nil
  end

  if @into = into
    @output = File.open(into, 'w')
    @output.puts "PID:#{Process.pid} #{self}"
  else
    @output = ui
  end

  @key = [@type, @pattern, @into].freeze

  enable
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



17
18
19
# File 'lib/debug/tracer.rb', line 17

def key
  @key
end

#typeObject (readonly)

Returns the value of attribute type.



17
18
19
# File 'lib/debug/tracer.rb', line 17

def type
  @type
end

Instance Method Details

#colorize(str, color) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/debug/tracer.rb', line 8

def colorize(str, color)
  # don't colorize trace sent into a file
  if @into
    str
  else
    super
  end
end

#descriptionObject



61
62
63
# File 'lib/debug/tracer.rb', line 61

def description
  nil
end

#disableObject



53
54
55
# File 'lib/debug/tracer.rb', line 53

def disable
  @tracer.disable
end

#enableObject



49
50
51
# File 'lib/debug/tracer.rb', line 49

def enable
  @tracer.enable
end

#enabled?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/debug/tracer.rb', line 57

def enabled?
  @tracer.enabled?
end

#header(depth) ⇒ Object



45
46
47
# File 'lib/debug/tracer.rb', line 45

def header depth
  "DEBUGGER (trace/#{@type}) \#th:#{Thread.current.instance_variable_get(:@__thread_client_id)} \#depth:#{'%-2d'%depth}"
end

#minfo(tp) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/debug/tracer.rb', line 92

def minfo tp
  return "block{}" if tp.event == :b_call

  klass = tp.defined_class

  if klass.singleton_class?
    "#{tp.self}.#{tp.method_id}"
  else
    "#{klass}\##{tp.method_id}"
  end
end

#out(tp, msg = nil, depth = caller.size - 1) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/debug/tracer.rb', line 80

def out tp, msg = nil, depth = caller.size - 1
  location_str = colorize("#{FrameInfo.pretty_path(tp.path)}:#{tp.lineno}", [:GREEN])
  buff = "#{header(depth)}#{msg} at #{location_str}"

  if false # TODO: Ractor.main?
    ThreadClient.current.on_trace self.object_id, buff
  else
    @output.puts buff
    @output.flush
  end
end

#skip?(tp) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/debug/tracer.rb', line 72

def skip? tp
  ThreadClient.current.management? || skip_path?(tp.path) || skip_with_pattern?(tp)
end

#skip_with_pattern?(tp) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/debug/tracer.rb', line 76

def skip_with_pattern?(tp)
  @pattern && !tp.path.match?(@pattern)
end

#to_sObject



65
66
67
68
69
70
# File 'lib/debug/tracer.rb', line 65

def to_s
  s = "#{@name}#{description} (#{@tracer.enabled? ? 'enabled' : 'disabled'})"
  s += " with pattern #{@pattern.inspect}" if @pattern
  s += " into: #{@into}" if @into
  s
end