如何在同一程序中多次启动和关闭 spdlog

How to start and shutdown spdlog multiple times in the same program?

本文关键字:启动 spdlog 程序      更新时间:2023-10-16

我正在使用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的创建者那里得到了这个答案。

从如何在同一程序中关机和重新启动?到关机:

void Shutdown()
{
    spdlog::drop("Logger");
    delete _instance;
}

然后,您可以再次完成上面的创建过程。