Can I access the name of the calling function programmatically?

I hope to add a little functionality to the log file system for the project I'm working on. For my calls, LogError()I would like to enable the function in which the error occurred. I am wondering if there is a way to access the name of a function called LogError()so that I can programmatically access this information in order to add this to the log.

For instance:

bool Engine::GraphicsManager::Initialize(const HWND i_hWindow_main)
{
    if ( !InitializeWindow( i_hWindow_main ) )
    {
        Engine::LogManager::Instance().LogError(L"GraphicsManager::Initialize - Unable to initialize graphics window");
        return false;
    }
    Engine::LogManager::Instance().LogMessage(L"Graphics window initialized successfully");

    /* SNIP */

    initialized = true;
    return true;
}

In the above example, I would like to LogError()be able to determine that it was called from GraphicsManager::Initialize()and output (at least part) this function name instead of manually putting it in all directions.

EDIT: , LogError() ( ) vfwprintf_s(), . " ", , (, , ).

/?

!

+3
3

__FUNCTION__: http://msdn.microsoft.com/en-us/library/b0084kay%28v=vs.80%29.aspx

, ...log...() ...log...(__FUNCTION__).

+4

C/++, :

__FILE__ __LINE__

+1

EDIT:

//support macros
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define STRINGIZE(x) #x
#define __WFILE__ WIDEN(__FILE__)
#define __WFUNCTION__ WIDEN(__FUNCTION__)
#define __WLINE__ WIDEN( STRINGIZE(__LINE__) )
// logging macro
#define LOG(msg) Engine::LogManager::Instance().LogError(__WFILE__ L"::" __WFUNCTION__ L":" __WLINE__ L" - " msg)

:

if( error ) { LOG(L"Error!"); }

File.cpp::Function:Line - Error!

C-. "aa" "bb" → "aabb" , L "astring". __FILE__, __FUNCTION__ __LINE__, , . __LINE__ STRINGIZE. - BOOST_PP_STRINGIZE, boost .

0

Source: https://habr.com/ru/post/1788747/


All Articles