Module: DebugLogging::ArgumentPrinter
- Included in:
- LogSubscriber
- Defined in:
- lib/debug_logging/argument_printer.rb
Class Method Summary collapse
- .debug_args_to_s(args) ⇒ Object
- .debug_event_name_to_s(decorated_method: nil) ⇒ Object
- .debug_hash_key_to_s(key) ⇒ Object
- .debug_value_to_s(value) ⇒ Object
Instance Method Summary collapse
- #debug_benchmark_to_s(tms:) ⇒ Object
-
#debug_event_time_to_s(time_or_monotonic) ⇒ Object
A custom time format will never apply here, because ActiveSupport::Notifications have a required time format.
- #debug_invocation_id_to_s(args: nil, kwargs: nil, start_at: nil, config_proxy: nil) ⇒ Object
- #debug_invocation_to_s(klass: nil, separator: nil, decorated_method: nil, config_proxy: nil) ⇒ Object
- #debug_payload_to_s(payload: nil, config_proxy: nil) ⇒ Object
- #debug_safe_proc(proc_name:, proc:, args:, max_length:) ⇒ Object
-
#debug_signature_to_s(args: nil, kwargs: nil, config_proxy: nil) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
- #debug_time_to_s(time_or_monotonic, config_proxy: nil) ⇒ String
Class Method Details
.debug_args_to_s(args) ⇒ Object
209 210 211 |
# File 'lib/debug_logging/argument_printer.rb', line 209 def debug_args_to_s(args) args.map { |arg| debug_value_to_s(arg) }.join(", ") end |
.debug_event_name_to_s(decorated_method: nil) ⇒ Object
228 229 230 |
# File 'lib/debug_logging/argument_printer.rb', line 228 def debug_event_name_to_s(decorated_method: nil) "#{decorated_method}.log" end |
.debug_hash_key_to_s(key) ⇒ Object
224 225 226 |
# File 'lib/debug_logging/argument_printer.rb', line 224 def debug_hash_key_to_s(key) key.is_a?(Symbol) ? key.to_s : key.inspect end |
.debug_value_to_s(value) ⇒ Object
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/debug_logging/argument_printer.rb', line 213 def debug_value_to_s(value) case value when Hash "{#{value.map { |key, inner_value| "#{debug_hash_key_to_s(key)}: #{debug_value_to_s(inner_value)}" }.join(", ")}}" when Array "[#{value.map { |inner_value| debug_value_to_s(inner_value) }.join(", ")}]" else value.inspect end end |
Instance Method Details
#debug_benchmark_to_s(tms:) ⇒ Object
6 7 8 |
# File 'lib/debug_logging/argument_printer.rb', line 6 def debug_benchmark_to_s(tms:) "completed in #{format("%f", tms.real)}s (#{format("%f", tms.total)}s CPU)" end |
#debug_event_time_to_s(time_or_monotonic) ⇒ Object
A custom time format will never apply here, because ActiveSupport::Notifications have a required time format
40 41 42 43 44 45 46 47 48 |
# File 'lib/debug_logging/argument_printer.rb', line 40 def debug_event_time_to_s(time_or_monotonic) # Time format must match: # \d{4,}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-+]\d{4} # YYYY-MM-DD HH:mm:ss +00:00 # strftime("%F %T %z") time_or_monotonic = Time.now if time_or_monotonic.nil? || (time_or_monotonic.respond_to?(:empty?) && time_or_monotonic.empty?) time = Util.debug_time(time_or_monotonic) DebugLogging::Constants::EVENT_TIME_FORMATTER.call(time) end |
#debug_invocation_id_to_s(args: nil, kwargs: nil, start_at: nil, config_proxy: nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/debug_logging/argument_printer.rb', line 10 def debug_invocation_id_to_s(args: nil, kwargs: nil, start_at: nil, config_proxy: nil) return "" unless config_proxy return "" unless args || kwargs if config_proxy.debug_add_invocation_id time = start_at ? Util.debug_time(start_at) : Time.now unique_id = (time.to_f.to_s % "%#-21a")[4..-4] invocation = " ~#{args.object_id}|#{kwargs.object_id}@#{unique_id}~" 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, decorated_method: nil, config_proxy: nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/debug_logging/argument_printer.rb', line 50 def debug_invocation_to_s(klass: nil, separator: nil, decorated_method: 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[decorated_method.to_s]) else decorated_method.to_s end "#{klass_string}#{separator}#{method_string}" end |
#debug_payload_to_s(payload: nil, config_proxy: nil) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/debug_logging/argument_printer.rb', line 187 def debug_payload_to_s(payload: nil, config_proxy: nil) return "" unless payload && config_proxy case config_proxy.debug_add_payload when true debug_value_to_s(payload) else printed_payload = "" printed, add_payload_ellipsis = debug_safe_proc( proc_name: "add_payload", proc: config_proxy.debug_add_payload, args: payload, max_length: config_proxy.debug_payload_max_length, ) printed_payload += printed printed_payload += config_proxy.debug_ellipsis if add_payload_ellipsis printed_payload end end |
#debug_safe_proc(proc_name:, proc:, args:, max_length:) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/debug_logging/argument_printer.rb', line 174 def debug_safe_proc(proc_name:, proc:, args:, max_length:) max_length ||= 1000 # can't be nil begin add_ellipsis = false printed = String(proc.call(args)).tap do |x| add_ellipsis = x.length > max_length end[0..max_length] [printed, add_ellipsis] rescue StandardError => e ["#{e.class}: #{e.}\nPlease check that your #{proc_name} is able to handle #{args}", false] end end |
#debug_signature_to_s(args: nil, kwargs: nil, config_proxy: nil) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/debug_logging/argument_printer.rb', line 66 def debug_signature_to_s(args: nil, kwargs: nil, config_proxy: nil) # rubocop:disable Metrics/CyclomaticComplexity return "" unless config_proxy return "" unless args || kwargs printed_args = "" add_args_ellipsis = false args = args.dup args.push(kwargs) if kwargs&.keys && !kwargs&.keys&.length&.zero? if config_proxy.debug_last_hash_to_s_proc && args[-1].is_a?(Hash) add_other_args_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 = if config_proxy.debug_args_to_s_proc printed, add_other_args_ellipsis = debug_safe_proc( proc_name: "args_to_s_proc", proc: config_proxy.debug_args_to_s_proc, args: other_args, max_length: config_proxy.debug_args_max_length, ) printed else debug_args_to_s(other_args).tap do |x| add_other_args_ellipsis = x.length > config_proxy.debug_args_max_length end[0..(config_proxy.debug_args_max_length)] end other_args_string += config_proxy.debug_ellipsis if add_other_args_ellipsis # On the debug_multiple_last_hashes truthy branch we don't print the ellipsis after regular args # because it will go instead after each of the last hashes (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 = [] printed, add_last_hash_ellipsis = debug_safe_proc( proc_name: "last_hash_to_s_proc", proc: config_proxy.debug_last_hash_to_s_proc, args: arg, max_length: config_proxy.debug_last_hash_max_length, ) printed += config_proxy.debug_ellipsis if add_last_hash_ellipsis arr << printed 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 other_args = args[0..-2] other_args_string = if config_proxy.debug_args_to_s_proc printed, add_other_args_ellipsis = debug_safe_proc( proc_name: "args_to_s_proc", proc: config_proxy.debug_args_to_s_proc, args: other_args, max_length: config_proxy.debug_args_max_length, ) printed else debug_args_to_s(other_args).tap do |x| add_other_args_ellipsis = x.length > config_proxy.debug_args_max_length end[0..(config_proxy.debug_args_max_length)] end other_args_string += config_proxy.debug_ellipsis if add_other_args_ellipsis printed_args += other_args_string printed, add_last_hash_ellipsis = debug_safe_proc( proc_name: "last_hash_to_s_proc", proc: config_proxy.debug_last_hash_to_s_proc, args: args[-1], max_length: config_proxy.debug_last_hash_max_length, ) printed_args += ", #{printed}" printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis end else printed, add_last_hash_ellipsis = debug_safe_proc( proc_name: "last_hash_to_s_proc", proc: config_proxy.debug_last_hash_to_s_proc, args: args[0], max_length: config_proxy.debug_last_hash_max_length, ) printed_args += printed printed_args += config_proxy.debug_ellipsis if add_last_hash_ellipsis end else printed_args += if config_proxy.debug_args_to_s_proc printed, add_args_ellipsis = debug_safe_proc( proc_name: "args_to_s_proc", proc: config_proxy.debug_args_to_s_proc, args: args, max_length: config_proxy.debug_args_max_length, ) printed elsif args.length == 1 && args[0].is_a?(Hash) # handle double splat "**#{debug_args_to_s(args).tap do |x| add_args_ellipsis = x.length > config_proxy.debug_args_max_length end }"[0..(config_proxy.debug_args_max_length)] else debug_args_to_s(args).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 |
#debug_time_to_s(time_or_monotonic, config_proxy: nil) ⇒ String
30 31 32 33 34 35 36 37 |
# File 'lib/debug_logging/argument_printer.rb', line 30 def debug_time_to_s(time_or_monotonic, config_proxy: nil) return "" unless config_proxy&. return config_proxy.debug_time_formatter_proc.call(Time.now) unless time_or_monotonic time = Util.debug_time(time_or_monotonic) config_proxy.debug_time_formatter_proc.call(time) end |