【问题标题】:How to start and shutdown spdlog multiple times in the same program?如何在同一个程序中多次启动和关闭spdlog?
【发布时间】:2016-09-18 07:29:57
【问题描述】:

我正在使用 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;
    }

【问题讨论】:

    标签: c++ logging c++-cli spdlog


    【解决方案1】:

    我从 spdlog 的创建者那里得到了这个问题的唯一答案。

    How to shutdown and restart in the same program? 到关机:

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

    然后你就可以再次进行上面的创建过程了。

    【讨论】:

    • 这很好,但如果您使用 shared_ptrs,则不需要 delete,因为单独的 drop 将删除 shared_ptr 引用,从而自动释放它。 SpdLog 现在建议:make_shared&lt;spdlog::async_logger&gt;(...)
    猜你喜欢
    • 1970-01-01
    • 2010-10-24
    • 2019-05-05
    • 1970-01-01
    • 2015-12-20
    • 2016-10-03
    • 2013-07-03
    • 1970-01-01
    相关资源
    最近更新 更多