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

James A Mohler
  • 10,562
  • 14
  • 41
  • 65
Marcos Placona
  • 20,531
  • 11
  • 62
  • 92

4 Answers4

9

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()
    });
  }
}
Kip
  • 99,109
  • 82
  • 222
  • 258
  • That's a very elegant way. Thanks for taht – Marcos Placona Apr 18 '11 at 08:44
  • Your link is broken. When I search `CFLib` today, I find one UDF that operates using throwables: https://cflib.org/udf/getStackTrace. Using `currentThread().getStackTrace()` seems to be a very clean approach. – Bernhard Döbler Apr 12 '19 at 10:03
  • @BernhardDöbler Since CF10 you can just use CallStackGet() https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-c-d/CallStackGet.html – Kip Apr 14 '19 at 02:40
  • @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. – Bernhard Döbler Apr 14 '19 at 18:57
4

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">
Matt Busche
  • 13,789
  • 5
  • 34
  • 58
Richard Tingle
  • 15,728
  • 5
  • 47
  • 71
1

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

ale
  • 6,267
  • 6
  • 54
  • 64
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.

derivation
  • 3,746
  • 3
  • 24
  • 23