我正在使用 spdlog 为 Visual Studio 中的托管和非托管代码运行日志。出于这个原因,我编写了在后台使用 spdlog 的 shell 类。
但是,我的单元测试遇到了问题。我的单元测试在单个可执行文件中运行,因此我需要多次停止并重新启动 spdlog 记录器和日志文件。
我该怎么做?我在一个类中使用此代码来当前在 Windows DLL 中启动 spdlog 的实例:
private:
API static std::mutex _mutex;
API static std::shared_ptr<spdlog::logger> _instance;
static std::shared_ptr<spdlog::logger> Instance()
{
std::unique_lock<std::mutex> lck(_mutex, std::defer_lock);
lck.lock();
if (_instance == nullptr)
{
_instance = spdlog::rotating_logger_mt("Logger", "EXO", 1024 * 1024 * 5, 4, true);
}
lck.unlock();
return _instance;
}
最佳答案
我从 spdlog 的创建者那里得到了这个答案。
来自 How to shutdown and restart in the same program?关闭:
void Shutdown()
{
spdlog::drop("Logger");
delete _instance;
}
然后就可以重新执行上面的创建过程了。
关于c++ - 如何在同一个程序中多次启动和关闭 spdlog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37356743/