Module: DebugLogging::ArgumentPrinter

Included in:
LogSubscriber
Defined in:
lib/debug_logging/argument_printer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.debug_event_name_to_s(method_to_notify: nil) ⇒ Object



122
123
124
# File 'lib/debug_logging/argument_printer.rb', line 122

def debug_event_name_to_s(method_to_notify: nil)
  "#{method_to_notify}.log"
end

Instance Method Details

#debug_benchmark_to_s(tms:) ⇒ Object



5
6
7
# File 'lib/debug_logging/argument_printer.rb', line 5

def debug_benchmark_to_s(tms:)
  "completed in #{format('%f', tms.real)}s (#{format('%f', tms.total)}s CPU)"
end

#debug_invocation_id_to_s(args: nil, config_proxy: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/debug_logging/argument_printer.rb', line 9

def debug_invocation_id_to_s(args: nil, config_proxy: nil)
  return '' unless args && config_proxy

  if config_proxy.debug_add_invocation_id
    invocation = " ~#{args.object_id}@#{(Time.now.to_f.to_s % '%#-21a')[4..-4]}~"
    case config_proxy.debug_add_invocation_id
    when true
      invocation
    else
      config_proxy.debug_add_invocation_id.call(ColorizedString[invocation])
    end
  else
    ''
  end
end

#debug_invocation_to_s(klass: nil, separator: nil, method_to_log: nil, config_proxy: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/debug_logging/argument_printer.rb', line 25

def debug_invocation_to_s(klass: nil, separator: nil, method_to_log: nil, config_proxy: nil)
  return '' unless config_proxy

  klass_string = if config_proxy.debug_colorized_chain_for_class
                   config_proxy.debug_colorized_chain_for_class.call(ColorizedString[klass.to_s])
                 else
                   klass.to_s
                 end
  method_string = if config_proxy.debug_colorized_chain_for_method
                    config_proxy.debug_colorized_chain_for_method.call(ColorizedString[method_to_log.to_s])
                  else
                    method_to_log.to_s
                  end
  "#{klass_string}#{separator}#{method_string}"
end

#debug_payload_to_s(payload: nil, config_proxy: nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/debug_logging/argument_printer.rb', line 105

def debug_payload_to_s(payload: nil, config_proxy: nil)
  return '' unless payload && config_proxy

  if payload
    case config_proxy.debug_add_payload
    when true
      payload.inspect
    else
      config_proxy.debug_add_payload.call(**payload)
    end
  else
    ''
  end
end

#debug_signature_to_s(args: nil, config_proxy: nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/debug_logging/argument_printer.rb', line 41

def debug_signature_to_s(args: nil, config_proxy: nil) # rubocop:disable Metrics/CyclomaticComplexity
  return '' unless args && config_proxy

  printed_args = ''

  add_args_ellipsis = false
  if config_proxy.debug_last_hash_to_s_proc && args[-1].is_a?(Hash)
    add_last_hash_ellipsis = false
    if args.length > 1
      if config_proxy.debug_multiple_last_hashes
        last_hash_args, other_args = args.partition do |arg|
          arg.is_a?(Hash)
        end
        other_args_string = other_args.map(&:inspect).join(', ')[0..(config_proxy.debug_args_max_length)]
        # On the debug_multiple_last_hashes truthy branch we don't print the ellipsis after regular args
        #   because it will go instead after the last hash (if needed)
        #   ...join(", ").tap {|x| _add_args_ellipsis = x.length > config_proxy.debug_args_max_length}
        last_hash_args_string = last_hash_args.map do |arg|
          arr = []
          arr << config_proxy.debug_last_hash_to_s_proc.call(arg).to_s
                             .tap do |x|
            add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length
          end
          if add_last_hash_ellipsis
            arr[-1] = arr[-1][0..(config_proxy.debug_last_hash_max_length)]
            arr << config_proxy.debug_ellipsis
          end
          arr
        end.flatten.join(', ')
        printed_args += other_args_string if other_args_string
        printed_args += ', ' if !other_args_string.empty? && !last_hash_args_string.empty?
        printed_args += last_hash_args_string if last_hash_args_string && !last_hash_args_string.empty?
      else
        printed_args += args[0..-2].map(&:inspect).join(', ').tap do |x|
                          add_args_ellipsis = x.length > config_proxy.debug_args_max_length
                        end[0..(config_proxy.debug_args_max_length)]
        printed_args += config_proxy.debug_ellipsis if add_args_ellipsis
        printed_args += ", #{config_proxy.debug_last_hash_to_s_proc.call(args[-1]).tap do |x|
                               add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length
                             end[0..(config_proxy.debug_last_hash_max_length)]}"
        printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis
      end
    else
      printed_args += String(config_proxy.debug_last_hash_to_s_proc.call(args[0])).tap do |x|
                        add_last_hash_ellipsis = x.length > config_proxy.debug_last_hash_max_length
                      end[0..(config_proxy.debug_last_hash_max_length)]
      printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis
    end
  else
    printed_args += if args.length == 1 && args[0].is_a?(Hash)
                      # handle double splat
                      ("**#{args.map(&:inspect).join(', ').tap do |x|
                              add_args_ellipsis = x.length > config_proxy.debug_args_max_length
                            end }")[0..(config_proxy.debug_args_max_length)]
                    else
                      args.map(&:inspect).join(', ').tap do |x|
                        add_args_ellipsis = x.length > config_proxy.debug_args_max_length
                      end[0..(config_proxy.debug_args_max_length)]
                    end
    printed_args += config_proxy.debug_ellipsis if add_args_ellipsis
  end
  "(#{printed_args})"
end