Reputation: 12379
For a Rails app, I would like to overload or modify Kernel::load such that it will print out the full path like puts "-> #{File.expand_path(File.dirname(__FILE__))}/#{File.basename(__FILE__)}"
for each Rails app file that is loaded.
How would I do this? I would like to further be able to filter, so I am guessing I'd throw a regex on there to be able to only show output (when I don't want it to be grossly verbose) that indicates the access of files within the current rails app root.
At any rate, I'd appreciate an example of overloading this method and where the appropriate place to do this would be active in the Rails app on server start.
Resolution:
Well okay after a bit of beating things around and discovering set_trace_func
I came up with the following:
I integrated set_trace_fun
into the header of development.rb
along the lines of what was implemented in the mtrace
gem. I had a lot of tweaking to do to filter out everything but my app directory as well as a number of other chatty methods, but it seems to be working out thus far. The mtrace source that is effectively integrated with modification is as follows:
mtrace_indent = 0
set_trace_func lambda {|event, file, line, id, binding, klass|
case event
when 'call'
puts "\e[90m| " * mtrace_indent +
"\e[35m#{klass}\e[90m#\e[36m#{id} \e[90m#{file}:#{line}\e[0m" +
"#{' ' * ([10 - mtrace_indent, 1].max)}\e[90mtxmt://open/?url=file://#{ File.expand_path(file) }&line=#{ line }\e[0m"
mtrace_indent += 1
when 'return'
mtrace_indent -= 1
end
mtrace_indent = 0 if mtrace_indent < 0
}
Of course the txmt url portions can be removed if you're not on a mac, but that is really quite helpful.
Upvotes: 3
Views: 224
Reputation: 230521
Try this:
module Kernel
alias_method :old_load, :load
def load filename, wrap = false
puts "-> #{filename}"
old_load filename, wrap
end
end
You can put this into config/initializers
, but that may be a little bit late for what you're trying to do.
Apparently, Rails init process starts with config/environment.rb
, so if you put this code at the top of that file, it should be the first thing loaded.
Upvotes: 4