iriscouch / traceback

Easy access to the call stack, for Node.js
Apache License 2.0
44 stars 6 forks source link

Traceback: easy access to the call stack, for Node.js

Writing a Node app? Need to know the function backtrace? Don't want to compile C++ code? Use Traceback.

Traceback provides a normal JavaScript array of the execution stack frames. You can see function names, line numbers, and other useful stuff.

Traceback is available from NPM.

$ npm install traceback

Example

example.js

var traceback = require('../traceback');

function start() { first() }
function first() { second() }
var second = function() { last() }

function last() {
  var stack = traceback();
  console.log('I am ' + stack[0].name + ' from file ' + stack[0].file)

  for(var i = 1; i <= 3; i++)
    console.log('  ' + i + ' above me: ' + stack[i].name + ' at line ' + stack[i].line);
}

start();

Output:

I am last from file example.js
  1 above me: second at line 5
  2 above me: first at line 4
  3 above me: start at line 3

Usage

Simply calling traceback() gives you the stack, with the current function in position 0.

Stack frame objects have normal V8 CallSite objects as prototypes. All those methods will work. You can also call traceback.raw() to get the exact stack made by V8.

But traceback()'s stack frame objects have convenient attribute names:

They also work correctly in JSON.stringify().

Tests

Tests use node-tap. If you clone this Git repository, tap is included.

$ tap test
ok test/api.js ...................................... 286/286
ok test/fail.js ....................................... 35/35
ok test/format.js ....................................... 6/6
ok test/readme.js ....................................... 1/1
total ............................................... 332/332

ok

License

Apache 2.0