Search code examples
c++boostg++boost-log

Boost Log: variable is not a member of ‘boost::log::v2s_mt_posix::trivial’


I'd like to switch over different boost::log severity levels using a single variable to control lots of BOOST_TRIVIAL_LOG(lvl) calls.

// Class header
const boost::log::trivial::severity_level _LVL_DEBUG;

// Class ctor initialize list
#ifdef FOO_MACRO
        _LVL_DEBUG(boost::log::trivial::severity_level::info)
#else
        _LVL_DEBUG(boost::log::trivial::severity_level::debug)
#endif

// Class method
BOOST_LOG_TRIVIAL(_LVL_DEBUG) << "Foo bar";

But when g++ compiling I obtain

error: ‘_LVL_DEBUG’ is not a member of ‘boost::log::v2s_mt_posix::trivial’

I'd like to know where I'm missing the point.

Edit 1

Same output with boost::log::trivial::info (or other level ofc)


Solution

  • It's not clear what you're trying to do. BOOST_LOG_TRIVIAL(x) expands the severity level x to boost::log::trivial::severity_level::x, where severity_level is an enum and x is supposed to be a value of that enum.

    If you want to control severity levels in compile time then all you have to do is to define your macro like this:

    #ifdef FOO_MACRO
    #define _LVL_DEBUG info
    #else
    #define _LVL_DEBUG debug
    #endif
    

    Then you use it in logging statements like this:

    BOOST_LOG_TRIVIAL(_LVL_DEBUG) << "Foo bar";
    

    If you want runtime control then you can no longer use BOOST_LOG_TRIVIAL and have to use BOOST_LOG_SEV or other macros that take the severity level directly. This also means you would have to create and manage loggers yourself.