lib/verbose.rb in verbose-0.0.1 vs lib/verbose.rb in verbose-0.0.2

- old
+ new

@@ -27,39 +27,53 @@ # # Author:: Yegor Bugayenko (yegor256@gmail.com) # Copyright:: Copyright (c) 2024 Yegor Bugayenko # License:: MIT class Verbose - def initialize(origin, log: $stdout) + def initialize(origin, log: nil) @origin = origin @log = log end def method_missing(*args) start = Time.now mtd = args.shift - begin - result = - if @origin.respond_to?(mtd) - params = @origin.method(mtd).parameters - reqs = params.count { |p| p[0] == :req } - if params.any? { |p| p[0] == :key } && args.size > reqs - @origin.__send__(mtd, *args[0...-1], **args.last) do |*a| - yield(*a) if block_given? - end - else - @origin.__send__(mtd, *args) do |*a| - yield(*a) if block_given? - end - end + if @origin.respond_to?(mtd) + params = @origin.method(mtd).parameters + reqs = params.count { |p| p[0] == :req } + if params.any? { |p| p[0] == :key } && args.size > reqs + @origin.__send__(mtd, *args[0...-1], **args.last) do |*a| + yield(*a) if block_given? + end + else + @origin.__send__(mtd, *args) do |*a| + yield(*a) if block_given? + end + end + else + super + end + ensure + params = args.map do |a| + if a.is_a?(String) + max = 32 + a = a.inspect + if a.length > max + "#{a[0..(max / 2) - 2]}...#{a[(max / 2) + 1..]}" else - super + a end - @log.debug("#{@origin.class}.#{mtd}(): finished in #{start.ago}") - result - rescue StandardError => e - @log.debug("#{@origin.class}.#{mtd}(): thrown #{e.class} in #{start.ago}") - raise e + elsif [Integer, Float, TrueClass, FalseClass].include?(a.class) + a + else + a.class + end + end + msg = "#{@origin.class}.#{mtd}(#{params.join(', ')}) in #{start.ago}" + if @log.respond_to?(:debug) + @log.debug(msg) + else + puts(msg) end end def respond_to?(method, include_private = false) @origin.respond_to?(method, include_private)