Click here to Skip to main content
15,884,062 members
Articles / Desktop Programming / ATL

Function Call Tracing in JScript

Rate me:
Please Sign up or sign in to vote.
4.58/5 (18 votes)
3 Jul 2007CPOL15 min read 53.8K   6.4K   49  
Comprehensive JScript function call tracing without code modification.
/* Examples of user API */

function setButtonText() 
{
	// The current state of trace is used to set button text
	// appropriately
	var bState = __JSD.Trace;
	
	if (bState)
		document.all("oTrace").innerText = "Turn Off";
	else
	{
		__JSD.Reset();
		document.all("oTrace").innerText = "Turn On";
	}
}

// A simple 'class' used below...
function Value(x)
{
	this.m_X = x;
	this.reportValue = Value.reportValue;
}

// ...to show how _JSD_NAME_ directive gives a name to an anonymous 
// function
Value.reportValue = function() /* _JSD_NAME_ Value::reportValue */
{
	// TraceStack reports call stack to this point
	__JSD.TraceStack(arguments);
	
	__JSD.TraceText("Current value is " + this.m_X);
}

// When a function declares formal arguments these are reported - see the trace
// for "formalArg"
function doFlip(formalArg) 
{
	try		
	{
		document.all("oFlip").style.backgroundColor = "yellow";
		foobar();
	}
	catch(e)
	{
		alert(e.description);
	}
}

// If no formal arguments are defined but they are passed to the function
// these are listed as "arguments[x]".  See the trace for doFlop()
function doFlop()
{
	try		
	{
		document.all("oFlip").style.backgroundColor = "orange";
		var X = new Value(123);
		X.reportValue();
	}
	catch(e)
	{
		alert(e.description);
	}
}

function doOnOff()
{
	var bState = __JSD.Trace;

	if (bState)
		__JSD.TraceText("turning trace off...");

	__JSD.Trace = bState ? false : true;

	if (! bState)
		__JSD.TraceText("...its back on now.  Now the following call appears...");
	
	setButtonText();
}

// Use no trace or the exit message is recorded after this fn clears the log
// Note use of @JSD conditional compilation flag which is added for any instrumented script
function clearTrace() /* _JSD_NO_TRACE_ */
{
	@if (@JSD)
		__JSD.ClearTrace();
	@else
		alert("JSD needs to be used implictly for this to work");
	@end
}

// Explicit use of the _JSD_TRACE_ or _JSD_NO_TRACE_ directive means the functionn
// (or file if declared at the top of the file) is always traced/not traced
// regardless of the global settings.

// Turn off tracing of this function...
function foobar(myarg) /* _JSD_NO_TRACE_ */
{
	// ... and explicity uas a 'name' to override the default...
	__JSD.TraceFn(arguments,"Test.js::foobar");

	try 
	{
		__undefined__.bar();
	} 
	catch(e)
	{
		__JSD.TraceError(e.description);
	}

	// ...and for completeness need to add our own exit statement
	__JSD.TraceFnExit(arguments,"Test.js::foobar");
	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions