Find who's calling the method
Asked Answered
A

4

11

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

Artamas answered 20/4, 2010 at 11:27 Comment(0)
A
10

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()
    });
  }
}
Ascend answered 15/4, 2011 at 19:14 Comment(4)
That's a very elegant way. Thanks for tahtArtamas
Your link is broken. When I search CFLib today, I find one UDF that operates using throwables: cflib.org/udf/getStackTrace. Using currentThread().getStackTrace() seems to be a very clean approach.Escalera
@BernhardDöbler Since CF10 you can just use CallStackGet() helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/…Ascend
@Kip. Thanks, I know CallStackGet(). I just had to deal with CF9 legacy code and wanted to maintain CF9 compatibility and was therefore looking to a CF9 version.Escalera
I
5

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">
Interosculate answered 21/11, 2014 at 15:59 Comment(0)
J
1

One kludgey way is to throw/catch a custom error and parse the stack trace. Here are some examples

Jestinejesting answered 20/4, 2010 at 13:40 Comment(0)
O
0

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.

Octachord answered 20/4, 2010 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.