__line_number__ and __filename__

Claude Pache claude.pache at gmail.com
Fri Aug 24 18:04:27 UTC 2018



> Le 24 août 2018 à 05:55, J Decker <d3ck0r at gmail.com> a écrit :
> 
> On Thu, Aug 23, 2018 at 5:26 PM Aaron Gray <aaronngray.lists at gmail.com <mailto:aaronngray.lists at gmail.com>> wrote:
> I am debugging existing code that I have modularized, and am class'izing that has unittests and it just would have been very useful to have this facility.
> In a  browser, console.log is usually associated with the file and line number anyway; which includes using devtools with node.... but it would be handy for logging.  with V8 there is console.trace; which spits out the stack trace too... before I discovered that I did a logging function like... 
> 
> (from  https://stackoverflow.com/questions/591857/how-can-i-get-a-javascript-stack-trace-when-i-throw-an-exception <https://stackoverflow.com/questions/591857/how-can-i-get-a-javascript-stack-trace-when-i-throw-an-exception> )
> function stackTrace() { var err = new Error(); return err.stack; }  // parse stack to get frame-1 online
> 
> // or maybe just frame-1...
> function stackTrace() { var err = new Error(); return err.stack.split( "\n" )[1]; }  
> 
> ---
> function stacktrace() { 
>   function st2(f) {
>     return !f ? [] : 
>         st2(f.caller).concat([f.toString().split('(')[0].substring(9) + '(' + f.arguments.join(',') + ')']);
>   }
>   return st2(arguments.callee.caller);
> }
> 
>  EDIT 2 (2017) :
> 
> In all modern browsers you can simply call: console.trace(); (MDN Reference)
> ---
> 
> Although I do still miss just being able to get __FILE__ and __LINE__ 


See also:

https://github.com/tc39/proposal-error-stacks <https://github.com/tc39/proposal-error-stacks>

If/when that proposal is implemented, you'll have a simple way to get a *structured* representation of the trace:

```js
System.getTrace(new Error)
```

from which you can much more easily extract line number, filename, etc.

—Claude

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180824/c7d5fc22/attachment.html>


More information about the es-discuss mailing list