1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-19 19:53:53 +00:00

Move Log implementation to cpp, remove expensive mutex include in hpp

This commit is contained in:
ζeh Matt 2022-06-07 01:36:08 +03:00
parent 14bf9af056
commit e185d186bf
No known key found for this signature in database
GPG key ID: 18CE582C71A225B0
2 changed files with 31 additions and 30 deletions

View file

@ -1,8 +1,36 @@
#include "debuglog.hpp" #include "debuglog.hpp"
#include <mutex>
namespace Debug namespace Debug
{ {
Level CurrentDebugLevel = Level::NoLevel; 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<unsigned char>(level);
}
Log::~Log()
{
if (!mShouldLog)
return;
std::cout << std::endl;
sLock.unlock();
}

View file

@ -1,7 +1,6 @@
#ifndef DEBUG_LOG_H #ifndef DEBUG_LOG_H
#define DEBUG_LOG_H #define DEBUG_LOG_H
#include <mutex>
#include <iostream> #include <iostream>
namespace Debug namespace Debug
@ -23,27 +22,9 @@ namespace Debug
class Log class Log
{ {
static std::mutex sLock;
std::unique_lock<std::mutex> mLock;
public: public:
explicit Log(Debug::Level level) explicit Log(Debug::Level level);
: mShouldLog(level <= Debug::CurrentDebugLevel) ~Log();
{
// 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<unsigned char>(level);
}
// Perfect forwarding wrappers to give the chain of objects to cout // Perfect forwarding wrappers to give the chain of objects to cout
template<typename T> template<typename T>
@ -55,14 +36,6 @@ public:
return *this; return *this;
} }
~Log()
{
if (mShouldLog)
std::cout << std::endl;
}
static std::unique_lock<std::mutex> lock() { return std::unique_lock<std::mutex>(sLock); }
private: private:
const bool mShouldLog; const bool mShouldLog;
}; };