欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-07 11:48:46
我想可能很多朋友都会用这样的方法“response.write ”,然后输出相关的语句来看看是否正确。前几天写了一个千行的页面,里面大概有七八个sub/function,调...

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

复制代码 代码如下:

<!--#include file="debuggingconsole.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) = "<tr><td style='cursor:hand;' onclick=""javascript:if (document.getelementbyid('data#sectname#').style.display=='none'){document.getelementbyid('data#sectname#').style.display='block';}else{document.getelementbyid('data#sectname#').style.display='none';}""><div id=sect#sectname# style=""font-weight:bold;cursor:hand;background:#7ea5d7;color:white;padding-left:4;padding-right:4;padding-bottom:2;"">|#title#| <div id=data#sectname# style=""cursor:text;display:none;background:#ffffff;padding-left:8;"" onclick=""window.event.cancelbubble = true;"">|#data#| </div>|</div>|"
   divsets(1) = "<tr><td><div id=sect#sectname# style=""font-weight:bold;cursor:hand;background:#7ea5d7;color:white;padding-left:4;padding-right:4;padding-bottom:2;"" onclick=""javascript:if (document.getelementbyid('data#sectname#').style.display=='none'){document.getelementbyid('data#sectname#').style.display='block';}else{document.getelementbyid('data#sectname#').style.display='none';}"">|#title#| <div id=data#sectname# style=""cursor:text;display:block;background:#ffffff;padding-left:8;"" onclick=""window.event.cancelbubble = true;"">|#data#| </div>|</div>|"
   divsets(2) = "<tr><td><div id=sect#sectname# style=""background:#7ea5d7;color:lightsteelblue;padding-left:4;padding-right:4;padding-bottom:2;"">|#title#| <div id=data#sectname# style=""display:none;background:lightsteelblue;padding-left:8"">|#data#| </div>|</div>|"
   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), "!!! 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, "<nobr>" & vitem & "</nobr>", 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 & "<tr><td colspan=2><hr></tr>" & 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 & "|<tr valign=top>|<td>|" & var & "|<td>= " & val & "|</tr>"
   addrow = t
end function
'***********************************************************
'* maketable
'***********************************************************
private function maketable(byval tdata)
   tdata = "|<table border=0 style=""font-size:10pt;font-weight:normal;"">" + tdata + "</table>|"
   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 "<br><table width=100% cellspacing=0 border=0 style=""font-family:arial;font-size:9pt;font-weight:normal;""><tr><td><div style=""background:#005a9e;color:white;padding:4;font-size:12pt;font-weight:bold;"">debugging-console:</div>"
   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 "</table>"
   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