Javascript tracer function

This is a handy little function that can be used for debugging in Drupal. SInce console.log can sometimes break some browsers without a console, this at least makes sure the console.log is only run when you are logged into the site. It also has a nice feature of including the name of the function that called it.

function tellme (outputMessage) {
    //check to see if logged in
    if (jQuery("body.logged-in").length) {
        // person is logged in so it is safe to output debug
        console.log('Called from', arguments.callee.caller.name, ': ', outputMessage);
    }
}

Anywhere in your js where you need a tracer, simply do something like

tellme ('I was clicked');

// or

tellme ('I ran');

If your debugging requires seeing a set of variables at each tracer, you can this

function tellme (outputMessage) {
    var oVariables = new Object();
    oVariables["start_date"] = start_date;
    oVariables["supertag_id"] = supertag_id;
    oVariables["supertag_group_id"] = supertag_group_id;
    oVariables["totalResults"] = totalResults;
//check to see if logged in
if (jQuery("body.logged-in").length) {
    // person is logged in so it is safe to output debug
    console.log('Called from', arguments.callee.caller.name, ': ', outputMessage, oVariables);
}
}

This will output the variables as an object that can be expanded for inspection.

When you are ready to move live, simply comment out the one console.log and you are safe to leave your tracers in place if you think they may be needed for future development.