Created
December 7, 2015 22:08
-
-
Save jeffcogswell/0988d2cee68b7a6ab208 to your computer and use it in GitHub Desktop.
Demo of simple way to get file/line in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a test... I'll probably pull stuff out into a small module.