简体   繁体   中英

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

I'm using spdlog to run logs for both managed and unmanaged code in Visual Studio. For that reason I've written shell classes that use spdlog under the hood.

However, I'm running into problems with my unit tests. My unit tests run in a single executable and so I need to stop and restart spdlog loggers and logfiles multiple times.

How do I do this? I'm using this code in a class to currently start instances of spdlog in a Windows DLL:

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;
    }

There actually is a single answer to this question which I got from the creator of spdlog.

From How to shutdown and restart in the same program? to shutdown:

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

Then you can go through the creation process above again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM