Skip to content

Instantly share code, notes, and snippets.

@jeffcogswell
Created December 7, 2015 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffcogswell/0988d2cee68b7a6ab208 to your computer and use it in GitHub Desktop.
Save jeffcogswell/0988d2cee68b7a6ab208 to your computer and use it in GitHub Desktop.
Demo of simple way to get file/line in node.js
var path = require('path');
function getfileline(stacklevel) {
var index = stacklevel || 2;
var stacklist = (new Error().stack).split('\n')[index];
// regexps borrowed from "tracer" in npm
var stackReg = /at\s+(.*)\s+\((.*):(\d*):(\d*)\)/gi;
var stackReg2 = /at\s+()(.*):(\d*):(\d*)/gi;
var sp = stackReg.exec(stacklist) || stackReg2.exec(stacklist);
var result = {
func: sp[1],
filename: path.basename(sp[2]),
fullfile: sp[2],
path: path.dirname(sp[2]),
line: sp[3]
};
return result;
}
function mylog() {
var traceobj = getfileline(3);
console.log(traceobj);
}
function tryit() {
mylog();
}
tryit();
@jeffcogswell
Copy link
Author

Just a test... I'll probably pull stuff out into a small module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment