Constructing a Lightweight Generic Function Trace Mechanism for Javascript

21 Feb

(I’ve brought together the ideas for this concept from various places – thanks to this answer on Stack Overflow and in turn this page for information about arguments.callee).

Oftentimes, when you’re constructing a complex Javascript application, it can be very useful to add tracing to it so that you can track the execution flow – when methods are called, and with which arguments. This is especially important when trying to debug later on. The Javascript arguments array can be used to help do this. However, the arguments array isn’t a real array – it has some quirks. I’ve spent some time experimenting, and I’ve come up with a generic tracing function that you can use. Crucially, the line to invoke it is just copy and paste, no modification is needed. This means you can easily paste it throughout your code without tiresome pasting in of function names or arguments.

The generic trace function looks like this:

function trace(argumentsArray, calleeItem) {
    var functionName = calleeItem.match(/function\s+(\w+)\s*\(/)[1];
    console.debug("Entered function " + functionName + " with arguments", argumentsArray);
}

You can use it simply by inserting the following line into the header of each function you write:

trace(Array.prototype.slice.call(arguments), arguments.callee.toString());

That line looks complex, but it does not need to be modified for each function you write. The first parameter copies the arguments array into a real, mutable array that can be passed into the trace function. The second takes the callee (i.e. the function being traced) and passes it into the trace function (which then strips off the function prefix).

For example, you might write some code like this:

function sayHello(firstname, surname) {
    trace(Array.prototype.slice.call(arguments), arguments.callee.toString());

    console.info("Hello, " + firstname + " " + surname + "!");
}

sayHello("John", "Smith");

That would produce output in your console that looks like:

Entered function sayHello with arguments ["John", "Smith"]
Hello, John Smith!

Any suggestions for improvements to this idea gladly welcomed!

About these ads

2 Responses to “Constructing a Lightweight Generic Function Trace Mechanism for Javascript”

  1. Ben Fletcher 2011/02/22 at 14:36 #

    one thought i had was a config file, that lists key functions of all classes that we develop, then, at startup, this config would be parsed and all the functions would be dojo.connect’ed to with the method you wrote there

    that way, we can customise the config WAS-style (you know, where we can specify which modules to trace)… and keep the tracing function code in one place

    … you asked for it :-)

  2. giacomo 2012/07/22 at 08:54 #

    your trace function doesn’t work with anonimous functions …

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 62 other followers

%d bloggers like this: