Get a line number?

2 views
Skip to first unread message

Tony Zakula

unread,
Dec 2, 2010, 9:18:00 PM12/2/10
to mynajs-general
Is there an easy way to get the line number of the script which is
executing when you call a function?

Example - I want to call my customized logger, and I want to pass the
line number and the name of the function to the logger. I do not mind
hard coding the function name as that would not change, but I would
like not to hand code the line number.

Thanks!

Tony Z

Mark Porter

unread,
Dec 3, 2010, 10:14:51 AM12/3/10
to mynajs-...@googlegroups.com
This would be nice,but the only way to get the line number is to throw
an exception and examine the stack trace. To add insult to injury,
there are several places and formats for the stack trace depending on
the kind of exception. Here is an example that will probably work, but
it can seriously slow down your app:

function getStackLine(){
try{
throw new Error("lineNum")
} catch(e){

var line= e.stack
.split(/\n/).filter(function(e){
return e.length;
})[1]
return {
file:line.listBefore(":").listAfter(" "),
line:parseInt(line.listLast(":"))
}
}
}
----------------------------------------------------------
Mark Porter

Myna JavaScript Application Server
Easy web development with server-side JavaScript
http://www.mynajs.org

> --
> You received this message because you are subscribed to the Google Groups "MynaJS-General" group.
> To post to this group, send email to mynajs-...@googlegroups.com.
> To unsubscribe from this group, send email to mynajs-genera...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mynajs-general?hl=en.
>
>

Tony Zakula

unread,
Dec 3, 2010, 10:20:23 AM12/3/10
to mynajs-...@googlegroups.com
Okay, thanks. I knew it was probably not possible, but I thought I
would ask. You never know. :-)

I suppose just specifying the class and method or something would
narrow it down enough as long as the code is concise, which I try to
do anyway.

Thanks!

Tony

Reply all
Reply to author
Forward
0 new messages