diff --git a/Log.cpp b/Log.cpp index 963a947..3ba56d7 100644 --- a/Log.cpp +++ b/Log.cpp @@ -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 Deleter::refCount{0}; + std::vector entryMetaFunctions; std::ostream& defaultEntryMetaTime(std::ostream& os, const Entry& e) { diff --git a/Log.h b/Log.h index c68c191..144fb58 100644 --- a/Log.h +++ b/Log.h @@ -1,5 +1,6 @@ #pragma once +#include #include // std::function #include // std::stringstream (buffer for log entries) #include @@ -100,6 +101,15 @@ public: } }; +// class to automatically stop the logger on destruction +class Deleter { +public: + Deleter(); + ~Deleter(); +private: + static std::atomic refCount; +}; + extern std::vector entryMetaFunctions; std::ostream& defaultEntryMetaTime(std::ostream&, const Entry&);