Wrapper for spdlog does not work with const char*

1.2k Views Asked by At

I'm writing a wrapper for spdlog, so that I can change spdlog for a different lib if there will be a need. My problem is that I cannot pass to spdlof::log a const char*. I'm receiving an error saying that it 'message' is not a constant expression. I checked function signature and it takes fmt::format_string<Args...>. Tried to convert message into that type, but every time I received an error.

Wrapper code:

template<typename T>
static void log(MessageLevel level, const T &value) {
    if (level > activeLogLevel) return;
    spdlog::log(static_cast<spdlog::level::level_enum>(level), value);
}
template <typename... Types>
static void log(MessageLevel level, const char* const message, const Types &...params) {
    if (level > activeLogLevel) return;
    spdlog::log(static_cast<spdlog::level::level_enum>(level), message, params...);
}

Code usage:

Logger::log(logger::MessageLevel::M_INFO, "test {0} {1}\r\n", device->name, 
communicationManager.isConnected());
auto response = Communication::Message{};
Logger::log(logger::MessageLevel::M_INFO,communicationManager.send(response));
Logger::log(logger::MessageLevel::M_INFO, sizeof(response));

If I call the function with only one argument, it works.

Whole error message:

'static void logger::Logger::log(logger::MessageLevel, const char*, const Types& ...) [with 
Types = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
bool}]':
in 'constexpr' expansion of 'fmt::v8::basic_format_string<char, const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const bool&>(message)'
error: 'message' is not a constant expression
spdlog::log(static_cast<spdlog::level::level_enum>(level), message, params...);
1

There are 1 best solutions below

0
On

For some reason c++ 20 with GNU compiler was producing this error. When I downgraded the c++ version to c++ 17 in CMake the problem went away.