• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pboling / debug_logging / 8114731249

01 Mar 2024 05:11PM UTC coverage: 92.479% (+0.7%) from 91.789%
8114731249

push

github

pboling
🐛 Added missing debug_* methods

136 of 158 branches covered (86.08%)

Branch coverage included in aggregate %.

7 of 11 new or added lines in 2 files covered. (63.64%)

8 existing lines in 3 files now uncovered.

528 of 560 relevant lines covered (94.29%)

136.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.53
/lib/debug_logging/argument_printer.rb
1
require "date"
1✔
2
require "time"
1✔
3

4
module DebugLogging
1✔
5
  module ArgumentPrinter
1✔
6
    def debug_benchmark_to_s(tms:)
1✔
7
      "completed in #{format("%f", tms.real)}s (#{format("%f", tms.total)}s CPU)"
38✔
8
    end
9

10
    def debug_invocation_id_to_s(args: nil, kwargs: nil, start_at: nil, config_proxy: nil)
1✔
11
      return "" unless (args || kwargs) && config_proxy
146✔
12

13
      if config_proxy.debug_add_invocation_id
145✔
14
        time = start_at ? Util.debug_time(start_at) : Time.now
110✔
15
        unique_id = (time.to_f.to_s % "%#-21a")[4..-4]
110✔
16
        invocation = " ~#{args.object_id}|#{kwargs.object_id}@#{unique_id}~"
110✔
17
        case config_proxy.debug_add_invocation_id
110✔
18
        when true
108✔
19
          invocation
108✔
20
        else
2✔
21
          config_proxy.debug_add_invocation_id.call(ColorizedString[invocation])
2✔
22
        end
23
      else
35✔
24
        ""
35✔
25
      end
26
    end
27

28
    # @return [String]
29
    def debug_time_to_s(time_or_monotonic, config_proxy: nil)
1✔
30
      return "" unless config_proxy&.debug_add_timestamp
94✔
31
      return config_proxy.debug_time_formatter_proc.call(Time.now) unless time_or_monotonic
5!
32

33
      time = Util.debug_time(time_or_monotonic)
5✔
34

35
      config_proxy.debug_time_formatter_proc.call(time)
5✔
36
    end
37

38
    # A custom time format will never apply here, because ActiveSupport::Notifications have a required time format
39
    def debug_event_time_to_s(time_or_monotonic)
1✔
40
      # Time format must match:
41
      #   \d{4,}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-+]\d{4}
42
      #   YYYY-MM-DD HH:mm:ss +00:00
43
      #   strftime("%F %T %z")
44
      time_or_monotonic = Time.now if time_or_monotonic.nil? || (time_or_monotonic.respond_to?(:empty?) && time_or_monotonic.empty?)
538✔
45
      time = Util.debug_time(time_or_monotonic)
538✔
46
      DebugLogging::Constants::EVENT_TIME_FORMATTER.call(time)
538✔
47
    end
48

49
    def debug_invocation_to_s(klass: nil, separator: nil, method_to_log: nil, config_proxy: nil)
1✔
50
      return "" unless config_proxy
147✔
51

52
      klass_string = if config_proxy.debug_colorized_chain_for_class
146✔
53
        config_proxy.debug_colorized_chain_for_class.call(ColorizedString[klass.to_s])
3✔
54
      else
143✔
55
        klass.to_s
143✔
56
      end
57
      method_string = if config_proxy.debug_colorized_chain_for_method
146✔
58
        config_proxy.debug_colorized_chain_for_method.call(ColorizedString[method_to_log.to_s])
38✔
59
      else
108✔
60
        method_to_log.to_s
108✔
61
      end
62
      "#{klass_string}#{separator}#{method_string}"
146✔
63
    end
64

65
    def debug_signature_to_s(args: nil, kwargs: nil, config_proxy: nil) # rubocop:disable Metrics/CyclomaticComplexity
1✔
66
      return "" unless (args || kwargs) && config_proxy
414✔
67

68
      printed_args = ""
413✔
69

70
      add_args_ellipsis = false
413✔
71
      args = args.dup
413✔
72
      args.push(kwargs) if kwargs
413✔
73
      if config_proxy.debug_last_hash_to_s_proc && args[-1].is_a?(Hash)
413✔
74
        add_other_args_ellipsis = false
25✔
75
        if args.length > 1
25✔
76
          if config_proxy.debug_multiple_last_hashes
11✔
77
            last_hash_args, other_args = args.partition do |arg|
6✔
78
              arg.is_a?(Hash)
34✔
79
            end
80
            other_args_string = if config_proxy.debug_args_to_s_proc
6✔
81
              printed, add_other_args_ellipsis = debug_safe_proc(
2✔
82
                proc_name: "args_to_s_proc",
83
                proc: config_proxy.debug_args_to_s_proc,
84
                args: other_args,
85
                max_length: config_proxy.debug_args_max_length,
86
              )
87
              printed
2✔
88
            else
4✔
89
              other_args.map(&:inspect).join(", ").tap do |x|
4✔
90
                add_other_args_ellipsis = x.length > config_proxy.debug_args_max_length
4✔
91
              end[0..(config_proxy.debug_args_max_length)]
4✔
92
            end
93
            other_args_string += config_proxy.debug_ellipsis if add_other_args_ellipsis
6✔
94
            # On the debug_multiple_last_hashes truthy branch we don't print the ellipsis after regular args
95
            #   because it will go instead after each of the last hashes (if needed)
