From 678a4619044a0f9f8decfeddd555e7b2d7084a0c Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Tue, 1 Aug 2023 20:23:05 +1000 Subject: [PATCH] Logger: Do string formatting before lock and only if LogLevel is loggable --- source/core/StarLogging.cpp | 30 +++++++++++++++++++++++++----- source/core/StarLogging.hpp | 18 +++++++++++------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/source/core/StarLogging.cpp b/source/core/StarLogging.cpp index b80ae6a..ba58c04 100644 --- a/source/core/StarLogging.cpp +++ b/source/core/StarLogging.cpp @@ -16,6 +16,7 @@ LogSink::~LogSink() {} void LogSink::setLevel(LogLevel level) { m_level = level; + Logger::refreshLoggable(); } LogLevel LogSink::level() { @@ -44,11 +45,13 @@ void FileLogSink::log(char const* msg, LogLevel level) { void Logger::addSink(LogSinkPtr s) { MutexLocker locker(s_mutex); s_sinks.insert(s); + refreshLoggable(); } void Logger::removeSink(LogSinkPtr s) { MutexLocker locker(s_mutex); s_sinks.erase(s); + refreshLoggable(); } LogSinkPtr Logger::stdoutSink() { @@ -59,19 +62,36 @@ LogSinkPtr Logger::stdoutSink() { void Logger::removeStdoutSink() { MutexLocker locker(s_mutex); s_sinks.erase(s_stdoutSink); + refreshLoggable(); } void Logger::log(LogLevel level, char const* msg) { - MutexLocker locker(s_mutex); - - for (auto const& l : s_sinks) { - if (l->level() <= level) - l->log(msg, level); + if (loggable(level)) { + MutexLocker locker(s_mutex); + for (auto const& l : s_sinks) { + if (l->level() <= level) + l->log(msg, level); + } } } +bool Logger::loggable(LogLevel level) { + return s_loggable[(int)level]; +} + +void Logger::refreshLoggable() { + Array loggable; + for (auto const& l : s_sinks) { + for (auto i = (size_t)l->level(); i != loggable.size(); ++i) + loggable[i] = true; + } + MutexLocker locker(s_mutex); + s_loggable = loggable; +} + shared_ptr Logger::s_stdoutSink = make_shared(); HashSet Logger::s_sinks{s_stdoutSink}; +Array Logger::s_loggable = Array::filled(false); Mutex Logger::s_mutex; String LogMap::getValue(String const& key) { diff --git a/source/core/StarLogging.hpp b/source/core/StarLogging.hpp index 3e71aff..1ab21a9 100644 --- a/source/core/StarLogging.hpp +++ b/source/core/StarLogging.hpp @@ -81,9 +81,13 @@ public: template static void error(char const* msg, Args const&... args); + static bool loggable(LogLevel level); + static void refreshLoggable(); private: + static shared_ptr s_stdoutSink; static HashSet s_sinks; + static Array s_loggable; static Mutex s_mutex; }; @@ -152,13 +156,13 @@ private: template void Logger::logf(LogLevel level, char const* msg, Args const&... args) { - MutexLocker locker(s_mutex); - Maybe output; - for (auto const& l : s_sinks) { - if (l->level() <= level) { - if (!output) - output = strf(msg, args...); - l->log(output->c_str(), level); + if (loggable(level)) { + std::string output = strf(msg, args...); + MutexLocker locker(s_mutex); + for (auto const& l : s_sinks) { + if (l->level() <= level) { + l->log(output.c_str(), level); + } } } }