Add logvisor::LockLog() for ensuring output stream sync

This commit is contained in:
Jack Andersen 2016-08-24 14:25:06 -10:00
parent 68fcfd8469
commit f8a24cc128
2 changed files with 17 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include <atomic> #include <atomic>
#include <memory> #include <memory>
#include <mutex>
extern "C" void logvisorBp(); extern "C" void logvisorBp();
@ -79,6 +80,19 @@ extern std::atomic_size_t ErrorCount;
*/ */
extern std::atomic_uint_fast64_t FrameIndex; extern std::atomic_uint_fast64_t FrameIndex;
/**
* @brief Centralized logging lock
*
* Ensures logging streams aren't written concurrently
*/
extern std::mutex LogMutex;
/**
* @brief Take a centralized lock for the logging output stream(s)
* @return RAII mutex lock
*/
static inline std::unique_lock<std::mutex> LockLog() {return std::unique_lock<std::mutex>{LogMutex};}
/** /**
* @brief Restore centralized logger vector to default state (silent operation) * @brief Restore centralized logger vector to default state (silent operation)
*/ */
@ -145,6 +159,7 @@ public:
template <typename CharType> template <typename CharType>
inline void report(Level severity, const CharType* format, va_list ap) inline void report(Level severity, const CharType* format, va_list ap)
{ {
auto lk = LockLog();
for (auto& logger : MainLoggers) for (auto& logger : MainLoggers)
{ {
va_list apc; va_list apc;
@ -179,6 +194,7 @@ public:
template <typename CharType> template <typename CharType>
inline void reportSource(Level severity, const char* file, unsigned linenum, const CharType* format, va_list ap) inline void reportSource(Level severity, const char* file, unsigned linenum, const CharType* format, va_list ap)
{ {
auto lk = LockLog();
for (auto& logger : MainLoggers) for (auto& logger : MainLoggers)
{ {
va_list apc; va_list apc;

View File

@ -73,6 +73,7 @@ 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::mutex LogMutex;
static inline int ConsoleWidth() static inline int ConsoleWidth()
{ {