summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuha Alanen <juha.alanen@mapbox.com>2020-03-31 11:08:11 +0300
committerJuha Alanen <juha.alanen@mapbox.com>2020-04-01 11:17:43 +0300
commita67aaca56b21f334018a5d10e3d5a691bdc4e98a (patch)
tree34214545b7289fbebaf50ff4eb151aebf1133fca
parent349c6153de3c88f45b730be0d8ba9e2b14ab4d2a (diff)
downloadqtlocation-mapboxgl-a67aaca56b21f334018a5d10e3d5a691bdc4e98a.tar.gz
[core] Move logging off the main thread
-rw-r--r--include/mbgl/util/logging.hpp16
-rw-r--r--src/mbgl/util/logging.cpp53
2 files changed, 62 insertions, 7 deletions
diff --git a/include/mbgl/util/logging.hpp b/include/mbgl/util/logging.hpp
index 03db2d7462..1b72d74738 100644
--- a/include/mbgl/util/logging.hpp
+++ b/include/mbgl/util/logging.hpp
@@ -3,6 +3,7 @@
#include <mbgl/util/event.hpp>
#include <mbgl/util/noncopyable.hpp>
+#include <mbgl/util/optional.hpp>
#include <memory>
#include <string>
@@ -36,6 +37,11 @@ private:
}
public:
+ Log();
+ ~Log();
+
+ static void useLogThread(bool enable);
+
template <typename ...Args>
static void Debug(Event event, Args&& ...args) {
Record(EventSeverity::Debug, event, ::std::forward<Args>(args)...);
@@ -66,15 +72,23 @@ public:
}
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<std::string>& 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> impl;
};
} // namespace mbgl
diff --git a/src/mbgl/util/logging.cpp b/src/mbgl/util/logging.cpp
index 366f9f1a05..00409b974a 100644
--- a/src/mbgl/util/logging.cpp
+++ b/src/mbgl/util/logging.cpp
@@ -1,5 +1,7 @@
-#include <mbgl/util/logging.hpp>
+#include <mbgl/actor/scheduler.hpp>
+#include <mbgl/platform/settings.hpp>
#include <mbgl/util/enum.hpp>
+#include <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
#include <cstdio>
@@ -11,21 +13,55 @@ namespace mbgl {
namespace {
std::unique_ptr<Log::Observer> currentObserver;
+std::atomic<bool> 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> scheduler;
+};
+
+Log::Log() : impl(std::make_unique<Impl>()) {}
+
+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> observer) {
+ std::lock_guard<std::mutex> lock(mutex);
currentObserver = std::move(observer);
}
std::unique_ptr<Log::Observer> Log::removeObserver() {
+ std::lock_guard<std::mutex> lock(mutex);
std::unique_ptr<Observer> 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<std::string>& threadName) {
+ std::lock_guard<std::mutex> 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<Event>::toString(event) << "]";
if (code >= 0) {