From 716b784dac5efc4cbf882de1898630476138e400 Mon Sep 17 00:00:00 2001 From: Tim Sylvester Date: Wed, 5 Jun 2024 08:27:25 -0700 Subject: [PATCH] Make `useLogThread` a per-severity option and default `Error` to false (#2473) --- include/mbgl/util/event.hpp | 1 + include/mbgl/util/logging.hpp | 1 + platform/darwin/src/MLNLoggingConfiguration.mm | 2 ++ src/mbgl/util/logging.cpp | 18 +++++++++++++++--- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/mbgl/util/event.hpp b/include/mbgl/util/event.hpp index bd517031b..aa4359179 100644 --- a/include/mbgl/util/event.hpp +++ b/include/mbgl/util/event.hpp @@ -9,6 +9,7 @@ enum class EventSeverity : uint8_t { Info, Warning, Error, + SeverityCount }; enum class Event : uint8_t { diff --git a/include/mbgl/util/logging.hpp b/include/mbgl/util/logging.hpp index 67d5b6e26..479df84d2 100644 --- a/include/mbgl/util/logging.hpp +++ b/include/mbgl/util/logging.hpp @@ -39,6 +39,7 @@ public: ~Log(); static void useLogThread(bool enable) noexcept; + static void useLogThread(bool enable, EventSeverity) noexcept; template static void Debug(Event event, Args&&... args) noexcept { diff --git a/platform/darwin/src/MLNLoggingConfiguration.mm b/platform/darwin/src/MLNLoggingConfiguration.mm index 273dd287f..02f321f9f 100644 --- a/platform/darwin/src/MLNLoggingConfiguration.mm +++ b/platform/darwin/src/MLNLoggingConfiguration.mm @@ -29,6 +29,8 @@ public: case EventSeverity::Error: MLNLogError(message); break; + default: + assert(false); } return true; } diff --git a/src/mbgl/util/logging.cpp b/src/mbgl/util/logging.cpp index 1b5fb9fde..d1f0d191d 100644 --- a/src/mbgl/util/logging.cpp +++ b/src/mbgl/util/logging.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -14,7 +15,8 @@ namespace mbgl { namespace { std::unique_ptr currentObserver; -std::atomic useThread(true); +constexpr auto SeverityCount = underlying_type(EventSeverity::SeverityCount); +std::atomic useThread[SeverityCount] = {true, true, true, false}; std::mutex mutex; } // namespace @@ -25,7 +27,7 @@ public: : scheduler(Scheduler::GetSequenced()) {} void record(EventSeverity severity, Event event, int64_t code, const std::string& msg) try { - if (useThread) { + if (useThread[underlying_type(severity)]) { auto threadName = platform::getCurrentThreadName(); scheduler->schedule([=]() { Log::record(severity, event, code, msg, threadName); }); } else { @@ -55,7 +57,17 @@ Log* Log::get() noexcept { } void Log::useLogThread(bool enable) noexcept { - useThread = enable; + useLogThread(enable, EventSeverity::Debug); + useLogThread(enable, EventSeverity::Info); + useLogThread(enable, EventSeverity::Warning); + useLogThread(enable, EventSeverity::Error); +} + +void Log::useLogThread(bool enable, EventSeverity severity) noexcept { + const auto index = underlying_type(severity); + if (index < SeverityCount) { + useThread[index] = enable; + } } void Log::setObserver(std::unique_ptr observer) {