markdown_logging_proxy 1.1.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b7fa845a8bab9f3a70e7362c0910ff5b24c6bc95d5267e82e8d4f87f90fa2e3
4
- data.tar.gz: 0c26ee702af858bc046077ebf4209f473c50e66ee14e03f45da161f1990b510a
3
+ metadata.gz: 215b4bc73f85cdb7ec9bfa2e3ef3b1efefa55a9d99c34f36007788c798dcbcc0
4
+ data.tar.gz: 391123e09db062038d9e4e4ef3aa2a98f15dc07a7727ede676d6a51d7c5cf532
5
5
  SHA512:
6
- metadata.gz: 6ac27cf32075e6046405f21bec1d4bae709c9a3e91b40b461a53b8a5794ff25165efe4295183a9a0f58a1f8686a15d08b0424e79f87355887967338c85e00526
7
- data.tar.gz: 927d9792258911a98117c6915288b13bb05a6596257a38504adf24d1439aa972e71868c8895ab1cd0d5afbb8e4910e463948bb0bd11cbf0e29544fc66b04685d
6
+ metadata.gz: 3119ffa9798cf92d8010df9d20738eaf1c7e782de72b40709aba08066270811b31b119ada4ba977e4af67fc5f37da6d455a01afd81ab0a8a62fe944587323c58
7
+ data.tar.gz: 6171e82b3677b73c9fa83753a9a21927625427a344b77e07f6285f5090bc1eff156c51504f74310a33bf7fef1bd36bfab4c191423369715b0427d7018b0a475e
data/CHANGELOG.md CHANGED
@@ -24,3 +24,16 @@
24
24
 
25
25
  - Don't show object IDs for args arrays. It's confusing.
26
26
  - BREAKING: Proxies most `Object` methods by default now
