Anonymous
Clangd: вызов постоянной функции
Сообщение
Anonymous » 06 окт 2025, 20:03
Я использую
spdlog с FMT :: format_String для сообщений журнала, и я также хочу захватить местоположение вызова, используя std :: source_location. Моя цель состоит в том, чтобы иметь простую функцию журнала, подобную следующему: < /p>
Код: Выделить всё
Logger::Error(std::source_location::current(),
"Error during Resize swap chain: {}", e.what());
< /code>
Моя функция шаблона выглядит следующим образом: < /p>
template
inline void Error(const std::source_location& loc,
fmt::format_string fmt,
Args&&... args)
{
Log(loc, fmt, spdlog::level::err, std::forward(args)...);
}
< /code>
Полный код: < /p>
#pragma once
#define FMT_UNICODE 0
#include
#include
#include
#include
#include
#include
#include
#include
namespace ARF::Logger {
struct Config {
bool async = false;
std::size_t queue_size = 8192;
std::size_t thread_count = 1;
std::string logger_name = "app";
spdlog::level::level_enum level = spdlog::level::debug;
bool to_console = true;
bool to_file = false;
std::string file_name = "logs/app.log";
std::size_t max_file_size = 1048576 * 5;
std::size_t max_files = 3;
std::string pattern = "[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] [%s:%# %!] %v";
spdlog::level::level_enum flush_level = spdlog::level::err;
};
inline void Init(const Config& cfg = {}) {
std::vector sinks;
if (cfg.to_console) {
auto console_sink = std::make_shared();
sinks.push_back(console_sink);
}
if (cfg.to_file) {
auto file_sink = std::make_shared(
cfg.file_name, cfg.max_file_size, cfg.max_files
);
sinks.push_back(file_sink);
}
if (cfg.async) {
spdlog::init_thread_pool(cfg.queue_size, cfg.thread_count);
auto async_logger = std::make_shared(
cfg.logger_name,
sinks.begin(), sinks.end(),
spdlog::thread_pool(),
spdlog::async_overflow_policy::block
);
async_logger->set_level(cfg.level);
async_logger->set_pattern(cfg.pattern);
spdlog::set_default_logger(async_logger);
}
else {
auto sync_logger = std::make_shared(
cfg.logger_name,
sinks.begin(), sinks.end()
);
sync_logger->set_level(cfg.level);
sync_logger->set_pattern(cfg.pattern);
spdlog::set_default_logger(sync_logger);
}
spdlog::flush_on(cfg.flush_level);
}
inline void SetLevel(spdlog::level::level_enum level) {
spdlog::default_logger()->set_level(level);
}
inline void Flush() {
spdlog::default_logger()->flush();
}
template
inline void Log(const std::source_location& loc, fmt::format_string fmt, spdlog::level::level_enum lvl, Args&&... args) {
spdlog::log(
spdlog::source_loc{ loc.file_name(), static_cast(loc.line()), loc.function_name() },
lvl,
fmt,
std::forward(args)...
);
}
template
inline void Trace(const std::source_location& loc, fmt::format_string fmt, Args&&... args) {
Log(loc, fmt, spdlog::level::trace, std::forward(args)...);
}
template
inline void Debug(const std::source_location& loc, fmt::format_string fmt, Args&&... args) {
Log(loc, fmt, spdlog::level::debug, std::forward(args)...);
}
template
inline void Info(const std::source_location& loc, fmt::format_string fmt, Args&&... args) {
Log(loc, fmt, spdlog::level::info, std::forward(args)...);
}
template
inline void Warn(const std::source_location& loc, fmt::format_string fmt, Args&&... args) {
Log(loc, fmt, spdlog::level::warn, std::forward(args)...);
}
template
inline void Error(const std::source_location& loc, fmt::format_string fmt, Args&&... args) {
Log(loc, fmt, spdlog::level::err, std::forward(args)...);
}
template
inline void Critical(const std::source_location& loc, fmt::format_string fmt, Args&&... args) {
Log(loc, fmt, spdlog::level::critical, std::forward(args)...);
}
}
< /code>
При компиляции с Clangd я получаю ошибку: < /p>
In template: call to consteval function 'fmt::basic_format_string::basic_format_string' is not a constant expression
Тем не менее, один и тот же код скомпилируется с MSVC.
среда :
spdlog = 1.15.1
clang-tidy в clion
msvc 2022 p> p>
msvc 2022 p>>
Подробнее здесь:
https://stackoverflow.com/questions/797 ... not-a-cons
1759770189
Anonymous
Я использую [b] spdlog [/b] с FMT :: format_String для сообщений журнала, и я также хочу захватить местоположение вызова, используя std :: source_location. Моя цель состоит в том, чтобы иметь простую функцию журнала, подобную следующему: < /p> [code]Logger::Error(std::source_location::current(), "Error during Resize swap chain: {}", e.what()); < /code> Моя функция шаблона выглядит следующим образом: < /p> template inline void Error(const std::source_location& loc, fmt::format_string fmt, Args&&... args) { Log(loc, fmt, spdlog::level::err, std::forward(args)...); } < /code> Полный код: < /p> #pragma once #define FMT_UNICODE 0 #include #include #include #include #include #include #include #include namespace ARF::Logger { struct Config { bool async = false; std::size_t queue_size = 8192; std::size_t thread_count = 1; std::string logger_name = "app"; spdlog::level::level_enum level = spdlog::level::debug; bool to_console = true; bool to_file = false; std::string file_name = "logs/app.log"; std::size_t max_file_size = 1048576 * 5; std::size_t max_files = 3; std::string pattern = "[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] [%s:%# %!] %v"; spdlog::level::level_enum flush_level = spdlog::level::err; }; inline void Init(const Config& cfg = {}) { std::vector sinks; if (cfg.to_console) { auto console_sink = std::make_shared(); sinks.push_back(console_sink); } if (cfg.to_file) { auto file_sink = std::make_shared( cfg.file_name, cfg.max_file_size, cfg.max_files ); sinks.push_back(file_sink); } if (cfg.async) { spdlog::init_thread_pool(cfg.queue_size, cfg.thread_count); auto async_logger = std::make_shared( cfg.logger_name, sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block ); async_logger->set_level(cfg.level); async_logger->set_pattern(cfg.pattern); spdlog::set_default_logger(async_logger); } else { auto sync_logger = std::make_shared( cfg.logger_name, sinks.begin(), sinks.end() ); sync_logger->set_level(cfg.level); sync_logger->set_pattern(cfg.pattern); spdlog::set_default_logger(sync_logger); } spdlog::flush_on(cfg.flush_level); } inline void SetLevel(spdlog::level::level_enum level) { spdlog::default_logger()->set_level(level); } inline void Flush() { spdlog::default_logger()->flush(); } template inline void Log(const std::source_location& loc, fmt::format_string fmt, spdlog::level::level_enum lvl, Args&&... args) { spdlog::log( spdlog::source_loc{ loc.file_name(), static_cast(loc.line()), loc.function_name() }, lvl, fmt, std::forward(args)... ); } template inline void Trace(const std::source_location& loc, fmt::format_string fmt, Args&&... args) { Log(loc, fmt, spdlog::level::trace, std::forward(args)...); } template inline void Debug(const std::source_location& loc, fmt::format_string fmt, Args&&... args) { Log(loc, fmt, spdlog::level::debug, std::forward(args)...); } template inline void Info(const std::source_location& loc, fmt::format_string fmt, Args&&... args) { Log(loc, fmt, spdlog::level::info, std::forward(args)...); } template inline void Warn(const std::source_location& loc, fmt::format_string fmt, Args&&... args) { Log(loc, fmt, spdlog::level::warn, std::forward(args)...); } template inline void Error(const std::source_location& loc, fmt::format_string fmt, Args&&... args) { Log(loc, fmt, spdlog::level::err, std::forward(args)...); } template inline void Critical(const std::source_location& loc, fmt::format_string fmt, Args&&... args) { Log(loc, fmt, spdlog::level::critical, std::forward(args)...); } } < /code> При компиляции с Clangd я получаю ошибку: < /p> In template: call to consteval function 'fmt::basic_format_string::basic_format_string' is not a constant expression [/code] Тем не менее, один и тот же код скомпилируется с MSVC. [b] среда [/b]: spdlog = 1.15.1 clang-tidy в clion msvc 2022 p> p> msvc 2022 p>> Подробнее здесь: [url]https://stackoverflow.com/questions/79749530/clangd-call-to-consteval-function-fmtbasic-format-string-is-not-a-cons[/url]