mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
32 lines
1.1 KiB
C++
32 lines
1.1 KiB
C++
#include "ILogCpp.h"
|
|
#include <memory>
|
|
std::shared_ptr<ILogCpp> &ILogCpp::GetInstance(std::shared_ptr<ILogCpp> *impl)
|
|
{
|
|
static std::shared_ptr<ILogCpp> instance = std::make_shared<ILogCpp>();
|
|
static bool instanceChanging = false;
|
|
if (impl && false == instanceChanging)
|
|
{
|
|
// Don't use std::mutex for runing faster.
|
|
// Sleep for difference thread to release instance.
|
|
instanceChanging = true;
|
|
// std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Don't sleep and make sure that start fast.
|
|
if (instance.use_count() == 1) // bug?
|
|
{
|
|
// LogInfo("Instance change succeed.\n");
|
|
// instance->UnInit();
|
|
// (*impl)->Init();
|
|
instance = *impl;
|
|
}
|
|
else
|
|
{
|
|
// LogError("[ error ] instance change failed, using by some one.\n");
|
|
}
|
|
instanceChanging = false;
|
|
}
|
|
if (instanceChanging)
|
|
{
|
|
static std::shared_ptr<ILogCpp> tmporaryInstance = std::make_shared<ILogCpp>();
|
|
return tmporaryInstance;
|
|
}
|
|
return instance;
|
|
} |