0

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...);
Kajetanoss
  • 159
  • 1
  • 2
  • 8
  • Works well https://godbolt.org/z/cxnx919dr. Please post a [mcve]. – 273K Mar 17 '22 at 16:18
  • I thing I found the problem. When I'm using IDE build-in compiler I'm having this error. When I built via console with different toolchain It builds and works like a charm. Thanks for you comment, it gave an idea to investigate :) – Kajetanoss Mar 17 '22 at 18:27
  • although this is an older question, it is better asked [here](https://stackoverflow.com/questions/76017748/mismatch-between-c17-and-c20-const-char-no-longer-a-constant-expression) and better answered [here](https://stackoverflow.com/questions/68675303/how-to-create-a-function-that-forwards-its-arguments-to-fmtformat-keeping-the). – jwm Jun 05 '23 at 23:18

1 Answers1

0

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.

Kajetanoss
  • 159
  • 1
  • 2
  • 8