From 9ea5878a234dd41ed598b05c167ed50d68dd937c Mon Sep 17 00:00:00 2001 From: mrbesen Date: Mon, 2 Nov 2020 23:51:44 +0100 Subject: [PATCH 1/2] mutex --- Log.cpp | 22 ++++++++++++++++++++++ Log.h | 6 ++++++ 2 files changed, 28 insertions(+) diff --git a/Log.cpp b/Log.cpp index a08b65c..a8ebcfe 100644 --- a/Log.cpp +++ b/Log.cpp @@ -6,6 +6,10 @@ #include // std::ostream, std::cout, std::cin #include // list of outputs +#if LOG_USEMUTEX == 1 +#include +#endif + namespace Log { /* @@ -21,6 +25,11 @@ public: virtual ~Output() {} virtual void log(Level lvl, std::stringbuf* sbuf) { + //aquire lock + #if LOG_USEMUTEX == 1 + std::unique_lock lock(ostreamLock); + #endif + std::ostream* os = getOs(lvl); if (os) { *os << sbuf << std::endl; @@ -34,6 +43,10 @@ protected: // returns the correct ostream for the given log-level // or returns nullptr if no ostream is set/enabled for this level virtual std::ostream* getOs(Level lvl) = 0; // abstract + + #if LOG_USEMUTEX == 1 + std::mutex ostreamLock; //used for both streams + #endif }; // logging to stdout/stderr @@ -56,6 +69,11 @@ private: // OFF FATAL ERROR WARN NOTE INFO DEBUG TRACE static const char* color_codes[] = {"", "1;31;40m", "31m", "33m", "96m", "32m", "0m", "0m"}; + //aquire lock + #if LOG_USEMUTEX == 1 + std::unique_lock lock(ostreamLock); + #endif + std::ostream* os = getOs(lvl); if (os) { @@ -98,6 +116,10 @@ private: std::ofstream ofs; Level lvl_min = FATAL; + #if LOG_USEMUTEX == 1 + std::mutex ostreamLock; //used for both streams + #endif + virtual std::ostream* getOs(Level lvl) { if (lvl_min <= lvl && lvl <= lvl_max) return &ofs; diff --git a/Log.h b/Log.h index 9fc13ee..5303580 100644 --- a/Log.h +++ b/Log.h @@ -3,6 +3,10 @@ #include // std::stringstream (buffer for log entries) #include +#ifndef LOG_USEMUTEX +#define LOG_USEMUTEX 1 +#endif + namespace Log { enum Level { OFF = 0, FATAL, ERROR, WARN, NOTE, INFO, DEBUG, TRACE }; @@ -17,6 +21,8 @@ void addLogfile(const std::string& filename, Level min, Level max); void setConsoleLogLevel(Level lvl); void setColoredOutput(bool enabled); +inline bool useMutex() { return LOG_USEMUTEX; } + // Log entry that can be formed with various mixed data types // by concatenation with the << operator // Inspired from https://stackoverflow.com/a/8337882 From 63e4c5e830e5c810fa6e0d8c7944a89cd64210f7 Mon Sep 17 00:00:00 2001 From: MrBesen Date: Fri, 20 Nov 2020 22:04:05 +0100 Subject: [PATCH 2/2] USE_MUTEX defaults to 0 --- Log.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Log.h b/Log.h index 5303580..0107bf4 100644 --- a/Log.h +++ b/Log.h @@ -4,7 +4,7 @@ #include #ifndef LOG_USEMUTEX -#define LOG_USEMUTEX 1 +#define LOG_USEMUTEX 0 #endif namespace Log { @@ -21,8 +21,6 @@ void addLogfile(const std::string& filename, Level min, Level max); void setConsoleLogLevel(Level lvl); void setColoredOutput(bool enabled); -inline bool useMutex() { return LOG_USEMUTEX; } - // Log entry that can be formed with various mixed data types // by concatenation with the << operator // Inspired from https://stackoverflow.com/a/8337882