diff --git a/components/debug/debuglog.cpp b/components/debug/debuglog.cpp index 510c638614..f4f0fdffa6 100644 --- a/components/debug/debuglog.cpp +++ b/components/debug/debuglog.cpp @@ -1,8 +1,36 @@ #include "debuglog.hpp" +#include namespace Debug { Level CurrentDebugLevel = Level::NoLevel; } -std::mutex Log::sLock; +static std::mutex sLock; + +Log::Log(Debug::Level level) + : mShouldLog(level <= Debug::CurrentDebugLevel) +{ + // No need to hold the lock if there will be no logging anyway + if (!mShouldLog) + return; + + // Locks a global lock while the object is alive + sLock.lock(); + + // If the app has no logging system enabled, log level is not specified. + // Show all messages without marker - we just use the plain cout in this case. + if (Debug::CurrentDebugLevel == Debug::NoLevel) + return; + + std::cout << static_cast(level); +} + +Log::~Log() +{ + if (!mShouldLog) + return; + + std::cout << std::endl; + sLock.unlock(); +} diff --git a/components/debug/debuglog.hpp b/components/debug/debuglog.hpp index bcb282ab3a..aa8156e119 100644 --- a/components/debug/debuglog.hpp +++ b/components/debug/debuglog.hpp @@ -1,7 +1,6 @@ #ifndef DEBUG_LOG_H #define DEBUG_LOG_H -#include #include namespace Debug @@ -23,27 +22,9 @@ namespace Debug class Log { - static std::mutex sLock; - - std::unique_lock mLock; public: - explicit Log(Debug::Level level) - : mShouldLog(level <= Debug::CurrentDebugLevel) - { - // No need to hold the lock if there will be no logging anyway - if (!mShouldLog) - return; - - // Locks a global lock while the object is alive - mLock = lock(); - - // If the app has no logging system enabled, log level is not specified. - // Show all messages without marker - we just use the plain cout in this case. - if (Debug::CurrentDebugLevel == Debug::NoLevel) - return; - - std::cout << static_cast(level); - } + explicit Log(Debug::Level level); + ~Log(); // Perfect forwarding wrappers to give the chain of objects to cout template @@ -55,14 +36,6 @@ public: return *this; } - ~Log() - { - if (mShouldLog) - std::cout << std::endl; - } - - static std::unique_lock lock() { return std::unique_lock(sLock); } - private: const bool mShouldLog; };