spdlog 的包装器不适用于 const char*
我正在为 spdlog 编写一个包装器,以便在需要时可以将 spdlog 更改为不同的库。我的问题是我无法将 const char* 传递给 spdlof::log。我收到一条错误消息,指出“消息”不是常量表达式。我检查了函数签名,它需要 fmt::format_string
包装器代码:
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...);
}
代码用法:
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));
如果我仅使用一个参数调用该函数,它就会起作用。
整个错误消息:
'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...);
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...);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于某种原因,带有 GNU 编译器的 c++ 20 产生了此错误。当我在 CMake 中将 c++ 版本降级到 c++ 17 时,问题就消失了。
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.