How can we know the caller function's name?

后端 未结 10 1643
北海茫月
北海茫月 2020-12-02 10:26

In the C language, __FUNCTION__ can be used to get the current function\'s name. But if I define a function named a() and it is called

10条回答
  •  广开言路
    2020-12-02 11:16

    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();
    }
    

提交回复
热议问题