Module: DebugLogging::ClassLogger

Defined in:
lib/debug_logging/class_logger.rb

Instance Method Summary collapse

Instance Method Details

#logged(*methods_to_log) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/debug_logging/class_logger.rb', line 3

def logged(*methods_to_log)
  methods_to_log.each do |method_to_log|
    # method name must be a symbol
    method_to_log = method_to_log.to_sym
    original_method = method(method_to_log)
    (class << self; self; end).class_eval do
      define_method(method_to_log) do |*args|
        method_return_value = nil
        invocation_id = " ~#{args.object_id}@#{Time.now.to_i}~" if debug_add_invocation_id
        debug_log "#{self}.#{method_to_log}#{debug_arguments_to_s(args)}#{invocation_id}"
        if debug_class_benchmarks
          elapsed = Benchmark.realtime do
            method_return_value = original_method.call(*args)
          end
          debug_log "#{self}.#{method_to_log} completed in #{sprintf("%f", elapsed)}s#{invocation_id}"
        else
          method_return_value = original_method.call(*args)
        end
        method_return_value
      end
    end
  end
end