vendredi 26 juin 2020

Difficulty creating Spdlog asynchronous file logger

I am trying to make a logger for my application which doesn't run in the main thread. I am using a wrapper class, Logger, around spdlog. The class has a private static data member of type std::shared_ptr<spdlog::logger> which gets initialized in an init() function. I am able to successfully create a spdlog::sinks::basic_file_sink_mt but when I try to use spd::async_factory it won't compile.

Logger.h

class Logger {
public:
  static void init();
private:
  static std::shared_ptr<spdlog:::logger> my_logger;
}

Logger.cpp

std::shared_ptr<spdlog::logger> Logger::my_logger;

void Logger::init() {
  spdlog::sink_ptr my_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("myfile.log", true);
  my_logger = std::make_shared<spdlog::logger>("Application", my_sink);
}

This solution seems to work. It logs to the file, but the issue is that it is in the main thread. I try updating the logger to be asynchronous by changing the line std::make_shared<spdlog::sinks::basic_file_sink_mt> to be std::make_shared<spdlog::sinks::basic_file_sink_mt<spdlog::async_factory>> as follows:

void Logger::init() {
  spdlog::sink_ptr my_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt<spdlog::async_factory>>("myfile.log", true);
  my_logger = std::make_shared<spdlog::logger>("Application", my_sink);
}

When I try this I get compilation errors. What can I do to set my member variable to be an async logger?

Thanks for reading all of this, and thanks for the help.

Aucun commentaire:

Enregistrer un commentaire