<% @LANGUAGE = "VBscript" %> <% Option Explicit %> <% pageTitle = "ASP Debugging" pageAuthor = "steve" addFooterSection "/consulting/code.asp", "return to code page" %> <%= openHeader %> <%= closeHeader %>

ASP Debugging

These are some of the debugging functions for working with classic ASP code that I've developed over the years. They're all collected in a single .asp file, debug.asp that I include with any project that I'm working on. Debug.asp is self-contained, and doesn't depend on any other include files.

To use these functions, download debug.asp (and rename the .txt file to .asp). Add the line <!--#INCLUDE FILE="debug.asp"--> to the top of your own .asp file. You'll need to be running an IIS web server of course.

All these functions return a string.

Variables

The first four functions are fairly simple, and just display the type and/or value of a variable.

function debug_vv (var)

This simply returns a string with the value of variable, or an empty string if value can't be represented. (i.e. if it is an array or object. Strings have double quotes placed around them, and the whole thing is properly htmlencoded.

function debug_vt (var)

This just returns a string representation of a variable's type.

function debug_var (var)

This works just like debug_vv, except that it will return the variable's type if the value can't be represented.

function debug_vartype (var)

This combines debug_vv and debug_vt, and returns both the type and value of a variable.

Collections

The next 7 functions all deal with printing out the contents of various collections.

function debug_collection (name, byref obj)

This is just a helper function that does all the work for the other 6. It takes a name (just some text for the table heading), and a reference to the collection being printed. It then generates a nice html table showing the number of objects and the names, types and values of all objects in the collection. The table is assigned the class 'debug_table', so that any fancy formatting can be done in CSS.

function debug_querystring

Returns a string showing the contents of the request.querystring collection.

function debug_form

Returns a string showing the contents of the request.form collection.

function debug_servervariables

Returns a string showing the contents of the request.servervariables collection.

function debug_cookies

Returns a string showing the contents of the request.cookies collection.

function debug_clientcertificate

Returns a string showing the contents of the request.clientcertificate collection.

function debug_sessionvars

Returns a string showing the contents of the session.contents collection.

Here's an example using the debug_querystring function. To test it, change the URL of this page to include a querystring with as many variables as you'd like, then scroll back down to this example. Or try this link: <%=pageName%>?name=Joe&color=blue&sport=soccer

<% if request.querystring.count = 0 then response.write "

(The example looks boring now because there's nothing in the querystring)

" end if response.write debug_querystring %>

Recordsets

The next two functions deal with recordsets returned from a database.

function debug_fieldtype (t)

This is similar to debug_vt, except that it shows the type of a variable returned from a database operation. That is, the type that the database thinks it is, not what it becomes once you actually assign it.

function debug_rs (rs)

This shows all rows & columns of a recordset, properly numbered, labeled, typed and htmlencoded for your viewing pleasure. The output would look something like this:

Recordset: (2 records)
Record userid
(Integer)
email
(VarWChar)
firstname
(VarWChar)
lastname
(VarWChar)
address
(Integer)
0 1 "programming@ornery-geeks.org" "Steve" "Fredette" (Null)
1 2 "sales@ornery-geeks.org" "Jackie" "Patti" (Null)

Html

The last function formats html.

function prettyhtml (html)

This function makes a half-assed attempt to indent html code before it is sent to the browser. Note that this function doesn't make any attempt to match html tags or anything like that. It just compares the number of open tags to the number of closed ones and bases the indenting on that number.

Why would you even bother? Well, quite often I build up large chunks of html in a string before sending it to the browser. The code can be quite complex and the individual bits of html can be buried several functions deep. Indenting it can let me see where the errors are more easily.

Someday when I have a lot of time to kill I might rewrite this function to do full html validation and give a report.

Integration into a live website

Usually I'll make a few calls to debugging functions while I'm trying to track down a problem, then remove the debugging code later. Other times though, I want to have debugging information show up on the development server, but not on the live site. The problem is, I don't want to maintain two separate sets of files, one with debugging code and one without. To solve this problem, I put something like this in the global.asa file:

sub application_onstart
   application.lock
   if instr (server.mappath ("/"), "projectcode") <> 0 then
      application ("debugmode") = true
   else
      application ("debugmode") = false
   end if
   application.unlock
end sub

What this does is set the variable application ("debugmode") to true, but only if the site is running on our development server. (All the web sites on our server are in a subdirectory of projectcode.) This way I can leave the debugging code in place and not worry about it showing up on the live site, and I don't have to worry about maintaining duplicate files.

Here's an example from one of my projects. The pagefooter function is called from every page, and generates the html code that closes out the html and body tags. However, if the site is running on the development server, it also display all the session variables, form variables and querystrings.

function pagefooter
   dim html
   html = vbcrlf

   ' Debugging stuff
   if application ("debugmode") then
      html = html & "<!-- debugging code -->" & vbcrlf
      html = html & "<hr />" & vbcrlf
      html = html & "<b>Debugging</b><table><tr><td valign=top style='float: left;'>" & vbcrlf
      html = html & debug_sessionvars
      html = html & "</td><td valign=top style='float: left;'>" & vbcrlf
      html = html & debug_form
      html = html & "</td><td valign=top style='float: left;'>" & vbcrlf
      html = html & debug_querystring
      html = html & "</td></tr></table>" & vbcrlf
      html = html & "<!-- end debugging code -->" & vbcrlf
   end if

   html = html & "</body>" & vbcrlf
   html = html & "</html>" & vbcrlf

   pagefooter = html
end sub
<%= pageFooter %>