ruby_tracer 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 752f406d1a3b8967596a30cc918fe2176f0da30df84651316ce452fae36cc940
4
- data.tar.gz: cc21e0f38a5508be8154621d7a68a247a2cc1a43d0fc9c3294176bb32ad079ca
3
+ metadata.gz: ed2853558a9d7a761617ee215d7f46c9b146cdb4fcb431564052f0330d84069a
4
+ data.tar.gz: ad54ea0f5958215ea2271f782354539cf74907af3a53bb16cc6ce7412007eceb
5
5
  SHA512:
6
- metadata.gz: ef97f505b367ef51efe01877912697208f42be67e5d531515c176aa9b2e6c14a0c795a5707b075d3a9f4b4051a96257220c905c0437bfc64f7e99a01deb942aa
7
- data.tar.gz: 5162367688b0581d28c63219f6a38953eff903fcbe9473b22001edb8670ad1f945c89308f9833d7684d7b331457dc35d25e3ff6cdb4cef4d34defccee1d0ef5e
6
+ metadata.gz: e38ef5d999c9a01a32148cc6f5709fac2b6d19430ab345823dfa7896ec37a6349f759552623ae47e6c12d61dabddde71cc9647d1ffd3213d2457bacbdff90363
7
+ data.tar.gz: ae263df3f94d33ae886e4d1966fc06265349839f05b9fd8538e48f81e3b7d9cb4ab7c84ae0c8299567f031480d0d53c03d645e2e97351fc7f41b64c3ad14de58
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_tracer (0.1.0)
4
+ ruby_tracer (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -24,6 +24,46 @@ $ gem install ruby_tracer
24
24
 
25
25
  ## Usage
26
26
 
27
+ ```rb
28
+ Tracer.trace(object) { ... } # trace object's activities in the given block
29
+ Tracer.trace_call { ... } # trace method calls in the given block
30
+ Tracer.trace_exception { ... } # trace exceptions in the given block
31
+ ```
32
+
33
+ **Example**
34
+
35
+ ```rb
36
+ require "ruby_tracer"
37
+
38
+ obj = Object.new
39
+
40
+ def obj.foo
41
+ 100
42
+ end
43
+
44
+ def bar(obj)
45
+ obj.foo
46
+ end
47
+
48
+ Tracer.trace(obj) { bar(obj) }
49
+ #depth:1 #<Object:0x000000010903c190> is used as a parameter obj of Object#bar at test.rb:13:in `block in <main>'
50
+ #depth:2 #<Object:0x000000010903c190> receives .foo at test.rb:10:in `bar'
51
+ ```
52
+
53
+ ### `ruby_tracer/helper`
54
+
55
+ If you want to avoid the `Tracer` namespace, you can do `require "ruby_tracer/helper"` instead:
56
+
57
+ ```rb
58
+ require "ruby_tracer/helper"
59
+
60
+ trace(object) { ... } # trace object's activities in the given block
61
+ trace_call { ... } # trace method calls in the given block
62
+ trace_exception { ... } # trace exceptions in the given block
63
+ ```
64
+
65
+ If you want to have more control over individual traces, you can use individual tracer classes:
66
+
27
67
  ### ObjectTracer
28
68
 
29
69
  ```rb
@@ -120,6 +160,10 @@ end
120
160
  #depth:5 at test.rb:8
121
161
  ```
122
162
 
163
+ ## Customization
164
+
165
+ TBD
166
+
123
167
  ## Acknowledgement
124
168
 
125
169
  [@ko1](https://github.com/ko1) (Koichi Sasada) implemented the majority of [`ruby/debug`](https://github.com/ruby/debug), including its tracers. So this project wouldn't exist without his amazing work there.
@@ -70,10 +70,11 @@ module Tracer
70
70
  end
71
71
  end
72
72
 
73
- def initialize(output: STDOUT, pattern: nil, colorize: nil)
73
+ def initialize(output: STDOUT, pattern: nil, colorize: nil, depth_offset: 0)
74
74
  @name = self.class.name
75
75
  @type = @name.sub(/Tracer\z/, "")
76
76
  @output = output
77
+ @depth_offset = depth_offset
77
78
  @colorize = colorize || colorizable?
78
79
 
79
80
  if pattern
@@ -7,22 +7,30 @@ class CallTracer < Tracer::Base
7
7
  TracePoint.new(:a_call, :a_return) do |tp|
8
8
  next if skip?(tp)
9
9
 
10
+ location = caller_locations(2, 1).first
11
+ next if location.to_s.match?(/<internal:/)
12
+
10
13
  depth = caller.size
11
14
 
12
15
  call_identifier_str = (tp.defined_class ? minfo(tp) : "block")
13
-
14
16
  call_identifier_str = colorize_blue(call_identifier_str)
15
17
 
16
18
  case tp.event
17
19
  when :call, :c_call, :b_call
18
20
  depth += 1 if tp.event == :c_call
19
21
  sp = " " * depth
20
- out tp, ">#{sp}#{call_identifier_str}", depth: depth
22
+ out tp,
23
+ ">#{sp}#{call_identifier_str}",
24
+ depth: depth - 2 - @depth_offset,
25
+ location: location
21
26
  when :return, :c_return, :b_return
22
27
  depth += 1 if tp.event == :c_return
23
28
  sp = " " * depth
24
29
  return_str = colorize_magenta(safe_inspect(tp.return_value))
25
- out tp, "<#{sp}#{call_identifier_str} #=> #{return_str}", depth: depth
30
+ out tp,
31
+ "<#{sp}#{call_identifier_str} #=> #{return_str}",
32
+ depth: depth - 2 - @depth_offset,
33
+ location: location
26
34
  end
27
35
  end
28
36
  end
@@ -9,7 +9,9 @@ class ExceptionTracer < Tracer::Base
9
9
 
10
10
  exc = tp.raised_exception
11
11
 
12
- out tp, " #{colorize_magenta(exc.inspect)}"
12
+ out tp,
13
+ " #{colorize_magenta(exc.inspect)}",
14
+ depth: caller.size - (1 + @depth_offset)
13
15
  rescue Exception => e
14
16
  p e
15
17
  end
@@ -0,0 +1,3 @@
1
+ require "ruby_tracer"
2
+
3
+ Object.include(Tracer::Helper)
@@ -65,7 +65,7 @@ class ObjectTracer < Tracer::Base
65
65
  out tp,
66
66
  " #{colorized_target_label} receives #{colorize_blue(method_info)}",
67
67
  location: caller_locations(internal_depth, 1).first,
68
- depth: caller.size - internal_depth
68
+ depth: caller.size - internal_depth - @depth_offset
69
69
  elsif !tp.parameters.empty?
70
70
  b = tp.binding
71
71
  method_info = colorize_blue(minfo(tp))
@@ -82,7 +82,7 @@ class ObjectTracer < Tracer::Base
82
82
  out tp,
83
83
  " #{colorized_target_label} is used as a parameter #{colorized_name} of #{method_info}",
84
84
  location: caller_locations(internal_depth, 1).first,
85
- depth: caller.size - internal_depth
85
+ depth: caller.size - internal_depth - @depth_offset
86
86
  end
87
87
  when :rest
88
88
  next if name == :"*"
@@ -94,7 +94,7 @@ class ObjectTracer < Tracer::Base
94
94
  out tp,
95
95
  " #{colorized_target_label} is used as a parameter in #{colorized_name} of #{method_info}",
96
96
  location: caller_locations(internal_depth, 1).first,
97
- depth: caller.size - internal_depth
97
+ depth: caller.size - internal_depth - @depth_offset
98
98
  end
99
99
  end
100
100
  when :keyrest
@@ -106,7 +106,7 @@ class ObjectTracer < Tracer::Base
106
106
  out tp,
107
107
  " #{colorized_target_label} is used as a parameter in #{colorized_name} of #{method_info}",
108
108
  location: caller_locations(internal_depth, 1).first,
109
- depth: caller.size - internal_depth
109
+ depth: caller.size - internal_depth - @depth_offset
110
110
  end
111
111
  end
112
112
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tracer
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/ruby_tracer.rb CHANGED
@@ -5,3 +5,26 @@ require_relative "ruby_tracer/line_tracer"
5
5
  require_relative "ruby_tracer/call_tracer"
6
6
  require_relative "ruby_tracer/exception_tracer"
7
7
  require_relative "ruby_tracer/object_tracer"
8
+
9
+ module Tracer
10
+ module Helper
11
+ DEPTH_OFFSET = 3
12
+
13
+ def trace_exception(&block)
14
+ tracer = ExceptionTracer.new(depth_offset: DEPTH_OFFSET)
15
+ tracer.start(&block)
16
+ end
17
+
18
+ def trace_call(&block)
19
+ tracer = CallTracer.new(depth_offset: DEPTH_OFFSET)
20
+ tracer.start(&block)
21
+ end
22
+
23
+ def trace(target, &block)
24
+ tracer = ObjectTracer.new(target, depth_offset: DEPTH_OFFSET)
25
+ tracer.start(&block)
26
+ end
27
+ end
28
+
29
+ extend Helper
30
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stan Lo
@@ -29,6 +29,7 @@ files:
29
29
  - lib/ruby_tracer/call_tracer.rb
30
30
  - lib/ruby_tracer/color.rb
31
31
  - lib/ruby_tracer/exception_tracer.rb
32
+ - lib/ruby_tracer/helper.rb
32
33
  - lib/ruby_tracer/line_tracer.rb
33
34
  - lib/ruby_tracer/object_tracer.rb
34
35
  - lib/ruby_tracer/version.rb