Finding The Source Line Of A Function Call
I've built a custom logging utility which displays a Log Message and the DateTime. I would like to add the line number in the source code which called the function. Is there a way
Solution 1:
Having written a logging library (log4javascript) myself, I've considered this same problem and here are my thoughts:
The problem is that in order to get the information you want, you need an Error
object that was created on the line in question. Creating an Error
within your logging utility will only directly give you the filename and line number for the particular line in your logging utility code rather than for the line of code that made the logging call. The only way round this I can think of is parsing the stack
property of the Error
(or the message
property in Opera), which has several problems:
- the stack trace is only available in Mozilla, recent WebKit and Opera browsers
- the stack trace is a string that varies from browser to browser, and may change format again without notice in future browsers, thus breaking the parsing code
- throwing an
Error
and parsing its stack trace for every log call will add a significant performance overhead.
For the purposes of log4javascript, I decided it wasn't worth implementing, but for your own needs you may decide it's worthwhile.
Post a Comment for "Finding The Source Line Of A Function Call"