查找函数调用的源代码行 [英] Finding the Source Line of a function call

查看:132
本文介绍了查找函数调用的源代码行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个自定义日志记录实用程序,它显示了日志消息和日期时间。我想在源代码中添加一个调用该函数的行号。

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.

有没有办法确定特定javascript函数的哪一行HTML源被触发?

Is there a way to determine which line of the HTML source a particular javascript function was fired?

推荐答案

编写了一个日志库( log4javascript )我自己也考虑过同样的问题,这是我的想法:

Having written a logging library (log4javascript) myself, I've considered this same problem and here are my thoughts:

问题是为了得到你想要的信息,您需要在相关行上创建的错误对象。在日志记录实用程序中创建错误将只直接为您提供日志记录实用程序代码中特定行的文件名和行号,而不是用于进行日志记录的代码行呼叫。我能想到的唯一解决方法是解析错误堆栈属性(或消息属性在Opera中),它有几个问题:

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:


  • 堆栈跟踪仅在Mozilla中可用,最近的WebKit和Opera浏览器

  • 堆栈跟踪是一个不同浏览器的字符串,可能会在未来的浏览器中再次更改格式,从而破坏解析代码

  • 抛出错误并为每次日志调用解析其堆栈跟踪将增加显着的性能开销。

  • 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.

出于log4javascript的目的,我认为它不值得实现,但为了您自己的需要,您可能会认为这是值得的。

For the purposes of log4javascript, I decided it wasn't worth implementing, but for your own needs you may decide it's worthwhile.

这篇关于查找函数调用的源代码行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