96
            #   ...join(", ").tap {|x| _add_args_ellipsis = x.length > config_proxy.debug_args_max_length}
97
            last_hash_args_string = last_hash_args.map do |arg|
6✔
98
              arr = []
12✔
99
              printed, add_last_hash_ellipsis = debug_safe_proc(
12✔
100
                proc_name: "last_hash_to_s_proc",
101
                proc: config_proxy.debug_last_hash_to_s_proc,
102
                args: arg,
103
                max_length: config_proxy.debug_last_hash_max_length,
104
              )
105
              printed += config_proxy.debug_ellipsis if add_last_hash_ellipsis
12✔
106
              arr << printed
12✔
107
              arr
12✔
108
            end.flatten.join(", ")
109
            printed_args += other_args_string if other_args_string
6!
110
            printed_args += ", " if !other_args_string.empty? && !last_hash_args_string.empty?
6!
111
            printed_args += last_hash_args_string if last_hash_args_string && !last_hash_args_string.empty?
6!
112
          else
5✔
113
            other_args = args[0..-2]
5✔
114
            other_args_string = if config_proxy.debug_args_to_s_proc
5✔
115
              printed, add_other_args_ellipsis = debug_safe_proc(
2✔
116
                proc_name: "args_to_s_proc",
117
                proc: config_proxy.debug_args_to_s_proc,
118
                args: other_args,
119
                max_length: config_proxy.debug_args_max_length,
120
              )
121
              printed
2✔
122
            else
3✔
123
              other_args.map(&:inspect).join(", ").tap do |x|
3✔
124
                add_other_args_ellipsis = x.length > config_proxy.debug_args_max_length
3✔
125
              end[0..(config_proxy.debug_args_max_length)]
3✔
126
            end
127
            other_args_string += config_proxy.debug_ellipsis if add_other_args_ellipsis
5✔
128
            printed_args += other_args_string
5✔
129
            printed, add_last_hash_ellipsis = debug_safe_proc(
5✔
130
              proc_name: "last_hash_to_s_proc",
131
              proc: config_proxy.debug_last_hash_to_s_proc,
132
              args: args[-1],
133
              max_length: config_proxy.debug_last_hash_max_length,
134
            )
135
            printed_args += ", #{printed}"
5✔
136
            printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis
5✔
137
          end
138
        else
14✔
139
          printed, add_last_hash_ellipsis = debug_safe_proc(
14✔
140
            proc_name: "last_hash_to_s_proc",
141
            proc: config_proxy.debug_last_hash_to_s_proc,
142
            args: args[0],
143
            max_length: config_proxy.debug_last_hash_max_length,
144
          )
145
          printed_args += printed
14✔
146
          printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis
14✔
147
        end
148
      else
388✔
149
        printed_args += if config_proxy.debug_args_to_s_proc
388✔
150
          printed, add_args_ellipsis = debug_safe_proc(
2✔
151
            proc_name: "args_to_s_proc",
152
            proc: config_proxy.debug_args_to_s_proc,
153
            args: args,
154
            max_length: config_proxy.debug_args_max_length,
155
          )
156
          printed
2✔
157
        elsif args.length == 1 && args[0].is_a?(Hash)
386✔
158
          # handle double splat
93✔
159
          "**#{args.map(&:inspect).join(", ").tap do |x|
93✔
160
                 add_args_ellipsis = x.length > config_proxy.debug_args_max_length
93✔
161
               end }"[0..(config_proxy.debug_args_max_length)]
93✔
162
        else
293✔
163
          args.map(&:inspect).join(", ").tap do |x|
293✔
164
            add_args_ellipsis = x.length > config_proxy.debug_args_max_length
293✔
165
          end[0..(config_proxy.debug_args_max_length)]
293✔
166
        end
167
        printed_args += config_proxy.debug_ellipsis if add_args_ellipsis
388✔
168
      end
169
      "(#{printed_args})"
413✔
170
    end
171

172
    def debug_safe_proc(proc_name:, proc:, args:, max_length:)
1✔
173
      max_length ||= 1000 # can't be nil
39✔
174
      begin
175
        add_ellipsis = false
39✔
176
        printed = String(proc.call(args)).tap do |x|
39✔
177
          add_ellipsis = x.length > max_length
39✔
178
        end[0..max_length]
179
        [printed, add_ellipsis]
39✔
UNCOV
180
      rescue StandardError => e
×
UNCOV
181
        ["#{e.class}: #{e.message}\nPlease check that your #{proc_name} is able to handle #{args}", false]
×
182
      end
183
    end
184

185
    def debug_payload_to_s(payload: nil, config_proxy: nil)
1✔
186
      return "" unless payload && config_proxy
412✔
187

188
      case config_proxy.debug_add_payload
411✔
189
      when true
409✔
190
        payload.inspect
409✔
191
      else
2✔
192
        printed_payload = ""
2✔
193
        printed, add_payload_ellipsis = debug_safe_proc(
2✔
194
          proc_name: "add_payload",
195
          proc: config_proxy.debug_add_payload,
196
          args: payload,
197
          max_length: config_proxy.debug_payload_max_length,
198
        )
199
        printed_payload += printed
2✔
200
        printed_payload += config_proxy.debug_ellipsis if add_payload_ellipsis
2✔
201
        printed_payload
2✔
202
      end
203
    end
204

205
    module_function
1✔
206

207
    def debug_event_name_to_s(method_to_notify: nil)
1✔
208
      "#{method_to_notify}.log"
128✔
209
    end
210
  end
211
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc