Class: MarkdownLoggingProxy::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_logging_proxy/tracer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, proxy:, logger: nil, inspect_method: :pretty_inspect, ignore: [], proxy_response: [], proxy_options: {}) ⇒ Tracer

Returns a new instance of Tracer.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/markdown_logging_proxy/tracer.rb', line 5

def initialize(
    target:,
    proxy:,
    logger: nil,
    inspect_method: :pretty_inspect,
    ignore: [],
    proxy_response: [],
    proxy_options: {}
  )
  @target = target
  @logger = logger
  @inspect_method = inspect_method
  @ignore = ignore
  @proxy_response = proxy_response
  @proxy_options = proxy_options
end

Instance Attribute Details

#ignoreObject (readonly)

Returns the value of attribute ignore.



3
4
5
# File 'lib/markdown_logging_proxy/tracer.rb', line 3

def ignore
  @ignore
end

#inspect_methodObject (readonly)

Returns the value of attribute inspect_method.



3
4
5
# File 'lib/markdown_logging_proxy/tracer.rb', line 3

def inspect_method
  @inspect_method
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/markdown_logging_proxy/tracer.rb', line 3

def logger
  @logger
end

#proxyObject (readonly)

Returns the value of attribute proxy.



3
4
5
# File 'lib/markdown_logging_proxy/tracer.rb', line 3

def proxy
  @proxy
end

#targetObject (readonly)

Returns the value of attribute target.



3
4
5
# File 'lib/markdown_logging_proxy/tracer.rb', line 3

def target
  @target
end

Instance Method Details

#id_object(object) ⇒ Object



78
79
80
81
# File 'lib/markdown_logging_proxy/tracer.rb', line 78

def id_object(object)
  # #<Object:0xe140>
  "#<#{object.class}:0x#{object.object_id.to_s(16)}>"
end

#ignore?(meth) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/markdown_logging_proxy/tracer.rb', line 37

def ignore?(meth)
  @ignore.member?(meth)
end

#inspect_object(obj, args: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/markdown_logging_proxy/tracer.rb', line 41

def inspect_object(obj, args: false)
  obj_str =
    case inspect_method
    when :inspect then obj.inspect
    when :limited then limited_inspect(obj)
    when :id_object
      if args
        "[#{obj.map { |o| id_object(o) }.join(',')}]"
      else
        id_object(obj)
      end
    when :pretty_inpect
      [obj.pretty_inspect.chomp].tap do |lines|
        lines.prepend "# #{id_object(obj)}" unless args
      end.join("\n")
    else
      obj.send(inspect_method)
    end
  ['```ruby', obj_str, '```'].join("\n")
end

#limited_inspect(obj) ⇒ Object

recursive



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/markdown_logging_proxy/tracer.rb', line 63

def limited_inspect(obj)
  case obj
  when Array
    "[#{obj.map { |o| limited_inspect(o) }.join(',')}]"
  when Hash
    # assumes keys are safe to .inspect
    insides = obj.each { |k, v| "#{k.inspect} => #{limited_inspect(v)}" }
    "{#{insides.join(', ')}}"
  when String, Symbol, Numeric, Class, true, false, nil
    truncated_inspect(obj)
  else
    id_object(obj)
  end
end

#proxy_response?(meth) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
# File 'lib/markdown_logging_proxy/tracer.rb', line 29

def proxy_response?(meth)
  case @proxy_response
  when true, false then @proxy_response
  else
    @proxy_response.member?(meth)
  end
end

#trace(meth, args, &blk) ⇒ Object



22
23
24
25
26
27
# File 'lib/markdown_logging_proxy/tracer.rb', line 22

def trace(meth, args, &blk)
  log_call_signature(meth, args, &blk) unless ignore?(meth)
  log_and_proxy_response(meth, args, &blk)
rescue StandardError => e
  log_and_reraise_error(meth, e)
end

#truncated_inspect(obj, limit = 280) ⇒ Object



83
84
85
86
87
# File 'lib/markdown_logging_proxy/tracer.rb', line 83

def truncated_inspect(obj, limit = 280)
  obj_str = obj.inspect
  obj_str = "#{obj_str.first(limit)}..." if obj_str.length > limit
  obj_str
end