How to Get the Caller's Method Name in the Called Method

How to get the caller's method name in the called method?

inspect.getframeinfo and other related functions in inspect can help:

>>> import inspect
>>> def f1(): f2()
...
>>> def f2():
... curframe = inspect.currentframe()
... calframe = inspect.getouterframes(curframe, 2)
... print('caller name:', calframe[1][3])
...
>>> f1()
caller name: f1

this introspection is intended to help debugging and development; it's not advisable to rely on it for production-functionality purposes.

How do I get the caller's method name

I think debug_backtrace could do the trick.

It gives you a backtrace of a function call. Observe this result then you'll have to figure out how to grab the function name you want. But the information is there.

<?php 
class father {
public function db_select(){
echo debug_backtrace()[1]['function'];
print_r(debug_backtrace());
}
}

class plugin extends father {
public function select_plugin(){
$query = $this->db_select();
}
}

?>

Getting the caller function name inside another function in Python?

You can use the inspect module to get the info you want. Its stack method returns a list of frame records.

  • For Python 2 each frame record is a list. The third element in each record is the caller name. What you want is this:

    >>> import inspect
    >>> def f():
    ... print inspect.stack()[1][3]
    ...
    >>> def g():
    ... f()
    ...
    >>> g()
    g

  • For Python 3.5+, each frame record is a named tuple so you need to replace

    print inspect.stack()[1][3]

    with

    print(inspect.stack()[1].function)

    on the above code.

How can we know the caller function's name?

There's nothing you can do only in a.

However, with a simple standard macro trick, you can achieve what you want, IIUC showing the name of the caller.

void a()
{
/* Your code */
}

void a_special( char const * caller_name )
{
printf( "a was called from %s", caller_name );
a();
}

#define a() a_special(__func__)

void b()
{
a();
}

Get the calling function name from the called function

new StackFrame(1, true).GetMethod().Name

Note that in release builds the compiler might inline the method being called, in which case the above code would return the caller of the caller, so to be safe you should decorate your method with:

[MethodImpl(MethodImplOptions.NoInlining)]

Retrieving the calling method name from within a method

I don't think it can be done without tracing the stack. However, it's fairly simple to do that:

StackTrace stackTrace = new StackTrace();
MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
Console.WriteLine(methodBase.Name); // e.g.

However, I think you really have to stop and ask yourself if this is necessary.

How do I find the caller of a method using stacktrace or reflection?

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()

According to the Javadocs:

The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.

A StackTraceElement has getClassName(), getFileName(), getLineNumber() and getMethodName().

You will have to experiment to determine which index you want
(probably stackTraceElements[1] or [2]).



Related Topics



Leave a reply



Submit