Better regulation of logging mutex

This commit is contained in:
Jack Andersen 2017-01-18 10:54:00 -10:00
parent f913fb8479
commit e6f24e6428
2 changed files with 20 additions and 3 deletions

View File

@ -87,14 +87,29 @@ extern std::atomic_uint_fast64_t FrameIndex;
* *
* Ensures logging streams aren't written concurrently * Ensures logging streams aren't written concurrently
*/ */
extern std::recursive_mutex LogMutex; struct LogMutex
{
bool enabled = true;
std::recursive_mutex mutex;
~LogMutex() { enabled = false; }
std::unique_lock<std::recursive_mutex> lock()
{
if (enabled)
return std::unique_lock<std::recursive_mutex>(mutex);
else
return std::unique_lock<std::recursive_mutex>();
}
};
extern LogMutex _LogMutex;
/** /**
* @brief Take a centralized lock for the logging output stream(s) * @brief Take a centralized lock for the logging output stream(s)
* @return RAII mutex lock * @return RAII mutex lock
*/ */
static inline std::unique_lock<std::recursive_mutex> LockLog() static inline std::unique_lock<std::recursive_mutex> LockLog()
{return std::unique_lock<std::recursive_mutex>{LogMutex};} {
return _LogMutex.lock();
}
/** /**
* @brief Restore centralized logger vector to default state (silent operation) * @brief Restore centralized logger vector to default state (silent operation)

View File

@ -193,8 +193,11 @@ void logvisorAbort()
#endif #endif
LogMutex _LogMutex;
static void AbortHandler(int signum) static void AbortHandler(int signum)
{ {
_LogMutex.enabled = false;
switch (signum) switch (signum)
{ {
case SIGSEGV: case SIGSEGV:
@ -217,7 +220,6 @@ static std::chrono::steady_clock::time_point GlobalStart = MonoClock.now();
static inline std::chrono::steady_clock::duration CurrentUptime() static inline std::chrono::steady_clock::duration CurrentUptime()
{return MonoClock.now() - GlobalStart;} {return MonoClock.now() - GlobalStart;}
std::atomic_uint_fast64_t FrameIndex(0); std::atomic_uint_fast64_t FrameIndex(0);
std::recursive_mutex LogMutex;
static inline int ConsoleWidth() static inline int ConsoleWidth()
{ {