|
1 | 1 | module FirePHP
|
2 |
| - private |
| 2 | + def self.included(controller) |
| 3 | + controller.after_filter :firephp_filter |
| 4 | + end |
3 | 5 |
|
| 6 | + protected |
4 | 7 | def firephp(obj = "", type = :log)
|
| 8 | + @firephp ||= {} |
| 9 | + types = [:log, :info, :warn, :error] |
| 10 | + type = (types.include?(type) ? type : :log).to_s.upcase |
| 11 | + @firephp[type] ||= [] |
| 12 | + @firephp[type] << obj |
5 | 13 | end
|
6 | 14 | alias_method :fb, :firephp
|
| 15 | + |
| 16 | + private |
| 17 | + def firephp_filter |
| 18 | + # Add headers only when browser has FirePHP-Plugin |
| 19 | + return if !(request.headers["HTTP_USER_AGENT"]=~/FirePHP\//) |
| 20 | + # Do not add headers in production mode |
| 21 | + return if ENV["RAILS_ENV"] == "production" |
| 22 | + return if @firephp.blank? |
| 23 | + |
| 24 | + headers["X-FirePHP-Data-100000000001"]='{' |
| 25 | + headers["X-FirePHP-Data-200000000001"]='"FirePHP.Dump":{' |
| 26 | + headers["X-FirePHP-Data-200000000002"]="\"RailsVersion\":\"#{RAILS_GEM_VERSION}\"," |
| 27 | + headers["X-FirePHP-Data-299999999999"]='"__SKIP__":"__SKIP__"},' |
| 28 | + headers["X-FirePHP-Data-300000000001"]='"FirePHP.Firebug.Console":[' |
| 29 | + count=2 |
| 30 | + @firephp.each do |type, arr| |
| 31 | + next if !(type=~/^(LOG|INFO|WARN|ERROR)$/) |
| 32 | + arr.each do |a| |
| 33 | + headers["X-FirePHP-Data-3#{sprintf("%011d", count)}"]="[\"#{type}\",#{a.to_json}]," |
| 34 | + count+=1 |
| 35 | + end |
| 36 | + end |
| 37 | + headers["X-FirePHP-Data-399999999999"]='["__SKIP__"]],' |
| 38 | + headers["X-FirePHP-Data-999999999999"]='"__SKIP__":"__SKIP__"}' |
| 39 | + end |
7 | 40 | end
|
0 commit comments