Skip to content

Instantly share code, notes, and snippets.

@spencerroan
Created January 4, 2012 20:06
Show Gist options
  • Save spencerroan/1561809 to your computer and use it in GitHub Desktop.
Save spencerroan/1561809 to your computer and use it in GitHub Desktop.
wrap an object in a proxy that will report what methods are called, from where and what is returned.
class InterfaceProxy
def self.create(delegate)
begin
#check if proxy exists
if delegate.__class == self
delegate.log "proxy previously created, returning delegate"
return delegate
end
rescue
# if not return a new one
# this is bad form, but most methods are forwarded to the delegate object
return new(delegate)
end
end
# forward methods to the delegate logging what is called and returned
def method_missing(method, *args)
begin
log "called #{method}(#{args.map(&:inspect).join(",")}) from #{caller.grep(/lead_scope/).reject{|m| m =~ /vendor/}}"
rval = @delegate.send(method, *args)
rescue => e
log e.message
log e.backtrace.grep(/lead_scope/).reject {|m| m =~ /vendor/}.join("\n")
end
log "returned #{rval.inspect}"
return rval
end
def log(message)
META_LOGGER.info "#{@delegate.object_id} #{@delegate.class} #{message}"
end
# expose :class as __class to check whether an object is already proxied.
alias_method :__class, :class
# remove almost all of the methods of this class.
instance_methods.each {|m| undef_method m unless m.to_s =~ /^log$|method_missing|^__/ }
private
# make this private to force use of create method
def initialize(delegate)
@delegate = delegate
log "created from #{caller[2,1]}"
end
end
=begin
I grep for lines I care about from the log and store them in 'log/meta.log'.
Then do a little processing with this script
I make a csv of the data.
=end
require 'fastercsv'
foo = Hash.new {|h,k| h[k] = Hash.new(0) }
lines = IO.readlines("log/meta.log")
lines.join("\n").scan(/ ([\w]+) called ([\w=?]+)/) {|type, method|
puts [type,method].inspect
foo[type][method] += 1
};nil
FCSV.new("meta.csv", 'w') {|f|
foo.each_pair {|klass, methods|
methods.each_pair {|m,count|
puts [klass,m,count].inspect;
f << [klass,m,count]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment