class Class

Public Class Methods

add_proc(proc_filter) click to toggle source
# File lib/method_call_logger.rb, line 29
def self.add_proc(proc_filter)
  unless defined?(@proc_list)
    @proc_list = []
    set_trace_func(
      proc {|*args|
        @proc_list.each{|_proc| _proc.call(*args)}
      }
    )
  end
  @proc_list.push(proc_filter)
end

Public Instance Methods

method_call_logging(option={}) click to toggle source
# File lib/method_call_logger.rb, line 4
def method_call_logging(option={})
  Class.add_proc(
    proc {|event, file, line, id, binding, klass|
      if event == "call" && klass == self
        if id != :initialize
          msg = "#{self.to_s}##{id.to_s}"
          unless binding.local_variables.empty?
            msg += "("
            msg += binding.local_variables.map{|vname|
                      "#{vname}:#{binding.local_variable_get(vname).inspect}"
                    }.join(", ")
            msg += ")"
          end
          logger = binding.receiver.instance_variable_get(:@logger)
          if option[id]
            logger.log(option[id], msg)
          else
            logger.debug(msg)
          end
        end
      end
    }
  )
end