From b31ae6e69085311193ddd547e5d96b18bb68ae4b Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 25 Mar 2024 20:37:19 +0100 Subject: [PATCH] impr: Optimize includes in logger --- lib/libimhex/include/hex/helpers/logger.hpp | 19 +++++++++++-------- lib/libimhex/source/helpers/logger.cpp | 12 +++++++++--- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/logger.hpp b/lib/libimhex/include/hex/helpers/logger.hpp index ca7d44b03..e1fa78dd7 100644 --- a/lib/libimhex/include/hex/helpers/logger.hpp +++ b/lib/libimhex/include/hex/helpers/logger.hpp @@ -2,13 +2,11 @@ #include -#include - #include #include #include -#include +#include namespace hex::log { @@ -20,10 +18,12 @@ namespace hex::log { [[maybe_unused]] void redirectToFile(); [[maybe_unused]] void enableColorPrinting(); - [[nodiscard]] std::recursive_mutex& getLoggerMutex(); [[nodiscard]] bool isLoggingSuspended(); [[nodiscard]] bool isDebugLoggingEnabled(); + void lockLoggerMutex(); + void unlockLoggerMutex(); + struct LogEntry { std::string project; std::string level; @@ -39,7 +39,8 @@ namespace hex::log { if (isLoggingSuspended()) [[unlikely]] return; - std::scoped_lock lock(getLoggerMutex()); + lockLoggerMutex(); + ON_SCOPE_EXIT { unlockLoggerMutex(); }; auto dest = getDestination(); try { @@ -94,7 +95,8 @@ namespace hex::log { } [[maybe_unused]] void print(const std::string &fmt, auto && ... args) { - std::scoped_lock lock(impl::getLoggerMutex()); + impl::lockLoggerMutex(); + ON_SCOPE_EXIT { impl::unlockLoggerMutex(); }; try { auto dest = impl::getDestination(); @@ -105,7 +107,8 @@ namespace hex::log { } [[maybe_unused]] void println(const std::string &fmt, auto && ... args) { - std::scoped_lock lock(impl::getLoggerMutex()); + impl::lockLoggerMutex(); + ON_SCOPE_EXIT { impl::unlockLoggerMutex(); }; try { auto dest = impl::getDestination(); @@ -115,4 +118,4 @@ namespace hex::log { } catch (const std::exception&) { } } -} \ No newline at end of file +} diff --git a/lib/libimhex/source/helpers/logger.cpp b/lib/libimhex/source/helpers/logger.cpp index 297fd74c9..d27220cb0 100644 --- a/lib/libimhex/source/helpers/logger.cpp +++ b/lib/libimhex/source/helpers/logger.cpp @@ -1,14 +1,15 @@ #include +#include #include #include #include #include +#include #include #include -#include #if defined(OS_WINDOWS) #include @@ -40,10 +41,15 @@ namespace hex::log { namespace impl { - std::recursive_mutex& getLoggerMutex() { - return s_loggerMutex; + void lockLoggerMutex() { + s_loggerMutex.lock(); } + void unlockLoggerMutex() { + s_loggerMutex.unlock(); + } + + bool isLoggingSuspended() { return s_loggingSuspended; }