Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@jeffcogswell jeffcogswell commented Dec 7, 2015

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