Move default tag from libbase to liblog
Bug: 119867234
Test: log tags look right, libbase/liblog unit tests
Change-Id: I3670c3fdce3d0238a23a53bba2877ffed1291f9c
diff --git a/base/logging.cpp b/base/logging.cpp
index 508871d..1d8ef57 100644
--- a/base/logging.cpp
+++ b/base/logging.cpp
@@ -211,26 +211,27 @@
return aborter;
}
+// Only used for Q fallback.
static std::recursive_mutex& TagLock() {
static auto& tag_lock = *new std::recursive_mutex();
return tag_lock;
}
+// Only used for Q fallback.
static std::string* gDefaultTag;
-std::string GetDefaultTag() {
- std::lock_guard<std::recursive_mutex> lock(TagLock());
- if (gDefaultTag == nullptr) {
- return "";
- }
- return *gDefaultTag;
-}
+
void SetDefaultTag(const std::string& tag) {
- std::lock_guard<std::recursive_mutex> lock(TagLock());
- if (gDefaultTag != nullptr) {
- delete gDefaultTag;
- gDefaultTag = nullptr;
- }
- if (!tag.empty()) {
- gDefaultTag = new std::string(tag);
+ static auto& liblog_functions = GetLibLogFunctions();
+ if (liblog_functions) {
+ liblog_functions->__android_log_set_default_tag(tag.c_str());
+ } else {
+ std::lock_guard<std::recursive_mutex> lock(TagLock());
+ if (gDefaultTag != nullptr) {
+ delete gDefaultTag;
+ gDefaultTag = nullptr;
+ }
+ if (!tag.empty()) {
+ gDefaultTag = new std::string(tag);
+ }
}
}
@@ -574,24 +575,18 @@
const char* message) {
static auto& liblog_functions = GetLibLogFunctions();
auto priority = LogSeverityToPriority(severity);
- if (tag == nullptr) {
- std::lock_guard<std::recursive_mutex> lock(TagLock());
- if (gDefaultTag == nullptr) {
- gDefaultTag = new std::string(getprogname());
- }
-
- if (liblog_functions) {
- __android_logger_data logger_data = {sizeof(__android_logger_data), LOG_ID_DEFAULT, priority,
- gDefaultTag->c_str(), file, line};
- __android_log_write_logger_data(&logger_data, message);
- } else {
- Logger()(DEFAULT, severity, gDefaultTag->c_str(), file, line, message);
- }
+ if (liblog_functions) {
+ __android_logger_data logger_data = {
+ sizeof(__android_logger_data), LOG_ID_DEFAULT, priority, tag, file, line};
+ __android_log_write_logger_data(&logger_data, message);
} else {
- if (liblog_functions) {
- __android_logger_data logger_data = {
- sizeof(__android_logger_data), LOG_ID_DEFAULT, priority, tag, file, line};
- __android_log_write_logger_data(&logger_data, message);
+ if (tag == nullptr) {
+ std::lock_guard<std::recursive_mutex> lock(TagLock());
+ if (gDefaultTag == nullptr) {
+ gDefaultTag = new std::string(getprogname());
+ }
+
+ Logger()(DEFAULT, severity, gDefaultTag->c_str(), file, line, message);
} else {
Logger()(DEFAULT, severity, tag, file, line, message);
}