Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Use separate thread for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Juha Alanen committed Mar 20, 2020
1 parent 8bf841a commit d9779a7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
16 changes: 15 additions & 1 deletion include/mbgl/util/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
#include <mbgl/util/event.hpp>

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/optional.hpp>

#include <memory>
#include <string>

namespace mbgl {

class Log {

class Impl;
class LogThread;

public:
class Observer : private util::noncopyable {
public:
Expand All @@ -36,6 +41,11 @@ class Log {
}

public:
Log();
~Log() = default;

static void useLogThread(bool enable);

template <typename ...Args>
static void Debug(Event event, Args&& ...args) {
Record(EventSeverity::Debug, event, ::std::forward<Args>(args)...);
Expand Down Expand Up @@ -66,15 +76,19 @@ 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<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
49 changes: 44 additions & 5 deletions src/mbgl/util/logging.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <mbgl/util/logging.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/platform/settings.hpp>

#include <cstdio>
#include <cstdarg>
Expand All @@ -11,9 +13,46 @@ namespace mbgl {
namespace {

static std::unique_ptr<Log::Observer> currentObserver;
static bool useThread = true;

} // namespace

class Log::LogThread {
public:
void record(EventSeverity severity, Event event, int64_t code, const std::string &msg, const optional<std::string>(threadName)) {
Log::record(severity, event, code, msg, threadName);
}
};

class Log::Impl {
public:
Impl() : thread(std::make_unique<util::Thread<LogThread>>(
util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_WORKER),
"LogThread")) {}

void record(EventSeverity severity, Event event, int64_t code, const std::string &msg) {
if (useThread) {
thread->actor().invoke(&LogThread::record, severity, event, code, msg, platform::getCurrentThreadName());
} else {
Log::record(severity, event, code, msg, {});
}
}

private:
const std::unique_ptr<util::Thread<LogThread>> thread;
};

Log::Log() : impl(std::make_unique<Impl>()) {}

Log* Log::get() noexcept {
static Log instance;
return &instance;
}

void Log::useLogThread(bool enable) {
useThread = enable;
}

void Log::setObserver(std::unique_ptr<Observer> observer) {
currentObserver = std::move(observer);
}
Expand All @@ -25,7 +64,7 @@ std::unique_ptr<Log::Observer> Log::removeObserver() {
}

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, ...) {
Expand All @@ -35,7 +74,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, ...) {
Expand All @@ -45,18 +84,18 @@ 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) {
if (currentObserver && severity != EventSeverity::Debug &&
currentObserver->onRecord(severity, event, code, msg)) {
return;
}

std::stringstream logStream;

logStream << "{" << platform::getCurrentThreadName() << "}";
logStream << "{" << threadName.value_or(platform::getCurrentThreadName()) << "}";
logStream << "[" << Enum<Event>::toString(event) << "]";

if (code >= 0) {
Expand Down

0 comments on commit d9779a7

Please sign in to comment.