Skip to content

Instantly share code, notes, and snippets.

@coldnebo
Created July 9, 2012 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save coldnebo/3077744 to your computer and use it in GitHub Desktop.
Save coldnebo/3077744 to your computer and use it in GitHub Desktop.
unroller replacement for rails...
# to be used only for local debugging
LK_ENABLE_TRACE = false
if LK_ENABLE_TRACE && Rails.env.development?
require 'thread'
class LkTracer
def initialize
@sem = Mutex.new
end
def record
set_trace_func proc { |event, file, line, id, binding, classname|
if event != "c-call" && event != "c-return" && file =~ /(app\/.*)/
store_trace(event, $1, file, line)
end
}
end
def stop
set_trace_func nil
end
def dump(req,res)
@sem.synchronize {
File.open("log/web_trace.log", "a") do |f|
if req.request_line.match(/\.(js|css|gif|png|jpg)\?/).nil?
f.puts "--REQUEST------------------------------------------------------------------"
f.puts req.to_s rescue nil
f.puts "--RESPONSE-----------------------------------------------------------------"
f.puts resp.to_s rescue nil
f.puts "--TRACE--------------------------------------------------------------------"
@trace_lines ||= []
@trace_lines.each do |arr|
#debugger
event, rel_path, full_path, line = arr
#printf "\033[34m%8s %s:%-2d:\033[0m%s", event, rel_path, line, get_line(full_path,line)
source_line = get_line(full_path,line)
#if source_line =~ /\s(if|unless)/
#f.printf "# %-20s:%-2d: \n%s", rel_path, line, source_line
f.printf "%8s: %s # TRACE %s:%-2d\n", event, source_line.chop, rel_path, line,
#end
end
@trace_lines = []
#puts "called dump_trace from #{object_id}"
f.puts "---------------------------------------------------------------------------"
end
end
}
end
private
def get_line(file, line)
@script_lines ||= {}
unless list = @script_lines[file]
begin
f = ::File::open(file)
begin
@script_lines[file] = list = f.readlines
ensure
f.close
end
rescue Exception => e
@script_lines[file] = list = []
end
end
if l = list[line - 1]
l
else
"-\n"
end
end
def store_trace(event, rel_path, full_path, line)
@sem.synchronize {
@trace_lines ||= []
@trace_lines << [event, rel_path, full_path, line]
#puts "called store_trace from #{object_id}"
}
end
end
module Rack
module Handler
class WEBrick < ::WEBrick::HTTPServlet::AbstractServlet
alias_method :orig_service, :service
def service(req, res)
result = nil
begin
tr = ::LkTracer.new
tr.record
result = orig_service(req,res)
#tr.stop
tr.dump(req,res)
rescue
end
result
end
end
end
end
end
@coldnebo
Copy link
Author

coldnebo commented Jul 9, 2012

This isn't wrapped up into a gem or unit-tested yet, but it's a sketch of how I rolled my own "unroller" for rails. Output is limited to the rails app codebase.

I rolled my own because the ruby unroller gem has gone stale (it doesn't work with advertised old dependencies or new dependencies, so it's broken right now.) But as I was looking at unroller, I realized that it is doing a lot of things (color, formatting, logging, etc.) besides simply fronting the tracing functionality. I have ideas on how unroller might be simplified and streamlined down to just the essential DSL, while allowing extensions for color, etc... but I don't have time right now to pursue that. So here's my sketch for rails 2.3.14 at least and if you like it, feel free to build/run with it.

Thanks!

@coldnebo
Copy link
Author

coldnebo commented Jul 9, 2012

Some notes: regarding line #100 above (tr.stop) -- I commented that out because I'm getting seg faults if I leave it in. Not sure why, since everything seems to be kosher above. Previously I had output writing to console and figured there was some kind of GC happening during the last drip of output causing the fault, but now it writes to a file and has the thread guards on it. Maybe I need more guards in an ensure? I haven't devoted much time to thinking about this in detail since what I have works for now.

@coldnebo
Copy link
Author

See the new version for Rails3 using Rack middleware instead of the monkey patch: https://gist.github.com/4346202

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment