查找谁在调用该方法

发布于 2024-08-29 12:14:19 字数 228 浏览 4 评论 0原文

我想以某种方式找出哪个 CFC 正在调用我的方法。

我有一个记录 CFC,它被许多不同的 CFC 调用。在此日志记录 CFC 上,需要存储哪个 CFC 调用日志。

虽然我可以简单地将 CFC 名称作为参数传递给 log.cfc,但我发现这是一项重复性任务,如果我能以某种方式找出“谁”在 log.cfc 上调用该方法,则可能没有

必要有什么程序化的方式来实现这一点吗?

提前致谢

I'd like to somehow find out which CFC is calling my method.

I have a logging CFC which is called by many different CFC's. On this logging CFC there's a need to store which CFC called for the log.

Whilst I could simply pass the CFC name as an argument to my log.cfc, I find this to be a repetitive task, that might not be necessary, if I somehow could find out "who's" calling the method on log.cfc

Is there any programmatic way of achieving this?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太阳男子 2024-09-05 12:14:19

更新:正如Richard Tingle的回答指出的,从CF10开始你可以使用CallStackGet(),这比抛出一个假人更好例外。


原始答案:最简单的方法是抛出一个虚拟异常并立即捕获它。但这有一个缺点,就是在调试输出中出现虚拟异常。对我来说,这是一个破坏性的事情,所以我编写了以下代码(基于 cflib 上的此代码)。我想创建一个类似于 cfcatch 对象的对象,以便我可以在需要 cfcatch 对象的地方使用它。

注意:您可能需要稍微调整此代码才能使其在 CF8 或更早版本中运行。我认为 CF9 之前不支持创建对象的 {...} 语法。

StackTrace = { 
  Type= 'StackTrace',
  Detail= '',
  Message= 'This is not a real exception. It is only used to generate debugging information.',
  TagContext= ArrayNew(1)
};
j = CreateObject("java","java.lang.Thread").currentThread().getStackTrace();

for (i=1; i LTE ArrayLen(j); i++)
{
  if(REFindNoCase("\.cf[cm]$", j[i].getFileName())) {
    ArrayAppend(StackTrace.TagContext, {
      Line= j[i].getLineNumber(),
      Column= 0,
      Template= j[i].getFileName()
    });
  }
}

Update: As Richard Tingle's answer points out, since CF10 you can use CallStackGet(), which is better than throwing a dummy exception.


Original answer: The easiest way is to throw a dummy exception and immediately catch it. But this has the downside of making a dummy exception show up in your debug output. For me, this was a deal-breaker, so I wrote the following code (based off of this code on cflib). I wanted to create an object that is similar to a cfcatch object, so that I could use it in places that expected a cfcatch object.

Note: You may have to adjust this code a bit to make it work in CF8 or earlier. I don't think the {...} syntax for creating object was supported prior to CF9.

StackTrace = { 
  Type= 'StackTrace',
  Detail= '',
  Message= 'This is not a real exception. It is only used to generate debugging information.',
  TagContext= ArrayNew(1)
};
j = CreateObject("java","java.lang.Thread").currentThread().getStackTrace();

for (i=1; i LTE ArrayLen(j); i++)
{
  if(REFindNoCase("\.cf[cm]$", j[i].getFileName())) {
    ArrayAppend(StackTrace.TagContext, {
      Line= j[i].getLineNumber(),
      Column= 0,
      Template= j[i].getFileName()
    });
  }
}
灵芸 2024-09-05 12:14:19

从 ColdFusion 10 开始,现在有一个函数可以执行此操作 callStackGet()

例如,以下代码会将堆栈跟踪转储到 D:/web/cfdump.txt

<cfdump var="#callStackGet()#" output="D:/web/cfdump.txt">

From ColdFusion 10 there is now a function to do this callStackGet()

For example the following code will dump the stack trace to D:/web/cfdump.txt

<cfdump var="#callStackGet()#" output="D:/web/cfdump.txt">
泡沫很甜 2024-09-05 12:14:19

我不知道有什么方法可以直接执行您所要求的操作,也许其他人会这样做。

但是,我相信您可以获取堆栈跟踪并创建一个函数来解析最后一个方法调用。

cflib 上的此函数将为您提供堆栈跟踪。

I don't know of a method to do directly what you are asking, maybe someone else does.

However, I believe you could get the stack trace and create a function to parse the last method call.

This function on cflib will get you the stack trace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文