Module: SimpleDebugLogging::ClassMethodLogger

Defined in:
lib/simple_debug_logging.rb

Instance Method Summary collapse

Instance Method Details

#logged(*methods_to_log) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/simple_debug_logging.rb', line 50

def logged(*methods_to_log)
  methods_to_log.each do |method_to_log|
    original_method = method(method_to_log)
    (class << self; self; end).class_eval do
      define_method(method_to_log.to_sym) do |*args|
        method_return_value = nil
        invocation_id = " ~#{args.object_id}@#{Time.now.to_i}~" if args
        puts "#{self}.#{method_to_log}(#{args.map {|x| x.inspect}.join(", ")})#{invocation_id}"
        elapsed = Benchmark.realtime do
          method_return_value = original_method.call(*args)
        end
        puts "#{self}.#{method_to_log} ~#{args.hash}~ complete in #{elapsed}s#{invocation_id}"
        method_return_value
      end
    end
  end
end