1

In Node.js, I'm trying to get the line number of each function that is called in a script. Is there any way to generate a summary of each function that is called in a script? I'd like to get a list of line numbers that are called by each function in the script, so that I can comment out one of the function calls (which I'm still trying to find.)

An example:

function printStuff(){
    console.log("This is an example.");
}
printStuff();
printStuff();

Now I'd like to get a summary of all the events in this script (without modifying the script). It might look something like this:

calling printStuff at line 4
calling console.log at line 2
calling printStuff at line 5
calling console.log at line 2

Are there any testing/debugging tools that make it possible to do this?

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • I've written a script that's over 2000 lines long, so I need to find the line number of every function call that is evaluated in the script. – Anderson Green Oct 20 '12 at 05:39
  • Is this relevant, by any chance? http://stackoverflow.com/questions/10182387/any-tools-to-draw-call-graphs-for-given-javascript – Anderson Green Oct 20 '12 at 05:57

1 Answers1

4

Not EXACTLY what you are looking for, I guess, but you should be able to modify it to your needs:

Object.defineProperty(global, '__stack', {
  get: function(){
    var orig = Error.prepareStackTrace;
    Error.prepareStackTrace = function(_, stack){ return stack; };
    var err = new Error;
    Error.captureStackTrace(err, arguments.callee);
    var stack = err.stack;
    Error.prepareStackTrace = orig;
    return stack;
  }
});

Object.defineProperty(global, '__line', {
  get: function(){
    return __stack[1].getLineNumber();
  }
});

console.log("I am running from line " + __line);

It was discussed here

Community
  • 1
  • 1
Werner Kvalem Vesterås
  • 10,226
  • 5
  • 43
  • 50
  • How can I modify this script so that it prints the line number of every function that runs in the script? – Anderson Green Oct 20 '12 at 17:53
  • Here it is on jsfiddle: http://jsfiddle.net/jarble/dG5Q2/ I get the following error message when running it: `Uncaught ReferenceError: global is not defined`. – Anderson Green Jan 18 '13 at 01:04
  • This script runs just fine in node.js, however. Would it be possible to get it to work in the browser as well? – Anderson Green Jan 18 '13 at 01:08