diff --git a/components/debug/debuglog.hpp b/components/debug/debuglog.hpp index f794768546..96b9d798e9 100644 --- a/components/debug/debuglog.hpp +++ b/components/debug/debuglog.hpp @@ -27,25 +27,29 @@ class Log std::unique_lock mLock; public: - // Locks a global lock while the object is alive - Log(Debug::Level level) : - mLock(sLock), - mLevel(level) + 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 = std::unique_lock(sLock); + // 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; - if (mLevel <= Debug::CurrentDebugLevel) - std::cout << static_cast(mLevel); + std::cout << static_cast(level); } // Perfect forwarding wrappers to give the chain of objects to cout template Log& operator<<(T&& rhs) { - if (mLevel <= Debug::CurrentDebugLevel) + if (mShouldLog) std::cout << std::forward(rhs); return *this; @@ -53,12 +57,12 @@ public: ~Log() { - if (mLevel <= Debug::CurrentDebugLevel) + if (mShouldLog) std::cout << std::endl; } private: - Debug::Level mLevel; + const bool mShouldLog; }; #endif