Move default tag from libbase to liblog

Bug: 119867234
Test: log tags look right, libbase/liblog unit tests
Change-Id: I3670c3fdce3d0238a23a53bba2877ffed1291f9c
diff --git a/liblog/logger_write.cpp b/liblog/logger_write.cpp
index 1783854..e86d9ec 100644
--- a/liblog/logger_write.cpp
+++ b/liblog/logger_write.cpp
@@ -16,6 +16,7 @@
 
 #include <errno.h>
 #include <inttypes.h>
+#include <libgen.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/time.h>
@@ -31,6 +32,7 @@
 #include <private/android_logger.h>
 
 #include "android/log.h"
+#include "log/log_read.h"
 #include "logger.h"
 #include "rwlock.h"
 #include "uio.h"
@@ -106,6 +108,45 @@
 #endif
 }
 
+#if defined(__GLIBC__) || defined(_WIN32)
+static const char* getprogname() {
+#if defined(__GLIBC__)
+  return program_invocation_short_name;
+#elif defined(_WIN32)
+  static bool first = true;
+  static char progname[MAX_PATH] = {};
+
+  if (first) {
+    char path[PATH_MAX + 1];
+    DWORD result = GetModuleFileName(nullptr, path, sizeof(path) - 1);
+    if (result == 0 || result == sizeof(path) - 1) return "";
+    path[PATH_MAX - 1] = 0;
+
+    char* path_basename = basename(path);
+
+    snprintf(progname, sizeof(progname), "%s", path_basename);
+    first = false;
+  }
+
+  return progname;
+#endif
+}
+#endif
+
+// It's possible for logging to happen during static initialization before our globals are
+// initialized, so we place this std::string in a function such that it is initialized on the first
+// call.
+static std::string& GetDefaultTag() {
+  static std::string default_tag = getprogname();
+  return default_tag;
+}
+static RwLock default_tag_lock;
+
+void __android_log_set_default_tag(const char* tag) {
+  auto lock = std::unique_lock{default_tag_lock};
+  GetDefaultTag().assign(tag, 0, LOGGER_ENTRY_MAX_PAYLOAD);
+}
+
 static int minimum_log_priority = ANDROID_LOG_DEFAULT;
 int __android_log_set_minimum_priority(int priority) {
   int old_minimum_log_priority = minimum_log_priority;
@@ -270,7 +311,11 @@
 }
 
 void __android_log_write_logger_data(__android_logger_data* logger_data, const char* msg) {
-  if (logger_data->tag == nullptr) logger_data->tag = "";
+  auto tag_lock = std::shared_lock{default_tag_lock, std::defer_lock};
+  if (logger_data->tag == nullptr) {
+    tag_lock.lock();
+    logger_data->tag = GetDefaultTag().c_str();
+  }
 
 #if __BIONIC__
   if (logger_data->priority == ANDROID_LOG_FATAL) {