holybody 发表于 2004-6-14 12:08:00

[原创]ASP中令人震撼的Debug类(VBScript)

不知道用ASP写代码的朋友是不是和我有一样的感受,ASP中最头疼的就是调试程序的时候不方便,我想可能很多朋友都会用这样的方法“response.write ”,然后输出相关的语句来看看是否正确。前几天写了一个千行的页面,里面大概有七八个SUB/FUNCTION,调试的时候用了有三十几个response.write ,天,调试完后把这三十个一个个删除,累! <BR><BR>今天看到一个ASP中的Debug类(VBS),试用了一下,绝! <BR>使用方法很简单: <BR>test.asp <BR><BR>&lt;!--#INCLUDE FILE="debuggingConsole.asp"--&gt; <BR>&lt;% <BR>output="XXXX" <BR>Set debugstr = New debuggingConsole <BR>debugstr.Enabled = true <BR>   debugstr.Print "参数output的值", output <BR>   '…… <BR>   debugstr.draw <BR>Set debugstr = Nothing <BR>%&gt; <BR><BR>=================================================== <BR><BR>debuggingConsole.asp <BR><BR>&lt;% <BR>Class debuggingConsole <BR><BR>   private dbg_Enabled <BR>   private dbg_Show <BR>   private dbg_RequestTime <BR>   private dbg_FinishTime <BR>   private dbg_Data <BR>   private dbg_DB_Data <BR>   private dbg_AllVars <BR>   private dbg_Show_default <BR>   private DivSets(2) <BR>               <BR>'Construktor =&gt; set the default values <BR>Private Sub Class_Initialize() <BR>   dbg_RequestTime = Now() <BR>   dbg_AllVars = false <BR>   Set dbg_Data = Server.CreateObject("Scripting.Dictionary") <BR><BR>DivSets(0) = "&lt;TR&gt;&lt;TD style='cursor:hand;' onclick=""<b>javascript</b> :if (document.getElementById('data#sectname#').style.display=='none'){document.getElementById('data#sectname#').style.display='block';}else{document.getElementById('data#sectname#').style.display='none';}""&gt;&lt;DIV id=sect#sectname# style=""font-weight:bold;cursor:hand;background:#7EA5D7;color:white;padding-left:4;padding-right:4;padding-bottom:2;""&gt;|#title#|        &lt;DIV id=data#sectname# style=""cursor:text;display:none;background:#FFFFFF;padding-left:8;"" onclick=""window.event.cancelBubble = true;""&gt;|#data#|        &lt;/DIV&gt;|&lt;/DIV&gt;|" <BR><BR>   DivSets(1) = "&lt;TR&gt;&lt;TD&gt;&lt;DIV id=sect#sectname# style=""font-weight:bold;cursor:hand;background:#7EA5D7;color:white;padding-left:4;padding-right:4;padding-bottom:2;"" onclick=""<b>javascript</b> :if (document.getElementById('data#sectname#').style.display=='none'){document.getElementById('data#sectname#').style.display='block';}else{document.getElementById('data#sectname#').style.display='none';}""&gt;|#title#|        &lt;DIV id=data#sectname# style=""cursor:text;display:block;background:#FFFFFF;padding-left:8;"" onclick=""window.event.cancelBubble = true;""&gt;|#data#|        &lt;/DIV&gt;|&lt;/DIV&gt;|" <BR><BR>   DivSets(2) = "&lt;TR&gt;&lt;TD&gt;&lt;DIV id=sect#sectname# style=""background:#7EA5D7;color:lightsteelblue;padding-left:4;padding-right:4;padding-bottom:2;""&gt;|#title#|        &lt;DIV id=data#sectname# style=""display:none;background:lightsteelblue;padding-left:8""&gt;|#data#|        &lt;/DIV&gt;|&lt;/DIV&gt;|" <BR><BR>   dbg_Show_default = "0,0,0,0,0,0,0,0,0,0,0" <BR>End Sub <BR><BR>Public Property Let Enabled(bNewValue) '' Sets "enabled" to true or false <BR>   dbg_Enabled = bNewValue <BR>End Property <BR><BR>Public Property Get Enabled '' Gets the "enabled" value <BR>   Enabled = dbg_Enabled <BR>End Property <BR><BR>Public Property Let Show(bNewValue) '' Sets the debugging panel. Where each digit in the string represents a debug information pane in order (11 of them). 1=open, 0=closed <BR>   dbg_Show = bNewValue <BR>End Property <BR><BR>Public Property Get Show '' Gets the debugging panel. <BR>   Show = dbg_Show <BR>End Property <BR><BR>Public Property Let AllVars(bNewValue) '' Sets wheather all variables will be displayed or not. true/false <BR>   dbg_AllVars = bNewValue <BR>End Property <BR><BR>Public Property Get AllVars '' Gets if all variables will be displayed. <BR>   AllVars = dbg_AllVars <BR>End Property


