boostboost-logboost-logging

Boost::log::string_literal construction issue


I'm trying unsuccessfully to use the macro BOOST_LOG_NAMED_SCOPE with no hard-coding (e.g no BOOST_LOG_NAMED_SCOPE("bla"), but BOOST_LOG_NAMED_SCOPE(some_variable); this macro uses inside a boost::log::string_literal that have no C'tor for std::string or char*. The only thing it accepts is const char[] (NOT const char*) - which doesn't help me at all because I can't hard-code it - this value must be retrieved from a function.

So, I need to find a way to construct boost::log::string_literal with std::string or char*, or somehow to edit const char[]... (I tried also to create a char[] and cast it to const char[], but failed)


Solution

  • erm. "String literal" says it all. It will only work with literals, and those are by definition (in the standard, that is) char const[].

    This design decision (to make names known and fixed at compiletime) can have several reasons. Conceptually, it will make logging "predictable" (if scope names vary, how are you going to interpret logs?), and might have to do with performance (it could even be used to statically disable some logging based on the scope name, but other things like output formatting could see improved performance depending on the compiler).

    So, you can wish to not hardcode things, but you cannot wish to use the macros which require a string literal while doing so.

    Now, you can use extern variables of the appropriate type (extern char const some_scope_name[]) which would at least give you the opportunity to define the value in another translation unit, I suppose.