markdown_logging_proxy 1.1.1 → 1.3.1

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: aff578739daa97caef0a7014bf062fe113a495cc124da496f6a08d8c31790e49
4
- data.tar.gz: 560c9efb88be9083d5d91b46a174c8b5c26c5c0b30289776c80f937b9ee81b1b
3
+ metadata.gz: e893add5ca96b98f32be5370fb7bfc61e3f80f387392359b4c8e25f9d7e1ca45
4
+ data.tar.gz: 37dd7b894552ba38329279dcb2c8ef23961ca719c23ba1f7a5343e0e1c365c89
5
5
  SHA512:
6
- metadata.gz: aa0ff0ff2637a3bd8eb9141cd6200e763478d071859958a07ca6585a2405fcce83160a9b03c49a88afa7f11428d3ed50bc4b3cde944bf96060c820b253406d20
7
- data.tar.gz: 45f2d7f82f0e7d366857b8da7ab7a6397e582190969155013facd87a5ae66c7c441f006486fee4cfe9b388538d614eaa60c533f478ec4a7165f9038031e31dcb
6
+ metadata.gz: fd08c3cecf05ef6173ae54c8cd9885e2817c94c141fd3c168c1a04f79f9c267e9fbaebb164cf00ba6670125f4dafd0135a47d39b758b11b043feabf851bf9e8d
7
+ data.tar.gz: '096efff296da1406507b5f2a5531bbf267324a4b53c27ce7ced1b05f85e59b223480e5cb21838b658937a00093cad8e2d961f1c6727a48e85bf3ecf06a9797bb'
data/CHANGELOG.md CHANGED
@@ -29,3 +29,16 @@
29
29
 
30
30
  - Allow proxying of `nil`