<BR>                                                                                                                                                                               接下

holybody 发表于 2004-6-14 12:09:00

*********************************************************** <BR>''@SDESCRIPTION: Adds a variable to the debug-informations. <BR>''@PARAM:       - label : Description of the variable <BR>''@PARAM:       - output : The variable itself <BR>'*********************************************************** <BR>Public Sub Print(label, output) <BR>   If dbg_Enabled Then <BR>     if err.number &gt; 0 then <BR>       call dbg_Data.Add(ValidLabel(label), "!!! Error: " &amp; err.number &amp; " " &amp;        err.Description) <BR>       err.Clear <BR>     else <BR>       uniqueID = ValidLabel(label) <BR>       response.write uniqueID <BR>       call dbg_Data.Add(uniqueID, output) <BR>     end if <BR>   End If <BR>End Sub <BR>       <BR>'*********************************************************** <BR>'* ValidLabel <BR>'*********************************************************** <BR>Private Function ValidLabel(byval label) <BR>   dim i, lbl <BR>   i = 0 <BR>   lbl = label <BR>   do <BR>   if not dbg_Data.Exists(lbl) then exit do <BR>   i = i + 1 <BR>   lbl = label &amp; "(" &amp; i &amp; ")" <BR>   loop until i = i <BR>       <BR>   ValidLabel = lbl <BR>End Function <BR>       <BR>'*********************************************************** <BR>'* PrintCookiesInfo <BR>'*********************************************************** <BR>Private Sub PrintCookiesInfo(byval DivSetNo) <BR>   dim tbl, cookie, key, tmp <BR>   For Each cookie in Request.Cookies <BR>   If Not Request.Cookies(cookie).HasKeys Then <BR>     tbl = AddRow(tbl, cookie, Request.Cookies(cookie))               <BR>   Else <BR>     For Each key in Request.Cookies(cookie) <BR>     tbl = AddRow(tbl, cookie &amp; "(" &amp; key &amp; ")", Request.Cookies(cookie)(key))               <BR>               Next <BR>   End If <BR>   Next <BR><BR>   tbl = MakeTable(tbl) <BR>   if Request.Cookies.count &lt;= 0 then DivSetNo = 2 <BR>   tmp = replace(replace(replace(DivSets(DivSetNo),"#sectname#","COOKIES"),"#title#","COOKIES"),"#data#",tbl) <BR>   Response.Write replace(tmp,"|", vbcrlf) <BR>end sub <BR>       <BR>'*********************************************************** <BR>'* PrintSummaryInfo <BR>'*********************************************************** <BR>Private Sub PrintSummaryInfo(byval DivSetNo) <BR>   dim tmp, tbl <BR>   tbl = AddRow(tbl, "Time of Request",dbg_RequestTime) <BR>   tbl = AddRow(tbl, "Elapsed Time",DateDiff("s", dbg_RequestTime, dbg_FinishTime) &amp; " seconds") <BR>   tbl = AddRow(tbl, "Request Type",Request.ServerVariables("REQUEST_METHOD")) <BR>   tbl = AddRow(tbl, "Status Code",Response.Status) <BR>   tbl = AddRow(tbl, "Script Engine",ScriptEngine &amp; " " &amp; ScriptEngineMajorVersion &amp; "." &amp; ScriptEngineMinorVersion &amp; "." &amp; ScriptEngineBuildVersion) <BR>   tbl = MakeTable(tbl) <BR>   tmp = replace(replace(replace(DivSets(DivSetNo),"#sectname#","SUMMARY"),"#title#","SUMMARY INFO"),"#data#",tbl) <BR>   Response.Write replace(tmp,"|", vbcrlf) <BR>End Sub <BR><BR>'*********************************************************** <BR>''@SDESCRIPTION: Adds the Database-connection object to the debug-instance. To display Database-information <BR>''@PARAM:       - oSQLDB : connection-object <BR>'*********************************************************** <BR>Public Sub GrabDatabaseInfo(byval oSQLDB) <BR>   dbg_DB_Data = AddRow(dbg_DB_Data, "ADO Ver",oSQLDB.Version) <BR>   dbg_DB_Data = AddRow(dbg_DB_Data, "OLEDB Ver",oSQLDB.Properties("OLE DB Version")) <BR>   dbg_DB_Data = AddRow(dbg_DB_Data, "DBMS",oSQLDB.Properties("DBMS Name") &amp; " Ver: " &amp; oSQLDB.Properties("DBMS Version")) <BR>   dbg_DB_Data = AddRow(dbg_DB_Data, "Provider",oSQLDB.Properties("Provider Name") &amp; " Ver: " &amp; oSQLDB.Properties("Provider Version")) <BR>End Sub <BR><BR>'*********************************************************** <BR>'* PrintDatabaseInfo <BR>'*********************************************************** <BR>Private Sub PrintDatabaseInfo(byval DivSetNo) <BR>   dim tbl <BR>   tbl = MakeTable(dbg_DB_Data) <BR>   tbl = replace(replace(replace(DivSets(DivSetNo),"#sectname#","DATABASE"),"#title#","DATABASE INFO"),"#data#",tbl) <BR>   Response.Write replace(tbl,"|", vbcrlf) <BR>End Sub <BR><BR>'*********************************************************** <BR>'* PrintCollection <BR>'*********************************************************** <BR>Private Sub PrintCollection(Byval Name, ByVal Collection, ByVal DivSetNo, ByVal ExtraInfo) <BR>   Dim vItem, tbl, Temp <BR>   For Each vItem In Collection <BR>     if isobject(Collection(vItem)) and Name &lt;&gt; "SERVER VARIABLES" and Name &lt;&gt; "QUERYSTRING" and Name &lt;&gt; "FORM" then <BR>       tbl = AddRow(tbl, vItem, "{object}") <BR>     elseif isnull(Collection(vItem)) then <BR>       tbl = AddRow(tbl, vItem, "{null}") <BR>     elseif isarray(Collection(vItem)) then <BR>       tbl = AddRow(tbl, vItem, "{array}") <BR>     else <BR>       if dbg_AllVars then <BR>       tbl = AddRow(tbl, "&lt;nobr&gt;" &amp; vItem &amp; "&lt;/nobr&gt;", server.HTMLEncode(Collection(vItem))) <BR>     elseif (Name = "SERVER VARIABLES" and vItem &lt;&gt; "ALL_HTTP" and vItem &lt;&gt; "ALL_RAW") or Name &lt;&gt; "SERVER VARIABLES" then <BR>       if Collection(vItem) &lt;&gt; "" then <BR>       tbl = AddRow(tbl, vItem, server.HTMLEncode(Collection(vItem))) ' &amp; " {" &amp; TypeName(Collection(vItem)) &amp; "}") <BR>       else <BR>       tbl = AddRow(tbl, vItem, "...") <BR>       end if <BR>     end if <BR>   end if <BR>   Next <BR>   if ExtraInfo &lt;&gt; "" then tbl = tbl &amp; "&lt;TR&gt;&lt;TD COLSPAN=2&gt;&lt;HR&gt;&lt;/TR&gt;" &amp; ExtraInfo <BR>   tbl = MakeTable(tbl) <BR>   if Collection.count &lt;= 0 then DivSetNo =2 <BR>     tbl = replace(replace(DivSets(DivSetNo),"#title#",Name),"#data#",tbl) <BR>     tbl = replace(tbl,"#sectname#",replace(Name," ","")) <BR>     Response.Write replace(tbl,"|", vbcrlf) <BR>End Sub <BR>       <BR>'*********************************************************** <BR>'* AddRow <BR>'*********************************************************** <BR>Private Function AddRow(byval t, byval var, byval val) <BR>   t = t &amp; "|&lt;TR valign=top&gt;|&lt;TD&gt;|" &amp; var &amp; "|&lt;TD&gt;= " &amp; val &amp; "|&lt;/TR&gt;" <BR>   AddRow = t <BR>End Function <BR><BR>'*********************************************************** <BR>'* MakeTable <BR>'*********************************************************** <BR>Private Function MakeTable(byval tdata) <BR>   tdata = "|&lt;table border=0 style=""font-size:10pt;font-weight:normal;""&gt;" + tdata + "&lt;/Table&gt;|" <BR>   MakeTable = tdata <BR>End Function <BR><BR>'*********************************************************** <BR>''@SDESCRIPTION: Draws the Debug-panel <BR>'*********************************************************** <BR>Public Sub draw() <BR>   If dbg_Enabled Then <BR>     dbg_FinishTime = Now() <BR>       <BR>   Dim DivSet, x <BR>   DivSet = split(dbg_Show_default,",") <BR>                 dbg_Show = split(dbg_Show,",") <BR>       <BR>   For x = 0 to ubound(dbg_Show) <BR>     divSet(x) = dbg_Show(x) <BR>   Next <BR>       <BR>   Response.Write "&lt;BR&gt;&lt;Table width=100% cellspacing=0 border=0 style=""font-family:arial;font-size:9pt;font-weight:normal;""&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV style=""background:#005A9E;color:white;padding:4;font-size:12pt;font-weight:bold;""&gt;Debugging-console:&lt;/DIV&gt;" <BR>   Call PrintSummaryInfo(divSet(0)) <BR>                 Call PrintCollection("VARIABLES", dbg_Data,divSet(1),"") <BR>         Call PrintCollection("QUERYSTRING", Request.QueryString(), divSet(2),"") <BR>         Call PrintCollection("FORM", Request.Form(),divSet(3),"") <BR>         Call PrintCookiesInfo(divSet(4)) <BR>         Call PrintCollection("SESSION", Session.Contents(),divSet(5),AddRow(AddRow(AddRow("","Locale ID",Session.LCID &amp; " (&amp;H" &amp; Hex(Session.LCID) &amp; ")"),"Code Page",Session.CodePage),"Session ID",Session.SessionID)) <BR>         Call PrintCollection("APPLICATION", Application.Contents(),divSet(6),"") <BR>         Call PrintCollection("SERVER VARIABLES", Request.ServerVariables(),divSet(7),AddRow("","Timeout",Server.ScriptTimeout)) <BR>         Call PrintDatabaseInfo(divSet(8)) <BR>         Call PrintCollection("SESSION STATIC OBJECTS", Session.StaticObjects(),divSet(9),"") <BR>         Call PrintCollection("APPLICATION STATIC OBJECTS", Application.StaticObjects(),divSet(10),"") <BR>         Response.Write "&lt;/Table&gt;" <BR>   End If <BR>End Sub <BR><BR>'Destructor <BR>Private Sub Class_Terminate() <BR>   Set dbg_Data = Nothing <BR>End Sub <BR><BR>End Class <BR><BR>%&gt; <BR>

