ASP里面令人震撼地Debug类(VBScript)

我想可能很多朋友都会用这样的方法“response.write ”,然后输出相关的语句来看看是否正确。前几天写了一个千行的页面,里面大概有七八个sub/function,调试的时候用了有三十几个response.write ,天,调试完后把这三十个一个个删除,累!
今天看到一个asp中的debug类(vbs),试用了一下,绝!
使用方法很简单:
test.asp

 代码如下:


<%
output="xxxx"
set debugstr = new debuggingconsole
debugstr.enabled = true
   debugstr.print "参数output的值", output
   '……
   debugstr.draw
set debugstr = nothing
%>

===================================================
debuggingconsole.asp
 代码如下:

<%
class debuggingconsole
   private dbg_enabled
   private dbg_show
   private dbg_requesttime
   private dbg_finishtime
   private dbg_data
   private dbg_db_data
   private dbg_allvars
   private dbg_show_default
   private divsets(2)
'construktor => set the default values
private sub class_initialize()
   dbg_requesttime = now()
   dbg_allvars = false
   set dbg_data = server.createobject("scripting.dictionary")
divsets(0) = "
|#title#|
|#data#|
|
|"
   divsets(1) = "
|#title#|
|#data#|
|
|"
   divsets(2) = "
|#title#|
|#data#|
|
|"
   dbg_show_default = "0,0,0,0,0,0,0,0,0,0,0"
end sub
public property let enabled(bnewvalue) ''[bool] sets "enabled" to true or false
   dbg_enabled = bnewvalue
end property
public property get enabled ''[bool] gets the "enabled" value
   enabled = dbg_enabled
end property
public property let show(bnewvalue) ''[string] sets the debugging panel. where each digit in the string represents a debug information pane in order (11 of them). 1=open, 0=closed
   dbg_show = bnewvalue
end property
public property get show ''[string] gets the debugging panel.
   show = dbg_show
end property
public property let allvars(bnewvalue) ''[bool] sets wheather all variables will be displayed or not. true/false
   dbg_allvars = bnewvalue
end property
public property get allvars ''[bool] gets if all variables will be displayed.
   allvars = dbg_allvars
end property
'***********************************************************
''@sdescription: adds a variable to the debug-informations.
''@param: - label [string]: description of the variable
''@param: - output [variable]: the variable itself
'***********************************************************
public sub print(label, output)
   if dbg_enabled then
     if err.number > 0 then
       call dbg_data.add(validlabel(label), "/b56/ error: " & err.number & " " & err.description)
       err.clear
     else
       uniqueid = validlabel(label)
       response.write uniqueid
       call dbg_data.add(uniqueid, output)
     end if
   end if
end sub
'***********************************************************
'* validlabel
'***********************************************************
private function validlabel(byval label)
   dim i, lbl
   i = 0
   lbl = label
   do
   if not dbg_data.exists(lbl) then exit do
   i = i + 1
   lbl = label & "(" & i & ")"
   loop until i = i
   validlabel = lbl
end function
'***********************************************************
'* printcookiesinfo
'***********************************************************
private sub printcookiesinfo(byval divsetno)
   dim tbl, cookie, key, tmp
   for each cookie in request.cookies
   if not request.cookies(cookie).haskeys then
     tbl = addrow(tbl, cookie, request.cookies(cookie))
   else
     for each key in request.cookies(cookie)
     tbl = addrow(tbl, cookie & "(" & key & ")", request.cookies(cookie)(key))
next
   end if
   next
   tbl = maketable(tbl)
   if request.cookies.count <= 0 then divsetno = 2
   tmp = replace(replace(replace(divsets(divsetno),"#sectname#","cookies"),"#title#","cookies"),"#data#",tbl)
   response.write replace(tmp,"|", vbcrlf)
end sub
'***********************************************************
'* printsummaryinfo
'***********************************************************
private sub printsummaryinfo(byval divsetno)
   dim tmp, tbl
   tbl = addrow(tbl, "time of request",dbg_requesttime)
   tbl = addrow(tbl, "elapsed time",datediff("s", dbg_requesttime, dbg_finishtime) & " seconds")
   tbl = addrow(tbl, "request type",request.servervariables("request_method"))
   tbl = addrow(tbl, "status code",response.status)
   tbl = addrow(tbl, "script engine",scriptengine & " " & scriptenginemajorversion & "." & scriptengineminorversion & "." & scriptenginebuildversion)
   tbl = maketable(tbl)
   tmp = replace(replace(replace(divsets(divsetno),"#sectname#","summary"),"#title#","summary info"),"#data#",tbl)
   response.write replace(tmp,"|", vbcrlf)
end sub
'***********************************************************
''@sdescription: adds the database-connection object to the debug-instance. to display database-information
''@param: - osqldb [object]: connection-object
'***********************************************************
public sub grabdatabaseinfo(byval osqldb)
   dbg_db_data = addrow(dbg_db_data, "ado ver",osqldb.version)
   dbg_db_data = addrow(dbg_db_data, "oledb ver",osqldb.properties("ole db version"))
   dbg_db_data = addrow(dbg_db_data, "dbms",osqldb.properties("dbms name") & " ver: " & osqldb.properties("dbms version"))
   dbg_db_data = addrow(dbg_db_data, "provider",osqldb.properties("provider name") & " ver: " & osqldb.properties("provider version"))
