mirror of
https://github.com/mii443/libdatachannel.git
synced 2025-09-01 14:49:25 +00:00
Made the data storred in a log counter a shared pointer
This commit is contained in:
@ -18,25 +18,23 @@
|
|||||||
|
|
||||||
#include "logcounter.hpp"
|
#include "logcounter.hpp"
|
||||||
|
|
||||||
rtc::LogCounter::LogCounter(plog::Severity severity, const std::string &text, std::chrono::seconds duration) :
|
rtc::LogCounter::LogCounter(plog::Severity severity, const std::string &text, std::chrono::seconds duration) {
|
||||||
mSeverity(severity), mText(text), mDuration(duration), mIsValidMutex(std::make_shared<std::mutex>()), mIsValid(std::make_shared<bool>(true)) {}
|
mData = std::make_shared<LogData>();
|
||||||
|
mData->mDuration =duration;
|
||||||
|
mData->mSeverity = severity;
|
||||||
|
mData->mText = text;
|
||||||
|
}
|
||||||
|
|
||||||
rtc::LogCounter& rtc::LogCounter::operator++(int) {
|
rtc::LogCounter& rtc::LogCounter::operator++(int) {
|
||||||
if (mCount++ == 1) {
|
if (mData->mCount++ == 0) {
|
||||||
ThreadPool::Instance().schedule(mDuration, [this, isValidMutex = mIsValidMutex, isValid = mIsValid]() {
|
ThreadPool::Instance().schedule(mData->mDuration, [](std::weak_ptr<LogData> data) {
|
||||||
std::lock_guard lock(*isValidMutex);
|
if (auto ptr = data.lock()) {
|
||||||
if (*isValid) {
|
|
||||||
int countCopy;
|
int countCopy;
|
||||||
countCopy = mCount.exchange(0);
|
countCopy = ptr->mCount.exchange(0);
|
||||||
PLOG(mSeverity) << mText << ": " << countCopy << " (over "
|
PLOG(ptr->mSeverity) << ptr->mText << ": " << countCopy << " (over "
|
||||||
<< std::chrono::duration_cast<std::chrono::seconds>(mDuration).count() << " seconds)";
|
<< std::chrono::duration_cast<std::chrono::seconds>(ptr->mDuration).count() << " seconds)";
|
||||||
}
|
}
|
||||||
});
|
}, mData);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
rtc::LogCounter::~LogCounter() {
|
|
||||||
std::lock_guard lock(*mIsValidMutex);
|
|
||||||
*mIsValid = false;
|
|
||||||
}
|
|
||||||
|
@ -25,21 +25,20 @@
|
|||||||
namespace rtc {
|
namespace rtc {
|
||||||
class LogCounter {
|
class LogCounter {
|
||||||
private:
|
private:
|
||||||
|
struct LogData {
|
||||||
plog::Severity mSeverity;
|
plog::Severity mSeverity;
|
||||||
std::string mText;
|
std::string mText;
|
||||||
std::chrono::steady_clock::duration mDuration;
|
std::chrono::steady_clock::duration mDuration;
|
||||||
|
|
||||||
std::atomic<int> mCount = 0;
|
std::atomic<int> mCount = 0;
|
||||||
|
};
|
||||||
|
|
||||||
std::shared_ptr<std::mutex> mIsValidMutex;
|
std::shared_ptr<LogData> mData;
|
||||||
std::shared_ptr<bool> mIsValid;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
LogCounter(plog::Severity severity, const std::string& text, std::chrono::seconds duration=std::chrono::seconds(1));
|
LogCounter(plog::Severity severity, const std::string& text, std::chrono::seconds duration=std::chrono::seconds(1));
|
||||||
|
|
||||||
~LogCounter();
|
|
||||||
|
|
||||||
LogCounter& operator++(int);
|
LogCounter& operator++(int);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user