holybody 发表于 2004-6-14 12:09:00

类的说明: <BR><BR><BR>CLASS debuggingConsole <BR>Version: 1.2 <BR><BR>-------------------------------------------------------------------------------- <BR><BR>Public Properties <BR><BR>Property Let Enabled(bNewValue)=== Sets "enabled" to true or false <BR>       <BR>Property Get Enabled=== Gets the "enabled" value <BR>       <BR>Property Let Show(bNewValue)=== Sets the debugging panel. Where each digit in the string represents a debug information pane in order (11 of them). 1=open, 0=closed <BR>       <BR>Property Get Show=== Gets the debugging panel. <BR>       <BR>Property Let AllVars(bNewValue)=== Sets wheather all variables will be displayed or not. true/false <BR><BR>Property Get AllVars=== Gets if all variables will be displayed.       <BR><BR>-------------------------------------------------------------------------------- <BR>Public Methods <BR><BR>public sub===Print (label, output) <BR>   Adds a variable to the debug-informations.       <BR><BR>public sub===GrabDatabaseInfo (byval oSQLDB) <BR>   Adds the Database-connection object to the debug-instance. To display Database-information       <BR><BR>public sub===draw () <BR>   Draws the Debug-panel       <BR><BR>-------------------------------------------------------------------------------- <BR>Methods Detail <BR>       <BR>public sub===Print (label, output) <BR>Parameters:       <BR>   - label : Description of the variable <BR>   - output : The variable itself <BR>       <BR>public sub===GrabDatabaseInfo (byval oSQLDB) <BR>Parameters:       <BR>   - oSQLDB : connection-object

金钱世界 发表于 2004-9-20 00:00:00

千言万语看不明,都系钱好睇
[此贴子已经被作者于2004-10-1 6:26:01编辑过]
页: [1]
查看完整版本: [原创]ASP中令人震撼的Debug类(VBScript)