diff options
author | Steven Laver <lavers@google.com> | 2020-04-26 19:23:49 -0700 |
---|---|---|
committer | Steven Laver <lavers@google.com> | 2020-04-26 19:23:49 -0700 |
commit | 4ac5a687c56a98bc54783c53d3a7ed38c93c4386 (patch) | |
tree | 36ded3563a964ecb44ef7fda3bb373aed83353d8 /base/logging.cpp | |
parent | be3a52c076b2355e6ef6e4c37e8a2d99310151b5 (diff) | |
parent | 60264d77dc3e467004412af972ac5a80e8d12059 (diff) |
Merge RP1A.200426.001
Change-Id: Ibf1e9770ee4685621e932873dbbebd97e4b4801a
Diffstat (limited to 'base/logging.cpp')
-rw-r--r-- | base/logging.cpp | 34 |
1 files changed, 7 insertions, 27 deletions
diff --git a/base/logging.cpp b/base/logging.cpp index cd460eb46c..3c73fea1a6 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -195,7 +195,6 @@ static std::mutex& LoggingLock() { return logging_lock; } -// Only used for Q fallback. static LogFunction& Logger() { #ifdef __ANDROID__ static auto& logger = *new LogFunction(LogdLogger()); @@ -205,7 +204,6 @@ static LogFunction& Logger() { return logger; } -// Only used for Q fallback. static AbortFunction& Aborter() { static auto& aborter = *new AbortFunction(DefaultAborter); return aborter; @@ -416,45 +414,27 @@ void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) { } void SetLogger(LogFunction&& logger) { + Logger() = std::move(logger); + static auto& liblog_functions = GetLibLogFunctions(); if (liblog_functions) { - // We need to atomically swap the old and new pointers since other threads may be logging. - // We know all threads will be using the new logger after __android_log_set_logger() returns, - // so we can delete it then. - // This leaks one std::function<> per instance of libbase if multiple copies of libbase within a - // single process call SetLogger(). That is the same cost as having a static - // std::function<>, which is the not-thread-safe alternative. - static std::atomic<LogFunction*> logger_function(nullptr); - auto* old_logger_function = logger_function.exchange(new LogFunction(logger)); liblog_functions->__android_log_set_logger([](const struct __android_log_message* log_message) { auto log_id = log_id_tToLogId(log_message->buffer_id); auto severity = PriorityToLogSeverity(log_message->priority); - auto& function = *logger_function.load(std::memory_order_acquire); - function(log_id, severity, log_message->tag, log_message->file, log_message->line, + Logger()(log_id, severity, log_message->tag, log_message->file, log_message->line, log_message->message); }); - delete old_logger_function; - } else { - std::lock_guard<std::mutex> lock(LoggingLock()); - Logger() = std::move(logger); } } void SetAborter(AbortFunction&& aborter) { + Aborter() = std::move(aborter); + static auto& liblog_functions = GetLibLogFunctions(); if (liblog_functions) { - // See the comment in SetLogger(). - static std::atomic<AbortFunction*> abort_function(nullptr); - auto* old_abort_function = abort_function.exchange(new AbortFunction(aborter)); - liblog_functions->__android_log_set_aborter([](const char* abort_message) { - auto& function = *abort_function.load(std::memory_order_acquire); - function(abort_message); - }); - delete old_abort_function; - } else { - std::lock_guard<std::mutex> lock(LoggingLock()); - Aborter() = std::move(aborter); + liblog_functions->__android_log_set_aborter( + [](const char* abort_message) { Aborter()(abort_message); }); } } |