c++ - 如何在同一个程序中多次启动和关闭 spdlog?

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

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

相关文章:

.net - C++/CLI 包装一个返回 std::shared_ptr 的函数

C++ 返回对模板的引用

c++ - Perl 中是否有类指针来访问成员变量和函数?

c++ - 向项目添加类后对 'main' 的 undefined reference

c++ - VirtualTreeView 在 C++ Builder 中完成 UnicodeString

perl - 在实用程序类中登录

python - 通过 HTTP 服务日志

kubernetes - Promtail 基本身份验证在 helm values.yaml 中使用 kubernetes secret

.net - 如何在 C++/CLI 应用程序中显示控制台?

c# - 能否将 C++ std::regex 表达式转换为 C# 表达式?