spdlog 的包装器不适用于 const char*

发布于 2025-01-15 09:40:15 字数 1657 浏览 2 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

开始看清了 2025-01-22 09:40:15

由于某种原因,带有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文