From 54061c56bfac21d4588d2fa5051b3f15b3eb5a1a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 18 Jun 2023 22:32:55 +0200 Subject: [PATCH] fix: Logging race conditions --- lib/libimhex/include/hex/helpers/logger.hpp | 23 ++++++++++++--------- lib/libimhex/source/helpers/logger.cpp | 21 ++++++++++--------- main/source/crash_handlers.cpp | 6 +++--- main/source/window/linux_window.cpp | 2 +- main/source/window/macos_window.cpp | 2 +- main/source/window/win_window.cpp | 2 +- 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/logger.hpp b/lib/libimhex/include/hex/helpers/logger.hpp index 0341caecc..a66c8096f 100644 --- a/lib/libimhex/include/hex/helpers/logger.hpp +++ b/lib/libimhex/include/hex/helpers/logger.hpp @@ -13,9 +13,16 @@ namespace hex::log { - FILE *getDestination(); - wolv::io::File& getFile(); - bool isRedirected(); + namespace impl { + + FILE *getDestination(); + wolv::io::File& getFile(); + bool isRedirected(); + [[maybe_unused]] void redirectToFile(); + + extern std::mutex s_loggerMutex; + + } namespace { @@ -24,7 +31,7 @@ namespace hex::log { fmt::print(dest, "[{0:%H:%M:%S}] ", now); - if (isRedirected()) + if (impl::isRedirected()) fmt::print(dest, "{0} ", level); else fmt::print(dest, ts, "{0} ", level); @@ -34,11 +41,9 @@ namespace hex::log { template [[maybe_unused]] void print(const fmt::text_style &ts, const std::string &level, const std::string &fmt, auto... args) { - auto dest = getDestination(); - - static std::mutex loggerMutex; - std::scoped_lock lock(loggerMutex); + std::scoped_lock lock(impl::s_loggerMutex); + auto dest = impl::getDestination(); printPrefix(dest, ts, level); fmt::print(dest, fmt::runtime(fmt), args...); fmt::print(dest, "\n"); @@ -70,6 +75,4 @@ namespace hex::log { hex::log::print(fg(fmt::color::purple) | fmt::emphasis::bold, "[FATAL]", fmt, args...); } - [[maybe_unused]] void redirectToFile(); - } \ No newline at end of file diff --git a/lib/libimhex/source/helpers/logger.cpp b/lib/libimhex/source/helpers/logger.cpp index ec2251d67..c26494a55 100644 --- a/lib/libimhex/source/helpers/logger.cpp +++ b/lib/libimhex/source/helpers/logger.cpp @@ -4,34 +4,35 @@ #include -namespace hex::log { +namespace hex::log::impl { - static wolv::io::File g_loggerFile; + static wolv::io::File s_loggerFile; + std::mutex s_loggerMutex; FILE *getDestination() { - if (g_loggerFile.isValid()) - return g_loggerFile.getHandle(); + if (s_loggerFile.isValid()) + return s_loggerFile.getHandle(); else return stdout; } wolv::io::File& getFile() { - return g_loggerFile; + return s_loggerFile; } bool isRedirected() { - return g_loggerFile.isValid(); + return s_loggerFile.isValid(); } void redirectToFile() { - if (g_loggerFile.isValid()) return; + if (s_loggerFile.isValid()) return; for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs, true)) { wolv::io::fs::createDirectories(path); - g_loggerFile = wolv::io::File(path / hex::format("{0:%Y%m%d_%H%M%S}.log", fmt::localtime(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()))), wolv::io::File::Mode::Create); - g_loggerFile.disableBuffering(); + s_loggerFile = wolv::io::File(path / hex::format("{0:%Y%m%d_%H%M%S}.log", fmt::localtime(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()))), wolv::io::File::Mode::Create); + s_loggerFile.disableBuffering(); - if (g_loggerFile.isValid()) break; + if (s_loggerFile.isValid()) break; } } diff --git a/main/source/crash_handlers.cpp b/main/source/crash_handlers.cpp index f2919222c..ba8f398e7 100644 --- a/main/source/crash_handlers.cpp +++ b/main/source/crash_handlers.cpp @@ -34,7 +34,7 @@ namespace hex::crash { log::fatal(message); nlohmann::json crashData{ - {"logFile", wolv::util::toUTF8String(hex::log::getFile().getPath())}, + {"logFile", wolv::util::toUTF8String(hex::log::impl::getFile().getPath())}, {"project", wolv::util::toUTF8String(ProjectFile::getPath())}, }; @@ -83,7 +83,7 @@ namespace hex::crash { #if defined(DEBUG) assert(!"Debug build, triggering breakpoint"); #else - std::raise(signalNumber); + std::exit(signalNumber); #endif } @@ -125,7 +125,7 @@ namespace hex::crash { #if defined(DEBUG) assert(!"Debug build, triggering breakpoint"); #else - std::terminate(); + std::exit(100); #endif } }); diff --git a/main/source/window/linux_window.cpp b/main/source/window/linux_window.cpp index c36a4dd1f..2ea25bd53 100644 --- a/main/source/window/linux_window.cpp +++ b/main/source/window/linux_window.cpp @@ -74,7 +74,7 @@ namespace hex { // Redirect stdout to log file if we're not running in a terminal if (!isatty(STDOUT_FILENO)) { - log::redirectToFile(); + log::impl::redirectToFile(); } } diff --git a/main/source/window/macos_window.cpp b/main/source/window/macos_window.cpp index 45d3b04ab..dd31a5b8a 100644 --- a/main/source/window/macos_window.cpp +++ b/main/source/window/macos_window.cpp @@ -36,7 +36,7 @@ namespace hex { // Redirect stdout to log file if we're not running in a terminal if (!isatty(STDOUT_FILENO)) { - log::redirectToFile(); + log::impl::redirectToFile(); } } diff --git a/main/source/window/win_window.cpp b/main/source/window/win_window.cpp index a44657f89..d2387116f 100644 --- a/main/source/window/win_window.cpp +++ b/main/source/window/win_window.cpp @@ -218,7 +218,7 @@ namespace hex { } } } else { - log::redirectToFile(); + log::impl::redirectToFile(); } // Open new files in already existing ImHex instance