Search code examples
c++legacy

Understanding legacy C++ #define


I have some legacy C++ code that I am trying to understand a bit better. One issue I am getting confused with is this with a line like this:

#define LOG_TRACE_ERROR(s)  LOG_traceError( _T(__FILE__), __LINE__, s )

which is in a header file. I can see the LOG_TRACE_ERROR is what the code is calling and it passes it a string, and I can see that LOG_traceError is a function that actually does the work, so I assume this line is mapping the two different names for the function together? What is confusing me is why is the parameter list different (just a string for LOG_TRACE_ERROR and (_T(FILE), LINE, s ) for LOG_traceError). Also I cant find _FILE_ or _LINE_ or s defined anywhere so how does the program know what they are?


Solution

  • _FILE_ expands to the file name.

    _LINE_ expands to the line number.

    s is the parameter you pass to the macro.

    When you write:

    //file.cpp
    //...
    LOG_TRACE_ERROR("error here"); //line 13
    

    the preprocessor will transform it to:

    //file.cpp
    //...
    LOG_traceError( _T("file.cpp"), "13", "error here" );
    

    _T() is a macro related to UNICODE. If in a unicode environment, it will transform your string to a wchar_t*.