node.js - Is there a way to know from which file a function is called in nodejs? -


this question has answer here:

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

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -