mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-03 17:37:18 +00:00
Merge branch 'log_lock' into 'master'
Acquire log lock only when logger should log See merge request OpenMW/openmw!1363
This commit is contained in:
commit
b6718ecb10
@ -27,25 +27,29 @@ class Log
|
||||
|
||||
std::unique_lock<std::mutex> 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<std::mutex>(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<unsigned char>(mLevel);
|
||||
std::cout << static_cast<unsigned char>(level);
|
||||
}
|
||||
|
||||
// Perfect forwarding wrappers to give the chain of objects to cout
|
||||
template<typename T>
|
||||
Log& operator<<(T&& rhs)
|
||||
{
|
||||
if (mLevel <= Debug::CurrentDebugLevel)
|
||||
if (mShouldLog)
|
||||
std::cout << std::forward<T>(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
|
||||
|
Loading…
Reference in New Issue
Block a user