#!/usr/local/bin/ruby # -*- coding: utf-8 -*- require 'json' module FirePHRuby module Common FPHR_INIT_HEADERS = { 'X-FirePHP-Data-100000000001' => '{', 'X-FirePHP-Data-999999999999' => '"__SKIP__":"__SKIP__"}' } FPHR_INIT_HEADERS_LOG = { 'X-FirePHP-Data-300000000001' => '"FirePHP.Firebug.Console":[', 'X-FirePHP-Data-399999999999' => '["__SKIP__"]],' } FPHR_INIT_HEADERS_DUMP = { 'X-FirePHP-Data-200000000001' => '"FirePHP.Dump":{', 'X-FirePHP-Data-299999999999' => '"__SKIP__":"__SKIP__"},' } FPHR_KINDS = %w(LOG INFO WARN ERROR DUMP TRACE EXCEPTION TABLE) def build_headers msg,kind='LOG' headers = [] t_pref,g_kind = kind == 'DUMP' ? [ 2, 'DUMP' ] : [ 3, 'LOG' ] c_time = Time.new digits = "#{c_time.tv_sec.to_s[-5,5]}#{c_time.usec}" headers << [ "X-FirePHP-Data-#{t_pref}#{digits}", "[ \"#{kind}\", #{msg.to_json} ]," ] unless instance_variables.member? "@firephruby_inited_#{g_kind.downcase}" FirePHRuby::Common::const_get( "FPHR_INIT_HEADERS_#{g_kind}" ).each_pair { |k,v| headers << [ k, v ] } instance_variable_set( "@firephruby_inited_#{g_kind.downcase}", true ) end return headers end end class CGI include Common def initialize FPHR_INIT_HEADERS.each_pair { |k,v| puts "#{k}: #{v}" } #FPHR_INIT_HEADERS_LOG.each_pair { |k,v| puts "#{k}: #{v}" } end def firelog msg,kind='LOG' headers = build_headers msg,kind headers.each { |h| puts "#{h[0]}: #{h[1]}" } end def log msg firelog msg,'LOG' end def info msg firelog msg,'INFO' end def warn msg firelog msg,'WARN' end def error msg firelog msg,'ERROR' end end module WEBrick include Common def fire_log msg,kind='LOG' headers = build_headers msg,kind headers.each { |h| @header[h[0]] = h[1] } unless defined? @firephruby_inited FPHR_INIT_HEADERS.each_pair { |k,v| @header[k] = v } @firephruby_inited = true end end def fire_info msg fire_log msg,'INFO' end def fire_warn msg fire_log msg,'WARN' end def fire_error msg fire_log msg,'ERROR' end end end module WEBrick class HTTPResponse include FirePHRuby::WEBrick end end