27
+
28
+ ## [1.1.1]
29
+
30
+ - Allow proxying of `nil`
31
+ - Add require statements to copy+paste version
32
+
33
+ ## [1.2.0]
34
+
35
+ - Overwrite as many `Object` methods as we can by default
36
+
37
+ ## [1.3.0]
38
+
39
+ - Add `inspect_method` configuration option
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- markdown_logging_proxy (1.1.0)
4
+ markdown_logging_proxy (1.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -18,7 +18,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
18
18
 
19
19
  ### Copy+Paste
20
20
 
21
- Look at `dist/` for compiled versions of the gem which can safely (relatively) be copied into irb/pry sessions.
21
+ You can copy the contents of [`dist/markdown_logging_proxy.rb`](dist/markdown_logging_proxy.rb) into a live irb/pry session.
22
22
 
23
23
  ## Usage
24
24
 
@@ -1,19 +1,9 @@
1
+ require 'logger'
2
+ require 'securerandom'
3
+ require 'pp'
4
+ require 'time'
1
5
  module MarkdownLoggingProxy
2
6
  class MarkdownLogger
3
- def self.inspect_object(object, show_id = true)
4
- [
5
- '```ruby',
6
- ("# #{id_object(object)}" if show_id),
7
- object.pretty_inspect.chomp,
8
- '```'
9
- ].compact.join("\n")
10
- end
11
-
12
- def self.id_object(object)
13
- # #<Object:0x00007f5a0919e140>
14
- "`#<#{object.class}:0x#{object.object_id.to_s(16)}>`"
15
- end
16
-
17
7
  def self.build(location, **options)
18
8
  return location if location.is_a?(MarkdownLogger)
19
9
  new(location, **options)
@@ -81,38 +71,27 @@ module MarkdownLoggingProxy
81
71
  end
82
72
  end
83
73
  module MarkdownLoggingProxy
84
- # frozen_string_literal: true
85
-
86
- require 'logger'
87
- require 'securerandom'
88
- require 'pp'
89
- require 'time'
90
-
91
74
  class Proxy
92
- # Object methods that should be proxied but won't hit method_missing
93
- DEFAULT_OVERWRITES = %i[
94
- ! != !~ <=> == === =~
95
- clone display dup enum_for eql? equal? freeze frozen? hash inspect
96
- is_a? itself kind_of? nil? taint tainted? tap then to_enum to_s
97
- trust untaint unstrust untrusted? yield_self
98
- ]
75
+ DO_NOT_OVERWRITE = %i[__binding__ __id__ __send__ class extend]
76
+ DEFAULT_OVERWRITES = Object.new.methods - DO_NOT_OVERWRITE
99
77
 
100
78
  def initialize(
101
79
  to_proxy = nil,
102
80
  target: nil,
103
81
  location: STDOUT,
104
82
  backtrace: true, # regex/true/false backtrace control
83
+ inspect_method: :pretty_inspect,
105
84
  ignore: [], # methods we shouldn't log/proxy
106
85
  proxy_response: [], # methods we should return a proxy for
107
86
  overwrite: DEFAULT_OVERWRITES
108
87
  )
109
88
  @target = to_proxy || target
110
- raise ArgumentError, "Missing required proxy target" unless @target
111
89
  @logger = MarkdownLogger.build(location, backtrace: backtrace)
112
90
  @tracer = Tracer.new(
113
91
  target: @target,
114
92
  proxy: self,
115
93
  logger: @logger,
94
+ inspect_method: inspect_method,
116
95
  ignore: ignore,
117
96
  proxy_response: proxy_response,
118
97
  proxy_options: {
@@ -134,18 +113,20 @@ module MarkdownLoggingProxy
134
113
  end
135
114
  module MarkdownLoggingProxy
136
115
  class Tracer
137
- attr_reader :target, :logger, :ignore, :proxy
116
+ attr_reader :target, :logger, :ignore, :proxy, :inspect_method
138
117
 
139
118
  def initialize(
140
119
  target:,
141
120
  proxy:,
142
121
  logger: nil,
122
+ inspect_method: :pretty_inspect,
143
123
  ignore: [],
144
124
  proxy_response: [],
145
125
  proxy_options: {}
146
126
  )
147
127
  @target = target
148
128
  @logger = logger
129
+ @inspect_method = inspect_method
149
130
  @ignore = ignore
150
131
  @proxy_response = proxy_response
151
132
  @proxy_options = proxy_options
@@ -170,16 +151,36 @@ module MarkdownLoggingProxy
170
151
  @ignore.member?(meth)
171
152
  end
172
153
 
154
+ def inspect_object(obj, show_id: true)
155
+ obj_str =
156
+ case inspect_method
157
+ when :inspect then obj.inspect
158
+ when :object_id then object_id(obj)
159
+ when :pretty_inpect
160
+ [obj.pretty_inspect.chomp].tap do |lines|
161
+ lines.prepend "# #{object_id(obj)}" if show_id
162
+ end.join("\n")
163
+ else
164
+ obj.send(inspect_method)
165
+ end
166
+ ['```ruby', obj_str, '```'].join("\n")
167
+ end
168
+
169
+ def id_object(object)
170
+ # #<Object:0xe140>
171
+ "`#<#{object.class}:0x#{object.object_id.to_s(16)}>`"
172
+ end
173
+
173
174
  private
174
175
 
175
176
  def log_call_signature(meth, args, &blk)
176
177
  return if ignore.member?(meth)
177
178
  logger.log :info, 1, <<~MSG.chomp
178
- Calling `#{meth}` on #{MarkdownLogger.id_object(target)}
179
+ Calling `#{meth}` on #{id_object(target)}
179
180
 
180
181
  Arguments:
181
182
 
182
- #{MarkdownLogger.inspect_object(args, false)}
183
+ #{inspect_object(args, show_id: false)}
183
184
 
184
185
  Block given? #{block_given? ? 'Yes' : 'No'}
185
186
  #{logger.inspect_backtrace}
@@ -193,15 +194,16 @@ module MarkdownLoggingProxy
193
194
  logger.log :info, 3, <<~MSG.chomp
194
195
  Returning proxied response to `#{meth}`
195
196
 
196
- Proxy from `#{meth}` on #{MarkdownLogger.id_object(target)}
197
+ Proxy from `#{meth}` on #{id_object(target)}
197
198
 
198
199
  Proxy for:
199
200
 
200
- #{MarkdownLogger.inspect_object(response)}
201
+ #{inspect_object(response)}
201
202
  MSG
202
203
  Proxy.new(**@proxy_options.merge(
203
204
  target: response,
204
205
  location: logger,
206
+ inspect_method: inspect_method,
205
207
  proxy_response: @proxy_response,
206
208
  ignore: @ignore,
207
209
  ))
@@ -209,19 +211,20 @@ module MarkdownLoggingProxy
209
211
 
210
212
  def log_and_proxy_block(meth, blk)
211
213
  return if blk.nil?
214
+ tracer = self
212
215
  proc do |*args|
213
- logger.log :info, 2, <<~MSG.chomp
214
- Yield to block in `#{meth}` on #{MarkdownLogger.id_object(target)}
216
+ tracer.logger.log :info, 2, <<~MSG.chomp
217
+ Yield to block in `#{meth}` on #{tracer.id_object(tracer.target)}
215
218
 
216
219
  Arguments:
217
220
 
218
- #{MarkdownLogger.inspect_object(args, false)}
221
+ #{tracer.inspect_object(args, show_id: false)}
219
222
  MSG
220
- blk.call(*args).tap do |response|
221
- logger.log :info, 3, <<~MSG.chomp
223
+ instance_exec(*args, &blk).tap do |response|
224
+ tracer.logger.log :info, 3, <<~MSG.chomp
222
225
  Response from block in `#{meth}`
223
226
 
224
- #{MarkdownLogger.inspect_object(response)}
227
+ #{tracer.inspect_object(response)}
225
228
  MSG
226
229
  end
227
230
  end
@@ -233,7 +236,7 @@ module MarkdownLoggingProxy
233
236
 
234
237
  Type: #{error.class}
235
238
 
236
- #{MarkdownLogger.inspect_object(error)}
239
+ #{inspect_object(error)}
237
240
  MSG
238
241
  raise error
239
242
  end
@@ -243,8 +246,9 @@ module MarkdownLoggingProxy
243
246
  logger.log :info, 2, <<~MSG.chomp
244
247
  `#{meth}` response
245
248
 
246
- #{MarkdownLogger.inspect_object(response)}
249
+ #{inspect_object(response)}
247
250
  MSG
248
251
  end
249
252
  end
250
253
  end
254
+
@@ -1,19 +1,5 @@
1
1
  module MarkdownLoggingProxy
2
2
  class MarkdownLogger
3
- def self.inspect_object(object, show_id = true)
4
- [
5
- '```ruby',
6
- ("# #{id_object(object)}" if show_id),
7
- object.pretty_inspect.chomp,
8
- '```'
9
- ].compact.join("\n")
10
- end
11
-
12
- def self.id_object(object)
13
- # #<Object:0x00007f5a0919e140>
14
- "`#<#{object.class}:0x#{object.object_id.to_s(16)}>`"
15
- end
16
-
17
3
  def self.build(location, **options)
18
4
  return location if location.is_a?(MarkdownLogger)
19
5
  new(location, **options)
@@ -1,36 +1,25 @@
1
1
  module MarkdownLoggingProxy
2
- # frozen_string_literal: true
3
-
4
- require 'logger'
5
- require 'securerandom'
6
- require 'pp'
7
- require 'time'
8
-
9
2
  class Proxy
10
- # Object methods that should be proxied but won't hit method_missing
11
- DEFAULT_OVERWRITES = %i[
12
- ! != !~ <=> == === =~
13
- clone display dup enum_for eql? equal? freeze frozen? hash inspect
14
- is_a? itself kind_of? nil? taint tainted? tap then to_enum to_s
15
- trust untaint unstrust untrusted? yield_self
16
- ]
3
+ DO_NOT_OVERWRITE = %i[__binding__ __id__ __send__ class extend]
4
+ DEFAULT_OVERWRITES = Object.new.methods - DO_NOT_OVERWRITE
17
5
 
18
6
  def initialize(
19
7
  to_proxy = nil,
20
8
  target: nil,
21
9
  location: STDOUT,
22
10
  backtrace: true, # regex/true/false backtrace control
11
+ inspect_method: :pretty_inspect,
23
12
  ignore: [], # methods we shouldn't log/proxy
24
13
  proxy_response: [], # methods we should return a proxy for
25
14
  overwrite: DEFAULT_OVERWRITES
26
15
  )
27
16
  @target = to_proxy || target
28
- raise ArgumentError, "Missing required proxy target" unless @target
29
17
  @logger = MarkdownLogger.build(location, backtrace: backtrace)
30
18
  @tracer = Tracer.new(
31
19
  target: @target,
32
20
  proxy: self,
33
21
  logger: @logger,
22
+ inspect_method: inspect_method,
34
23
  ignore: ignore,
35
24
  proxy_response: proxy_response,
36
25
  proxy_options: {
@@ -1,17 +1,19 @@
1
1
  module MarkdownLoggingProxy
2
2
  class Tracer
3
- attr_reader :target, :logger, :ignore, :proxy
3
+ attr_reader :target, :logger, :ignore, :proxy, :inspect_method
4
4
 
5
5
  def initialize(
6
6
  target:,
7
7
  proxy:,
8
8
  logger: nil,
9
+ inspect_method: :pretty_inspect,
9
10
  ignore: [],
10
11
  proxy_response: [],
11
12
  proxy_options: {}
12
13
  )
13
14
  @target = target
14
15
  @logger = logger
16
+ @inspect_method = inspect_method
15
17
  @ignore = ignore
16
18
  @proxy_response = proxy_response
17
19
  @proxy_options = proxy_options
@@ -36,16 +38,36 @@ module MarkdownLoggingProxy
36
38
  @ignore.member?(meth)
37
39
  end
38
40
 
41
+ def inspect_object(obj, show_id: true)
42
+ obj_str =
43
+ case inspect_method
44
+ when :inspect then obj.inspect
45
+ when :object_id then object_id(obj)
46
+ when :pretty_inpect
47
+ [obj.pretty_inspect.chomp].tap do |lines|
48
+ lines.prepend "# #{object_id(obj)}" if show_id
49
+ end.join("\n")
50
+ else
51
+ obj.send(inspect_method)
52
+ end
53
+ ['```ruby', obj_str, '```'].join("\n")
54
+ end
55
+
56
+ def id_object(object)
57
+ # #<Object:0xe140>
58
+ "`#<#{object.class}:0x#{object.object_id.to_s(16)}>`"
59
+ end
60
+
39
61
  private
40
62
 
41
63
  def log_call_signature(meth, args, &blk)
42
64
  return if ignore.member?(meth)
43
65
  logger.log :info, 1, <<~MSG.chomp
44
- Calling `#{meth}` on #{MarkdownLogger.id_object(target)}
66
+ Calling `#{meth}` on #{id_object(target)}
45
67
 
46
68
  Arguments:
47
69
 
48
- #{MarkdownLogger.inspect_object(args, false)}
70
+ #{inspect_object(args, show_id: false)}
49
71
 
50
72
  Block given? #{block_given? ? 'Yes' : 'No'}
51
73
  #{logger.inspect_backtrace}
@@ -59,15 +81,16 @@ module MarkdownLoggingProxy
59
81
  logger.log :info, 3, <<~MSG.chomp
60
82
  Returning proxied response to `#{meth}`
61
83
 
62
- Proxy from `#{meth}` on #{MarkdownLogger.id_object(target)}
84
+ Proxy from `#{meth}` on #{id_object(target)}
63
85
 
64
86
  Proxy for:
65
87
 
66
- #{MarkdownLogger.inspect_object(response)}
88
+ #{inspect_object(response)}
67
89
  MSG
68
90
  Proxy.new(**@proxy_options.merge(
69
91
  target: response,
70
92
  location: logger,
93
+ inspect_method: inspect_method,
71
94
  proxy_response: @proxy_response,
72
95
  ignore: @ignore,
73
96
  ))
@@ -75,19 +98,20 @@ module MarkdownLoggingProxy
75
98
 
76
99
  def log_and_proxy_block(meth, blk)
77
100
  return if blk.nil?
101
+ tracer = self
78
102
  proc do |*args|
79
- logger.log :info, 2, <<~MSG.chomp
80
- Yield to block in `#{meth}` on #{MarkdownLogger.id_object(target)}
103
+ tracer.logger.log :info, 2, <<~MSG.chomp
104
+ Yield to block in `#{meth}` on #{tracer.id_object(tracer.target)}
81
105
 
82
106
  Arguments:
83
107
 
84
- #{MarkdownLogger.inspect_object(args, false)}
108
+ #{tracer.inspect_object(args, show_id: false)}
85
109
  MSG
86
- blk.call(*args).tap do |response|
87
- logger.log :info, 3, <<~MSG.chomp
110
+ instance_exec(*args, &blk).tap do |response|
111
+ tracer.logger.log :info, 3, <<~MSG.chomp
88
112
  Response from block in `#{meth}`
89
113
 
90
- #{MarkdownLogger.inspect_object(response)}
114
+ #{tracer.inspect_object(response)}
91
115
  MSG
92
116
  end
93
117
  end
@@ -99,7 +123,7 @@ module MarkdownLoggingProxy
99
123
 
100
124
  Type: #{error.class}
101
125
 
102
- #{MarkdownLogger.inspect_object(error)}
126
+ #{inspect_object(error)}
103
127
  MSG
104
128
  raise error
105
129
  end
@@ -109,7 +133,7 @@ module MarkdownLoggingProxy
109
133
  logger.log :info, 2, <<~MSG.chomp
110
134
  `#{meth}` response
111
135
 
112
- #{MarkdownLogger.inspect_object(response)}
136
+ #{inspect_object(response)}
113
137
  MSG
114
138
  end
115
139
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "markdown_logging_proxy"
3
- spec.version = "1.1.0"
3
+ spec.version = "1.3.0"
4
4
  spec.authors = ["Carl Zulauf"]
5
5
  spec.email = ["carl@linkleaf.com"]
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown_logging_proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Zulauf
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  requirements: []
57
- rubygems_version: 3.3.7
57
+ rubygems_version: 3.3.20
58
58
  signing_key:
59
59
  specification_version: 4
60
60
  summary: Proxy object for debugging