autolog: set_trace_func without all the typing

If it helps anyone developing Rails, apps, or gems, I wrote a shortcut for set_trace_func, so next time you want to just add a line before and after some function you are trying to debug you can have Ruby temporarily output every line, method, etc. executed. Also, it lets you define the format, use other loggers, etc. via a proc/lambda define, since you might not like the default format. Apologize for posting here if it wouldn’t help with Rails development (you are probably using a real debugger).

Example:

In Gemfile add: gem ‘autolog’

Around some part of code add this before: Autolog.methods and this after: Autolog.off

Hope it helps someone who needs a quick tool periodically without all the time sink of profiling or setting a breakpoint and debugging in a slow IDE.

Hmm. Even though Autolog (which mostly just does a set_trace_func) works in places like before/after MyRailsApp::Application.configure block in development.rb, but putting Autolog.methods at the top of application_controller.rb results in a LocalJumpError (/path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `load’: unexpected return (LocalJumpError)). Maybe Rails 3.2.8 autoloading and set_trace_func (calling a lambda, if that matters) is not a good combination?:

return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb.30 #Binding:0x007ff7b1403d40 Rails::Initializable::Initializer run return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb.54 #Binding:0x007ff7b14039a8 Rails::Initializable run_initializers return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application.rb.136 #Binding:0x007ff7b1403728 Rails::Application initialize! return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/railtie/configurable.rb.30 #Binding:0x007ff7b1403480 Rails::Railtie::Configurable::ClassMethods method_missing call /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb.275 #Binding:0x007ff7b1402aa8 ActiveSupport::Dependencies::Blamable blame_file! return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb.277 #Binding:0x007ff7b1402440 ActiveSupport::Dependencies::Blamable blame_file! return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb.233 #Binding:0x007ff7b1401a40 ActiveSupport::Dependencies::Loadable load_dependency return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb.251 #Binding:0x007ff7b1409790 ActiveSupport::Dependencies::Loadable require return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb.51 #Binding:0x007ff7b14093f8 Rack::Builder initialize return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb.40 #Binding:0x007ff7b1408ac0 Rack::Builder parse_file return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb.200 #Binding:0x007ff7b1408840 Rack::Server app return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands/server.rb.46 #Binding:0x007ff7b1408598 Rails::Server app return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb.301 #Binding:0x007ff7b14082f0 Rack::Server wrapped_app return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb.252 #Binding:0x007ff7b1408048 Rack::Server start call /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb.179 #Binding:0x007ff7b1407c88 Rack::Server options return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb.181 #Binding:0x007ff7b14078f0 Rack::Server options Exiting return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands/server.rb.74 #Binding:0x007ff7b1406ec8 Rails::Server start /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in load': unexpected return (LocalJumpError) from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in block in load_file’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:639:in new_constants_in' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:468:in load_file’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:353:in require_or_load' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:502:in load_missing_constant’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:192:in block in const_missing' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in each’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in const_missing' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/base_controller/menu.rb:2:in module:ActiveAdmin’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/base_controller/menu.rb:1:in <top (required)>' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in require’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in block in require' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in load_dependency’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in require' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/base_controller.rb:2:in <top (required)>’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/resource_controller/actions.rb:2:in <module:ActiveAdmin>' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/resource_controller/actions.rb:1:in <top (required)>’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in require' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in block in require’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in load_dependency' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in require’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/resource_controller.rb:2:in <top (required)>' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/batch_actions.rb:7:in block in <top (required)>’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/event.rb:25:in call' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/event.rb:25:in block in dispatch’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/event.rb:24:in each' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/event.rb:24:in dispatch’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/application.rb:179:in load!' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/application.rb:205:in routes’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin.rb:79:in routes' from /path/to/git/some_app_gemset/config/routes.rb:2:in block in <top (required)>’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:282:in instance_exec' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:282:in eval_block’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:260:in draw' from /path/to/git/some_app_gemset/config/routes.rb:1:in <top (required)>’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in load' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in block in load’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in load_dependency' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in load’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application/routes_reloader.rb:40:in block in load_paths' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application/routes_reloader.rb:40:in each’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application/routes_reloader.rb:40:in load_paths' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application/routes_reloader.rb:16:in reload!’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application.rb:108:in reload_routes!' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/reloader.rb:34:in reload!’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/reloader.rb:61:in block in attach!' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:429:in _run__1105766863370890952__prepare__156232144867029268__callbacks’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in __run_callback' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in _run_prepare_callbacks’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in run_callbacks' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/actionpack-3.2.8/lib/action_dispatch/middleware/reloader.rb:74:in prepare!’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/actionpack-3.2.8/lib/action_dispatch/middleware/reloader.rb:48:in prepare!' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application/finisher.rb:47:in block in module:Finisher’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb:30:in instance_exec' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb:30:in run’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb:55:in block in run_initializers' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb:54:in each’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb:54:in run_initializers' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application.rb:136:in initialize!’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/railtie/configurable.rb:30:in method_missing' from /path/to/git/some_app_gemset/config/environment.rb:5:in <top (required)>’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in require' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in block in require’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in load_dependency' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in require’ from /path/to/git/some_app_gemset/config.ru:4:in block in <main>' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb:51:in instance_eval’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb:51:in initialize' from /path/to/git/some_app_gemset/config.ru:1:in new’ from /path/to/git/some_app_gemset/config.ru:1:in <main>' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb:40:in eval’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb:40:in parse_file' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb:200:in app’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands/server.rb:46:in app' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb:301:in wrapped_app’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb:252:in start' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands/server.rb:70:in start’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands.rb:55:in block in <top (required)>' from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands.rb:50:in tap’ from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands.rb:50:in <top (required)>' from script/rails:6:in require’ from script/rails:6:in `’

I forgot to mention the url: https://github.com/garysweaver/autolog

It’s now a lot more like Tracer and can take blocks, and is available on main as method and Object as class method:

autolog :methods do #… end

I put the LocalJumpError up on stackoverflow if anyone wants to help. I think it is matter of something not checking if block is given, but I’m doing it now everywhere in my code, so I think it might somehow be a Rails 3.2.8 dependency loading issue, but I’m probably incredibly wrong and should be smacked in the leg with a wooden paddle for saying that.

Please try my gem and see its new code before just looking at the stackoverflow post or attempting to guess based on stacktrace, unless you’re feeling lucky:

Thanks everyone.