Library xbDebug — Cross Browser Debugging API with support for Gecko based browsers, Internet Explorer 4+, Opera 5+, Netscape Navigator 4.x.
class xbDebug { // private: Array stack; Object debugwindow; Object execprofile; // open a Debug Window // automatically called from dump() void open(); // public // on == true, output dump messages Boolean on; // save current debugging state // turn debuggin state on void push(); // set debugging state to previous state void pop(); // write a message to the Debug Window void dump(); // close a Debug Window // automatically called when page unloads void close(); } void xbDebugTraceFunction(String xbDebugScopeName, String funcname) void xbDebugTraceObject(String xbDebugScopeName, String funcname) String xbDebugGetFunctionName(Function funcref) void xbDebugCreateFunctionWrapper(String xbDebugScopeName, String funcname, Function precall, Function postcall) void xbDebugTraceBefore(String xbDebugScopeName, String funcname, Arguments funcarguments) void xbDebugTraceAfter(String xbDebugScopeName, String funcname, Arguments funcarguments, Object rv)
xbDebug.js is a JavaScript library that provide the ability to output debug messages, trace function and Object method calls and provide performance profiling information. It is supported by any JavaScript 1.3 implementation that supports the ECMAScript Standard 3rd Edition.
Example 1. Basic Example
<HTML> <HEAD> <TITLE>xbDebug Example</TITLE> <SCRIPT LANGUAGE="JavaScript" SRC="/lib/js/xbDebug.js"></SCRIPT> <SCRIPT LANGUAGE="JavaScript"> <!-- // define an object to be traced function World() { this.isTurning = false; } World.prototype.turn = function (on) { this.isTurning = on; return on;} // define a function to be traced function Hello(s) { var world = new World(); world.turn(true); alert('Hello ' + s); } // turn on debugging xbDEBUG.push(); if (xbDEBUG.on) { // set traces xbDebugTraceObject('window', 'World'); xbDebugTraceFunction('window', 'Hello'); } // turn off debugging xbDEBUG.pop(); xbDEBUG.dump('This will not be displayed'); function init() { // turn on debugging xbDEBUG.push(); // this will be traced Hello('World!'); // turn off debugging xbDEBUG.pop(); } //--> </SCRIPT> </HEAD> <BODY onload="init()"> <h1>xbDebug Example</h1> <p> View Source to see the code </p> </BODY> </HTML>
xbDebug is a Class that encapsulates the information required for debugging. A single instance of the xbDebug class named xbDEBUG is created when you include the xbDebug.js script. xbDEBUG.on is a boolean property that records whether debugging is current on (true) or off (false). The initial value of xbDEBUG.on is false.
The Debug state is maintained in a Stack so that you can locally turn on / off Debugging while not affecting the Debug status of other parts of a script.
Turn On Debugging. You can explicitly set xbDEBUG.on = true to turn debugging messages on if you only care about global debugging. call xbDEBUG.push() to push the current Debug state onto a Stack and to turn the current Debug state on.
Turn Off Debugging. You can explicitly set xbDEBUG.on = false to turn debugging messages on if you only care about global debugging. call xbDEBUG.pop() to set the current Debug state to the previous state.
Why use xbDEBUG.push()/xbDEBUG.pop() versus xbDEBUG.on. xbDEBUG.push() can be used to selectively turn debugging on for sections of code without turning it on everywhere in your scripts. xbDEBUG.pop() will return xbDEBUG.on to it's values prior to the previous xbDEBUG.push() call.
An example would be where you have xbDEBUG.dump calls scattered throughout your application and do not wish to see debug messages except in a specific function. In that function, you can call xbDEBUG.push() to save the current value of xbDEBUG.on and set xbDEBUG.on to true. Before the function returns, you can call xbDEBUG.pop() to return the value of xbDEBUG.on to it's value before your function was called. push() and pop() allow you to selectively turn dump messages on in any number of locations without automatically turning dump messages on everywhere.
Writing a message to the Debug Window. call xbDEBUG.dump('some message') to write a message to the Debug Window. If the Debug Window is not already open, it will be automatically opened.
Do not use xbDEBUG.open(). xbDEBUG.open is called automatically the first time you call xbDEBUG.dump(). If you explicitly call xbDEBUG.open, then there is a possibility in Navigator 4 that a race condition may occur between different windows, resulting in lost dump messages.
Trace each call of a specified function
Arguments:
String scopename is the name of the Object which contains the function to be traced.
String funcname is the name of the Function to be traced.
Returns: nothing
Trace each call of the methods of a specified Object
Arguments:
String scopename is the name of the Object which contains the function to be traced.
String funcname is the name of the Constructor for the Object of be traced.
Returns: nothing
Internal Function used by xbTraceFunction and xbTraceObject to return the name of a function from a refernce to the function object.
Arguments:
Function funcrec is a reference to the function whose name you wish to determine.
Returns: String name of the function
Internal Function used by xbTraceFunction and xbTraceObject
Arguments:
String scopename is the name of the Object which contains the function to be traced.
String funcname is the name of the Function to be traced.
Arguments funcarguments is the arguments object the function will be called with
Returns: nothing
Internal Function used by xbTraceFunction and xbTraceObject
Arguments:
String scopename is the name of the Object which contains the function to be traced.
String funcname is the name of the Function to be traced.
Arguments funcarguments is the arguments object the function will be called with
Object rv is the return value from the function being traced
Returns: nothing
Internal Function used by xbTraceFunction and xbTraceObject. This function replaces a function with a new function that executes a precall function, then the original function, followed by a postcall function.
xbDebugTraceFunction and xbDebugTraceObject use xbDebugCreateFunctionWrapper along with xbDebugTraceBefore and xbDebugTraceAfter to trace function and method calls.
Arguments:
String scopename is the name of the Object which contains the function to be traced.
String funcname is the name of the Function to be traced.
Function precall is the function to be called before the function being wrapped is called.
Function postcall is the function to be called after the function being wrapped is called.
Returns: nothing