31
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
40
+
41
+ ## [1.3.1]
42
+
43
+ - Object inspection improvements and fixes
44
+ - Implement `inspect_method: :limited` 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.1)
4
+ markdown_logging_proxy (1.3.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -4,20 +4,6 @@ require 'pp'
4
4
  require 'time'
5
5
  module MarkdownLoggingProxy
6
6
  class MarkdownLogger
7
- def self.inspect_object(object, show_id = true)
8
- [
9
- '```ruby',
10
- ("# #{id_object(object)}" if show_id),
11
- object.pretty_inspect.chomp,
12
- '```'
13
- ].compact.join("\n")
14
- end
15
-
16
- def self.id_object(object)
17
- # #<Object:0x00007f5a0919e140>
18
- "`#<#{object.class}:0x#{object.object_id.to_s(16)}>`"
19
- end
20
-
21
7
  def self.build(location, **options)
22
8
  return location if location.is_a?(MarkdownLogger)
23
9
  new(location, **options)
@@ -86,19 +72,15 @@ module MarkdownLoggingProxy
86
72
  end
87
73
  module MarkdownLoggingProxy
88
74
  class Proxy
89
- # Object methods that should be proxied but won't hit method_missing
90
- DEFAULT_OVERWRITES = %i[
91
- ! != !~ <=> == === =~
92
- clone display dup enum_for eql? equal? freeze frozen? hash inspect
93
- is_a? itself kind_of? nil? taint tainted? tap then to_enum to_s
94
- trust untaint unstrust untrusted? yield_self
95
- ]
75
+ DO_NOT_OVERWRITE = %i[__binding__ __id__ __send__ class extend]
76
+ DEFAULT_OVERWRITES = Object.new.methods - DO_NOT_OVERWRITE
96
77
 
97
78
  def initialize(
98
79
  to_proxy = nil,
99
80
  target: nil,
100
81
  location: STDOUT,
101
82
  backtrace: true, # regex/true/false backtrace control
83
+ inspect_method: :pretty_inspect,
102
84
  ignore: [], # methods we shouldn't log/proxy
103
85
  proxy_response: [], # methods we should return a proxy for
104
86
  overwrite: DEFAULT_OVERWRITES
@@ -109,6 +91,7 @@ module MarkdownLoggingProxy
109
91
  target: @target,
110
92
  proxy: self,
111
93
  logger: @logger,
94
+ inspect_method: inspect_method,
112
95
  ignore: ignore,
113
96
  proxy_response: proxy_response,
114
97
  proxy_options: {
@@ -130,18 +113,20 @@ module MarkdownLoggingProxy
130
113
  end
131
114
  module MarkdownLoggingProxy
132
115
  class Tracer
133
- attr_reader :target, :logger, :ignore, :proxy
116
+ attr_reader :target, :logger, :ignore, :proxy, :inspect_method
134
117
 
135
118
  def initialize(
136
119
  target:,
137
120
  proxy:,
138
121
  logger: nil,
122
+ inspect_method: :pretty_inspect,
139
123
  ignore: [],
140
124
  proxy_response: [],
141
125
  proxy_options: {}
142
126
  )
143
127
  @target = target
144
128
  @logger = logger
129
+ @inspect_method = inspect_method
145
130
  @ignore = ignore
146
131
  @proxy_response = proxy_response
147
132
  @proxy_options = proxy_options
@@ -166,16 +151,64 @@ module MarkdownLoggingProxy
166
151
  @ignore.member?(meth)
167
152
  end
168
153
 
154
+ def inspect_object(obj, args: false)
155
+ obj_str =
156
+ case inspect_method
157
+ when :inspect then obj.inspect
158
+ when :limited then limited_inspect(obj)
159
+ when :id_object
160
+ if args
161
+ "[#{obj.map { |o| id_object(o) }.join(',')}]"
162
+ else
163
+ id_object(obj)
164
+ end
165
+ when :pretty_inpect
166
+ [obj.pretty_inspect.chomp].tap do |lines|
167
+ lines.prepend "# #{id_object(obj)}" unless args
168
+ end.join("\n")
169
+ else
170
+ obj.send(inspect_method)
171
+ end
172
+ ['```ruby', obj_str, '```'].join("\n")
173
+ end
174
+
175
+ # recursive
176
+ def limited_inspect(obj)
177
+ case obj
178
+ when Array
179
+ "[#{obj.map { |o| limited_inspect(o) }.join(',')}]"
180
+ when Hash
181
+ # assumes keys are safe to .inspect
182
+ insides = obj.each { |k, v| "#{k.inspect} => #{limited_inspect(v)}" }
183
+ "{#{insides.join(', ')}}"
184
+ when String, Symbol, Numeric, Class, true, false, nil
185
+ truncated_inspect(obj)
186
+ else
187
+ id_object(obj)
188
+ end
189
+ end
190
+
191
+ def id_object(object)
192
+ # #<Object:0xe140>
193
+ "#<#{object.class}:0x#{object.object_id.to_s(16)}>"
194
+ end
195
+
196
+ def truncated_inspect(obj, limit = 280)
197
+ obj_str = obj.inspect
198
+ obj_str = "#{obj_str.first(limit)}..." if obj_str.length > limit
199
+ obj_str
200
+ end
201
+
169
202
  private
170
203
 
171
204
  def log_call_signature(meth, args, &blk)
172
205
  return if ignore.member?(meth)
173
206
  logger.log :info, 1, <<~MSG.chomp
174
- Calling `#{meth}` on #{MarkdownLogger.id_object(target)}
207
+ Calling `#{meth}` on `#{id_object(target)}`
175
208
 
176
209
  Arguments:
177
210
 
178
- #{MarkdownLogger.inspect_object(args, false)}
211
+ #{inspect_object(args, args: true)}
179
212
 
180
213
  Block given? #{block_given? ? 'Yes' : 'No'}
181
214
  #{logger.inspect_backtrace}
@@ -189,15 +222,16 @@ module MarkdownLoggingProxy
189
222
  logger.log :info, 3, <<~MSG.chomp
190
223
  Returning proxied response to `#{meth}`
191
224
 
192
- Proxy from `#{meth}` on #{MarkdownLogger.id_object(target)}
225
+ Proxy from `#{meth}` on `#{id_object(target)}`
193
226
 
194
227
  Proxy for:
195
228
 
196
- #{MarkdownLogger.inspect_object(response)}
229
+ #{inspect_object(response)}
197
230
  MSG
198
231
  Proxy.new(**@proxy_options.merge(
199
232
  target: response,
200
233
  location: logger,
234
+ inspect_method: inspect_method,
201
235
  proxy_response: @proxy_response,
202
236
  ignore: @ignore,
203
237
  ))
@@ -205,19 +239,20 @@ module MarkdownLoggingProxy
205
239
 
206
240
  def log_and_proxy_block(meth, blk)
207
241
  return if blk.nil?
242
+ tracer = self
208
243
  proc do |*args|
209
- logger.log :info, 2, <<~MSG.chomp
210
- Yield to block in `#{meth}` on #{MarkdownLogger.id_object(target)}
244
+ tracer.logger.log :info, 2, <<~MSG.chomp
245
+ Yield to block in `#{meth}` on `#{tracer.id_object(tracer.target)}`
211
246
 
212
247
  Arguments:
213
248
 
214
- #{MarkdownLogger.inspect_object(args, false)}
249
+ #{tracer.inspect_object(args, args: true)}
215
250
  MSG
216
- blk.call(*args).tap do |response|
217
- logger.log :info, 3, <<~MSG.chomp
251
+ instance_exec(*args, &blk).tap do |response|
252
+ tracer.logger.log :info, 3, <<~MSG.chomp
218
253
  Response from block in `#{meth}`
219
254
 
220
- #{MarkdownLogger.inspect_object(response)}
255
+ #{tracer.inspect_object(response)}
221
256
  MSG
222
257
  end
223
258
  end
@@ -229,7 +264,7 @@ module MarkdownLoggingProxy
229
264
 
230
265
  Type: #{error.class}
231
266
 
232
- #{MarkdownLogger.inspect_object(error)}
267
+ #{inspect_object(error)}
233
268
  MSG
234
269
  raise error
235
270
  end
@@ -239,7 +274,7 @@ module MarkdownLoggingProxy
239
274
  logger.log :info, 2, <<~MSG.chomp
240
275
  `#{meth}` response
241
276
 
242
- #{MarkdownLogger.inspect_object(response)}
277
+ #{inspect_object(response)}
243
278
  MSG
244
279
  end
245
280
  end
@@ -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,18 +1,14 @@
1
1
  module MarkdownLoggingProxy
2
2
  class Proxy
3
- # Object methods that should be proxied but won't hit method_missing
4
- DEFAULT_OVERWRITES = %i[
5
- ! != !~ <=> == === =~
6
- clone display dup enum_for eql? equal? freeze frozen? hash inspect
7
- is_a? itself kind_of? nil? taint tainted? tap then to_enum to_s
8
- trust untaint unstrust untrusted? yield_self
9
- ]
3
+ DO_NOT_OVERWRITE = %i[__binding__ __id__ __send__ class extend]
4
+ DEFAULT_OVERWRITES = Object.new.methods - DO_NOT_OVERWRITE
10
5
 
11
6
  def initialize(
12
7
  to_proxy = nil,
13
8
  target: nil,
14
9
  location: STDOUT,
15
10
  backtrace: true, # regex/true/false backtrace control
11
+ inspect_method: :pretty_inspect,
16
12
  ignore: [], # methods we shouldn't log/proxy
17
13
  proxy_response: [], # methods we should return a proxy for
18
14
  overwrite: DEFAULT_OVERWRITES
@@ -23,6 +19,7 @@ module MarkdownLoggingProxy
23
19
  target: @target,
24
20
  proxy: self,
25
21
  logger: @logger,
22
+ inspect_method: inspect_method,
26
23
  ignore: ignore,
27
24
  proxy_response: proxy_response,
28
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,64 @@ module MarkdownLoggingProxy
36
38
  @ignore.member?(meth)
37
39
  end
38
40
 
41
+ def inspect_object(obj, args: false)
42
+ obj_str =
43
+ case inspect_method
44
+ when :inspect then obj.inspect
45
+ when :limited then limited_inspect(obj)
46
+ when :id_object
47
+ if args
48
+ "[#{obj.map { |o| id_object(o) }.join(',')}]"
49
+ else
50
+ id_object(obj)
51
+ end
52
+ when :pretty_inpect
53
+ [obj.pretty_inspect.chomp].tap do |lines|
54
+ lines.prepend "# #{id_object(obj)}" unless args
55
+ end.join("\n")
56
+ else
57
+ obj.send(inspect_method)
58
+ end
59
+ ['```ruby', obj_str, '```'].join("\n")
60
+ end
61
+
62
+ # recursive
63
+ def limited_inspect(obj)
64
+ case obj
65
+ when Array
66
+ "[#{obj.map { |o| limited_inspect(o) }.join(',')}]"
67
+ when Hash
68
+ # assumes keys are safe to .inspect
69
+ insides = obj.each { |k, v| "#{k.inspect} => #{limited_inspect(v)}" }
70
+ "{#{insides.join(', ')}}"
71
+ when String, Symbol, Numeric, Class, true, false, nil
72
+ truncated_inspect(obj)
73
+ else
74
+ id_object(obj)
75
+ end
76
+ end
77
+
78
+ def id_object(object)
79
+ # #<Object:0xe140>
80
+ "#<#{object.class}:0x#{object.object_id.to_s(16)}>"
81
+ end
82
+
83
+ def truncated_inspect(obj, limit = 280)
84
+ obj_str = obj.inspect
85
+ obj_str = "#{obj_str.first(limit)}..." if obj_str.length > limit
86
+ obj_str
87
+ end
88
+
39
89
  private
40
90
 
41
91
  def log_call_signature(meth, args, &blk)
42
92
  return if ignore.member?(meth)
43
93
  logger.log :info, 1, <<~MSG.chomp
44
- Calling `#{meth}` on #{MarkdownLogger.id_object(target)}
94
+ Calling `#{meth}` on `#{id_object(target)}`
45
95
 
46
96
  Arguments:
47
97
 
48
- #{MarkdownLogger.inspect_object(args, false)}
98
+ #{inspect_object(args, args: true)}
49
99
 
50
100
  Block given? #{block_given? ? 'Yes' : 'No'}
51
101
  #{logger.inspect_backtrace}
@@ -59,15 +109,16 @@ module MarkdownLoggingProxy
59
109
  logger.log :info, 3, <<~MSG.chomp
60
110
  Returning proxied response to `#{meth}`
61
111
 
62
- Proxy from `#{meth}` on #{MarkdownLogger.id_object(target)}
112
+ Proxy from `#{meth}` on `#{id_object(target)}`
63
113
 
64
114
  Proxy for:
65
115
 
66
- #{MarkdownLogger.inspect_object(response)}
116
+ #{inspect_object(response)}
67
117
  MSG
68
118
  Proxy.new(**@proxy_options.merge(
69
119
  target: response,
70
120
  location: logger,
121
+ inspect_method: inspect_method,
71
122
  proxy_response: @proxy_response,
72
123
  ignore: @ignore,
73
124
  ))
@@ -75,19 +126,20 @@ module MarkdownLoggingProxy
75
126
 
76
127
  def log_and_proxy_block(meth, blk)
77
128
  return if blk.nil?
129
+ tracer = self
78
130
  proc do |*args|
79
- logger.log :info, 2, <<~MSG.chomp
80
- Yield to block in `#{meth}` on #{MarkdownLogger.id_object(target)}
131
+ tracer.logger.log :info, 2, <<~MSG.chomp
132
+ Yield to block in `#{meth}` on `#{tracer.id_object(tracer.target)}`
81
133
 
82
134
  Arguments:
83
135
 
84
- #{MarkdownLogger.inspect_object(args, false)}
136
+ #{tracer.inspect_object(args, args: true)}
85
137
  MSG
86
- blk.call(*args).tap do |response|
87
- logger.log :info, 3, <<~MSG.chomp
138
+ instance_exec(*args, &blk).tap do |response|
139
+ tracer.logger.log :info, 3, <<~MSG.chomp
88
140
  Response from block in `#{meth}`
89
141
 
90
- #{MarkdownLogger.inspect_object(response)}
142
+ #{tracer.inspect_object(response)}
91
143
  MSG
92
144
  end
93
145
  end
@@ -99,7 +151,7 @@ module MarkdownLoggingProxy
99
151
 
100
152
  Type: #{error.class}
101
153
 
102
- #{MarkdownLogger.inspect_object(error)}
154
+ #{inspect_object(error)}
103
155
  MSG
104
156
  raise error
105
157
  end
@@ -109,7 +161,7 @@ module MarkdownLoggingProxy
109
161
  logger.log :info, 2, <<~MSG.chomp
110
162
  `#{meth}` response
111
163
 
112
- #{MarkdownLogger.inspect_object(response)}
164
+ #{inspect_object(response)}
113
165
  MSG
114
166
  end
115
167
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "markdown_logging_proxy"
3
- spec.version = "1.1.1"
3
+ spec.version = "1.3.1"
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.1
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Zulauf