end sub
'***********************************************************
'* printdatabaseinfo
'***********************************************************
private sub printdatabaseinfo(byval divsetno)
   dim tbl
   tbl = maketable(dbg_db_data)
   tbl = replace(replace(replace(divsets(divsetno),"#sectname#","database"),"#title#","database info"),"#data#",tbl)
   response.write replace(tbl,"|", vbcrlf)
end sub
'***********************************************************
'* printcollection
'***********************************************************
private sub printcollection(byval name, byval collection, byval divsetno, byval extrainfo)
   dim vitem, tbl, temp
   for each vitem in collection
     if isobject(collection(vitem)) and name <> "server variables" and name <> "querystring" and name <> "form" then
       tbl = addrow(tbl, vitem, "{object}")
     elseif isnull(collection(vitem)) then
       tbl = addrow(tbl, vitem, "{null}")
     elseif isarray(collection(vitem)) then
       tbl = addrow(tbl, vitem, "{array}")
     else
       if dbg_allvars then
       tbl = addrow(tbl, "" & vitem & "", server.htmlencode(collection(vitem)))
     elseif (name = "server variables" and vitem <> "all_http" and vitem <> "all_raw") or name <> "server variables" then
       if collection(vitem) <> "" then
       tbl = addrow(tbl, vitem, server.htmlencode(collection(vitem))) ' & " {" & typename(collection(vitem)) & "}")
       else
       tbl = addrow(tbl, vitem, "...")
       end if
     end if
   end if
   next
   if extrainfo <> "" then tbl = tbl & " " & extrainfo
   tbl = maketable(tbl)
   if collection.count <= 0 then divsetno =2
     tbl = replace(replace(divsets(divsetno),"#title#",name),"#data#",tbl)
     tbl = replace(tbl,"#sectname#",replace(name," ",""))
     response.write replace(tbl,"|", vbcrlf)
end sub
'***********************************************************
'* addrow
'***********************************************************
private function addrow(byval t, byval var, byval val)
   t = t & "|||" & var & "|= " & val & "|"
   addrow = t
end function
'***********************************************************
'* maketable
'***********************************************************
private function maketable(byval tdata)
   tdata = "|" + tdata + "
|"
   maketable = tdata
end function
'***********************************************************
''@sdescription: draws the debug-panel
'***********************************************************
public sub draw()
   if dbg_enabled then
     dbg_finishtime = now()
   dim divset, x
   divset = split(dbg_show_default,",")
   dbg_show = split(dbg_show,",")
   for x = 0 to ubound(dbg_show)
     divset(x) = dbg_show(x)
   next
   response.write "
debugging-console:
"
   call printsummaryinfo(divset(0))
   call printcollection("variables", dbg_data,divset(1),"")
   call printcollection("querystring", request.querystring(), divset(2),"")
   call printcollection("form", request.form(),divset(3),"")
   call printcookiesinfo(divset(4))
   call printcollection("session", session.contents(),divset(5),addrow(addrow(addrow("","locale id",session.lcid & " (&h" & hex(session.lcid) & ")"),"code page",session.codepage),"session id",session.sessionid))
   call printcollection("application", application.contents(),divset(6),"")
   call printcollection("server variables", request.servervariables(),divset(7),addrow("","timeout",server.scripttimeout))
   call printdatabaseinfo(divset(8))
   call printcollection("session static objects", session.staticobjects(),divset(9),"")
   call printcollection("application static objects", application.staticobjects(),divset(10),"")
   response.write "
"
   end if
end sub
'destructor
private sub class_terminate()
   set dbg_data = nothing
end sub
end class
%>

类的说明:

class debuggingconsole
version: 1.2
--------------------------------------------------------------------------------
public properties
property let enabled(bnewvalue)===[bool] sets "enabled" to true or false
property get enabled===[bool] gets the "enabled" value
property let show(bnewvalue)===[string] sets the debugging panel. where each digit in the string represents a debug information pane in order (11 of them). 1=open, 0=closed
property get show===[string] gets the debugging panel.
property let allvars(bnewvalue)===[bool] sets wheather all variables will be displayed or not. true/false
property get allvars===[bool] gets if all variables will be displayed.
--------------------------------------------------------------------------------
public methods
public sub===print (label, output)
   adds a variable to the debug-informations.
public sub===grabdatabaseinfo (byval osqldb)
   adds the database-connection object to the debug-instance. to display database-information
public sub===draw ()
   draws the debug-panel
--------------------------------------------------------------------------------
methods detail
public sub===print (label, output)
parameters:
   - label [string]: description of the variable
   - output [variable]: the variable itself
public sub===grabdatabaseinfo (byval osqldb)
parameters:
   - osqldb [object]: connection-object

相关文章