From 2544e150182b496cccbbfdeece18467164eb4a85 Mon Sep 17 00:00:00 2001 From: drath Date: Sun, 5 Oct 2025 21:53:42 +0200 Subject: [PATCH] split up mutexes for performance --- include/log.hpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/include/log.hpp b/include/log.hpp index 812acc9..c384064 100644 --- a/include/log.hpp +++ b/include/log.hpp @@ -8,7 +8,8 @@ #include #include -inline std::mutex mtx; +inline std::mutex cmtx; // console logging mutex +inline std::mutex fmtx; // file logging mutex enum class LOG { INF, WRN, ERR, DBG, START }; template @@ -33,18 +34,21 @@ inline void log(LOG level, std::string_view tag, std::format_string str break; } - std::scoped_lock lock(mtx); /* log to console */ - std::print(pattern, color, tag, reset); - std::println(str, std::forward(args)...); + { + std::scoped_lock lock(cmtx); + std::print(pattern, color, tag, reset); + std::println(str, std::forward(args)...); + } /* log to file */ if (logToFile) { auto now = std::chrono::system_clock::now(); - std::string msg = std::format("{:%x %H:%M:%S }", floor(now)); // timestamp - std::format_to(std::back_inserter(msg), pattern, color, tag, reset); // tag - std::format_to(std::back_inserter(msg), str, std::forward(args)...); // actual log message - + std::string msg = std::format("{:%x %H:%M:%S }", floor(now)); // timestamp + std::format_to(std::back_inserter(msg), pattern, color, tag, reset); // tag + std::format_to(std::back_inserter(msg), str, std::forward(args)...); // actual log message + + std::scoped_lock lock (fmtx); gzFile file = gzopen("linuxSmartie.log.gz", "ab"); if (file) { gzprintf(file, "%s\n", msg.c_str());