node.js - Is there a way to know from which file a function is called in nodejs? -
this question has answer here:
- nodejs: filename of caller function 5 answers
i'm writing module logger , want add prefix each time function dlog()
being called filename of file calling function. right have call each time this:
dlog(__filename + " log text");
is there way detect name of file calling function without this?
from nodejs: filename of caller function :
function _getcallerfile() { try { var err = new error(); var callerfile; var currentfile;
error.preparestacktrace = function (err, stack) { return stack; }; currentfile = err.stack.shift().getfilename(); while (err.stack.length) { callerfile = err.stack.shift().getfilename(); if(currentfile !== callerfile) return callerfile; } } catch (err) {} return undefined;
}
Comments
Post a Comment