[原创]ASP中令人震撼的Debug类(VBScript)
不知道用ASP写代码的朋友是不是和我有一样的感受,ASP中最头疼的就是调试程序的时候不方便,我想可能很多朋友都会用这样的方法“response.write ”,然后输出相关的语句来看看是否正确。前几天写了一个千行的页面,里面大概有七八个SUB/FUNCTION,调试的时候用了有三十几个response.write ,天,调试完后把这三十个一个个删除,累! <BR><BR>今天看到一个ASP中的Debug类(VBS),试用了一下,绝! <BR>使用方法很简单: <BR>test.asp <BR><BR><!--#INCLUDE FILE="debuggingConsole.asp"--> <BR><% <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>%> <BR><BR>=================================================== <BR><BR>debuggingConsole.asp <BR><BR><% <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 => 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) = "<TR><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';}""><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>|" <BR><BR> 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=""<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';}"">|#title#| <DIV id=data#sectname# style=""cursor:text;display:block;background:#FFFFFF;padding-left:8;"" onclick=""window.event.cancelBubble = true;"">|#data#| </DIV>|</DIV>|" <BR><BR> 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>|" <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> 接下
*********************************************************** <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 > 0 then <BR> call dbg_Data.Add(ValidLabel(label), "!!! Error: " & err.number & " " & 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 & "(" & i & ")" <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 & "(" & key & ")", Request.Cookies(cookie)(key)) <BR> Next <BR> End If <BR> Next <BR><BR> tbl = MakeTable(tbl) <BR> if Request.Cookies.count <= 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) & " 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 & " " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "." & 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") & " Ver: " & oSQLDB.Properties("DBMS Version")) <BR> dbg_DB_Data = AddRow(dbg_DB_Data, "Provider",oSQLDB.Properties("Provider Name") & " Ver: " & 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 <> "SERVER VARIABLES" and Name <> "QUERYSTRING" and Name <> "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, "<nobr>" & vItem & "</nobr>", server.HTMLEncode(Collection(vItem))) <BR> elseif (Name = "SERVER VARIABLES" and vItem <> "ALL_HTTP" and vItem <> "ALL_RAW") or Name <> "SERVER VARIABLES" then <BR> if Collection(vItem) <> "" then <BR> tbl = AddRow(tbl, vItem, server.HTMLEncode(Collection(vItem))) ' & " {" & TypeName(Collection(vItem)) & "}") <BR> else <BR> tbl = AddRow(tbl, vItem, "...") <BR> end if <BR> end if <BR> end if <BR> Next <BR> if ExtraInfo <> "" then tbl = tbl & "<TR><TD COLSPAN=2><HR></TR>" & ExtraInfo <BR> tbl = MakeTable(tbl) <BR> if Collection.count <= 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 & "|<TR valign=top>|<TD>|" & var & "|<TD>= " & val & "|</TR>" <BR> AddRow = t <BR>End Function <BR><BR>'*********************************************************** <BR>'* MakeTable <BR>'*********************************************************** <BR>Private Function MakeTable(byval tdata) <BR> tdata = "|<table border=0 style=""font-size:10pt;font-weight:normal;"">" + tdata + "</Table>|" <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 "<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>" <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 & " (&H" & Hex(Session.LCID) & ")"),"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 "</Table>" <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>%> <BR> 类的说明: <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-10-1 6:26:01编辑过]
页:
[1]