From 3187dbf0aa0943d9eca0c5c1259f8a8ca549709b Mon Sep 17 00:00:00 2001 From: tycho Date: Sun, 30 Aug 2015 22:57:43 +0100 Subject: Increase robustness of the logging subsystem --- src/Logger.h | 59 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'src/Logger.h') diff --git a/src/Logger.h b/src/Logger.h index 176c6e810..a0c2917a9 100644 --- a/src/Logger.h +++ b/src/Logger.h @@ -23,13 +23,35 @@ public: virtual ~cListener(){} }; + class cAttachment + { + public: + + cAttachment(cAttachment && a_other) + : m_listener(a_other.m_listener) + { + } + + ~cAttachment() + { + cLogger::GetInstance().DetachListener(m_listener); + } + + private: + + cListener * m_listener; + + friend class cLogger; + + cAttachment(cListener * a_listener) : m_listener(a_listener) {} + }; + void Log (const char * a_Format, eLogLevel a_LogLevel, va_list a_ArgList) FORMATSTRING(2, 0); /** Logs the simple text message at the specified log level. */ void LogSimple(AString a_Message, eLogLevel a_LogLevel = llRegular); - void AttachListener(cListener * a_Listener); - void DetachListener(cListener * a_Listener); + cAttachment AttachListener(std::unique_ptr a_Listener); static cLogger & GetInstance(void); // Must be called before calling GetInstance in a multithreaded context @@ -37,37 +59,20 @@ public: private: cCriticalSection m_CriticalSection; - std::vector m_LogListeners; - -}; - - - - - - -extern void LOG (const char * a_Format, ...) FORMATSTRING(1, 2); -extern void LOGINFO (const char * a_Format, ...) FORMATSTRING(1, 2); -extern void LOGWARN (const char * a_Format, ...) FORMATSTRING(1, 2); -extern void LOGERROR(const char * a_Format, ...) FORMATSTRING(1, 2); - - - - - -// In debug builds, translate LOGD to LOG, otherwise leave it out altogether: -#ifdef _DEBUG - #define LOGD LOG -#else - #define LOGD(...) -#endif // _DEBUG + std::vector> m_LogListeners; + void DetachListener(cListener * a_Listener); +}; -#define LOGWARNING LOGWARN +// These declarations are duplicated in globals.h +extern void LOG (const char * a_Format, ...) FORMATSTRING(1, 2); +extern void LOGINFO (const char * a_Format, ...) FORMATSTRING(1, 2); +extern void LOGWARNING(const char * a_Format, ...) FORMATSTRING(1, 2); +extern void LOGERROR (const char * a_Format, ...) FORMATSTRING(1, 2); -- cgit v1.2.3