I took a slightly different approach and just defined a property that sets the __stack__ variable by intentionally throwing an error, from this we can get the file name, line number (and many others, such as callers and their line numbers, if you choose to implement them) .
Instead of setting up the log function, I also set it as a variable, but registering when I set it. This will be the location where the LOG is installed, not where it was defined.
Its as simple as LOG = "my message." You can then use it later as a variable to get the location of your last debug using alert(LOG)
/*@const*/ //for closure-compiler DEBUG=2 // 0=off, 1=msg:file:line:column, 2=msg:stack-trace if(DEBUG){ /*@const @constructor*/ Object.defineProperty(window,'__stack__',{get:function(){ try{_ფ_()}catch(e){return e.stack.split(":")} }}) /*@const @constructor*/ Object.defineProperty(window,'__file__',{get:function(){ var s=__stack__,l=s.length return (isNaN(s[l-2]))?s[l-2]:s[l-3] }}) /*@const @constructor*/ Object.defineProperty(window,'__line__',{get:function(){ var s=__stack__,l=s.length return (isNaN(s[l-2]))?s[l-1]:s[l-2] }}) /*@const @constructor*/ Object.defineProperty(window,'__col__',{get:function(){ var s=__stack__,l=s.length return (isNaN(s[l-2]))?"NA":s[l-1] }}) /*@const @constructor*/ Object.defineProperty(window,'LOG',{ get:function(){return out}, set:function(msg){if(DEBUG>1)out=msg+"\t-\t"+__stack__ else out=msg+" in file:"+__file__+" @ Line:"+__line__+", Column:"+__col__ console.log(out)} }) }//end if(DEBUG)
source share