add deleter

This commit is contained in:
mrbesen 2023-09-17 11:13:59 +02:00
parent 13559b181e
commit 6ba52c9f45
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
2 changed files with 23 additions and 0 deletions

13
Log.cpp
View File

@ -186,6 +186,19 @@ Entry::~Entry() {
log(lvl, ss.rdbuf());
}
Deleter::Deleter() {
if(refCount.fetch_add(1) == 0) {
init();
}
}
Deleter::~Deleter() {
if(refCount.fetch_sub(1) == 1) {
stop();
}
}
std::atomic<uint32_t> Deleter::refCount{0};
std::vector<Entry::MetaFunction> entryMetaFunctions;
std::ostream& defaultEntryMetaTime(std::ostream& os, const Entry& e) {

10
Log.h
View File

@ -1,5 +1,6 @@
#pragma once
#include <atomic>
#include <functional> // std::function
#include <sstream> // std::stringstream (buffer for log entries)
#include <string>
@ -100,6 +101,15 @@ public:
}
};
// class to automatically stop the logger on destruction
class Deleter {
public:
Deleter();
~Deleter();
private:
static std::atomic<uint32_t> refCount;
};
extern std::vector<Entry::MetaFunction> entryMetaFunctions;
std::ostream& defaultEntryMetaTime(std::ostream&, const Entry&);