split up mutexes for performance

This commit is contained in:
drath
2025-10-05 21:53:42 +02:00
parent 438df9eca8
commit 2544e15018

View File

@@ -8,7 +8,8 @@
#include <print>
#include <string_view>
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 <typename... Args>
@@ -33,18 +34,21 @@ inline void log(LOG level, std::string_view tag, std::format_string<Args...> str
break;
}
std::scoped_lock lock(mtx);
/* log to console */
std::print(pattern, color, tag, reset);
std::println(str, std::forward<Args>(args)...);
{
std::scoped_lock lock(cmtx);
std::print(pattern, color, tag, reset);
std::println(str, std::forward<Args>(args)...);
}
/* log to file */
if (logToFile) {
auto now = std::chrono::system_clock::now();
std::string msg = std::format("{:%x %H:%M:%S }", floor<std::chrono::milliseconds>(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>(args)...); // actual log message
std::string msg = std::format("{:%x %H:%M:%S }", floor<std::chrono::milliseconds>(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>(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());