Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/method_call_logger.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_proc(proc_filter) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Method Details

#method_call_logging(option = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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