diff --git a/include/mbgl/util/logging.hpp b/include/mbgl/util/logging.hpp index 03db2d74629..1b72d747382 100644 --- a/include/mbgl/util/logging.hpp +++ b/include/mbgl/util/logging.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -36,6 +37,11 @@ class Log { } public: + Log(); + ~Log(); + + static void useLogThread(bool enable); + template static void Debug(Event event, Args&& ...args) { Record(EventSeverity::Debug, event, ::std::forward(args)...); @@ -66,15 +72,23 @@ class Log { } private: + static Log* get() noexcept; + static void record(EventSeverity severity, Event event, const std::string &msg); static void record(EventSeverity severity, Event event, const char* format = "", ...); static void record(EventSeverity severity, Event event, int64_t code, const char* format = "", ...); - static void record(EventSeverity severity, Event event, int64_t code, const std::string &msg); + static void record(EventSeverity severity, + Event event, + int64_t code, + const std::string& msg, + const optional& threadName); // This method is the data sink that must be implemented by each platform we // support. It should ideally output the error message in a human readable // format to the developer. static void platformRecord(EventSeverity severity, const std::string &msg); + class Impl; + const std::unique_ptr impl; }; } // namespace mbgl diff --git a/src/mbgl/util/logging.cpp b/src/mbgl/util/logging.cpp index fd3f14825d2..00409b974a0 100644 --- a/src/mbgl/util/logging.cpp +++ b/src/mbgl/util/logging.cpp @@ -1,5 +1,7 @@ -#include +#include +#include #include +#include #include #include @@ -10,22 +12,56 @@ namespace mbgl { namespace { -static std::unique_ptr currentObserver; +std::unique_ptr currentObserver; +std::atomic useThread(true); +std::mutex mutex; } // namespace +class Log::Impl { +public: + Impl() : scheduler(Scheduler::GetSequenced()) {} + + void record(EventSeverity severity, Event event, int64_t code, const std::string& msg) { + if (useThread) { + auto threadName = platform::getCurrentThreadName(); + scheduler->schedule([=]() { Log::record(severity, event, code, msg, threadName); }); + } else { + Log::record(severity, event, code, msg, {}); + } + } + +private: + const std::shared_ptr scheduler; +}; + +Log::Log() : impl(std::make_unique()) {} + +Log::~Log() = default; + +Log* Log::get() noexcept { + static Log instance; + return &instance; +} + +void Log::useLogThread(bool enable) { + useThread = enable; +} + void Log::setObserver(std::unique_ptr observer) { + std::lock_guard lock(mutex); currentObserver = std::move(observer); } std::unique_ptr Log::removeObserver() { + std::lock_guard lock(mutex); std::unique_ptr observer; std::swap(observer, currentObserver); return observer; } void Log::record(EventSeverity severity, Event event, const std::string &msg) { - record(severity, event, -1, msg); + get()->impl->record(severity, event, -1, msg); } void Log::record(EventSeverity severity, Event event, const char* format, ...) { @@ -35,7 +71,7 @@ void Log::record(EventSeverity severity, Event event, const char* format, ...) { vsnprintf(msg, sizeof(msg), format, args); va_end(args); - record(severity, event, -1, std::string{ msg }); + get()->impl->record(severity, event, -1, std::string{msg}); } void Log::record(EventSeverity severity, Event event, int64_t code, const char* format, ...) { @@ -45,10 +81,15 @@ void Log::record(EventSeverity severity, Event event, int64_t code, const char* vsnprintf(msg, sizeof(msg), format, args); va_end(args); - record(severity, event, code, std::string{ msg }); + get()->impl->record(severity, event, code, std::string{msg}); } -void Log::record(EventSeverity severity, Event event, int64_t code, const std::string &msg) { +void Log::record(EventSeverity severity, + Event event, + int64_t code, + const std::string& msg, + const optional& threadName) { + std::lock_guard lock(mutex); if (currentObserver && severity != EventSeverity::Debug && currentObserver->onRecord(severity, event, code, msg)) { return; @@ -56,7 +97,7 @@ void Log::record(EventSeverity severity, Event event, int64_t code, const std::s std::stringstream logStream; - logStream << "{" << platform::getCurrentThreadName() << "}"; + logStream << "{" << threadName.value_or(platform::getCurrentThreadName()) << "}"; logStream << "[" << Enum::toString(event) << "]"; if (code >= 0) {