Реализовать функцию вариативного журнала, которая автоматически получает имя вызывающего абонента [дубликат]C++

Программы на C++. Форум разработчиков
Гость
Реализовать функцию вариативного журнала, которая автоматически получает имя вызывающего абонента [дубликат]

Сообщение Гость »


I'm using msvc, now I need a logger function that can:
  • acquire the caller's name automatically
  • can receive variadic args just like fmt::format or std::format
I tried the following:
  • use macro, it works, but it's not easy to encapsulate into a class.
  • use stacktrace library. these libraries requires pdb file but I just need embedded name strings.
  • use

    Код: Выделить всё

    std::source_location::current()
    , I tried to use both

    Код: Выделить всё

    std::source_location
    and template argument pack

    Код: Выделить всё

    Args&&... args
    like that:

Код: Выделить всё

#include 
#include 
#include 

template
void log(const char* format_str, Args&&... args,
const std::source_location& location = std::source_location::current()) {
std::string func_name = location.function_name();
std::string message = std::format(format_str, std::forward(args)...);
std::cout 

Источник: [url]https://stackoverflow.com/questions/78132137/implement-variadic-log-function-that-acquire-callers-name-automatically[/url]

Вернуться в «C++»