Created
July 9, 2012 17:23
-
-
Save coldnebo/3077744 to your computer and use it in GitHub Desktop.
unroller replacement for rails...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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
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.