liblog_symbols.cpp/.h are removed

liblog APIs that are added after 29 SDK (which is the min_sdk_version of
libbaase) are now accessed directly without using dlsym. The symbols are
now available (i.e. not hidden behind __ANDROID_API__) at compile time.
Instead, the compiler enforces that the calls to the APIs are protected
with a runtime check __builtin_available.

Bug: 134795810
Test: m
Change-Id: I00e20dfc67cd55fcea2c06e911596d224c707f37
diff --git a/Android.bp b/Android.bp
index 524ff65..7167bb3 100644
--- a/Android.bp
+++ b/Android.bp
@@ -64,7 +64,6 @@
         "chrono_utils.cpp",
         "cmsg.cpp",
         "file.cpp",
-        "liblog_symbols.cpp",
         "logging.cpp",
         "mapped_file.cpp",
         "parsebool.cpp",
diff --git a/liblog_symbols.cpp b/liblog_symbols.cpp
deleted file mode 100644
index 2e4ab3e..0000000
--- a/liblog_symbols.cpp
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * Copyright (C) 2020 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "liblog_symbols.h"
-
-// __ANDROID_APEX_MIN_SDK_VERSION__ is the max of all the minSdkVersions that
-// this module shares an APEX with. In other words, this is the lowest OS
-// version that this code could possibly run on.
-#if defined(__ANDROID_APEX_MIN_SDK_VERSION__) && (__ANDROID_APEX_MIN_SDK_VERSION__ <= 29)
-#define USE_DLSYM
-#endif
-
-// __ANDROID_API__ is the historical name for __ANDROID_MIN_SDK_VERSION__. The
-// latter is not set by the Clang we're currently using. This path will be used
-// when this code is built by NDK modules rather than APEX modules.
-#if defined(__ANDROID_API__) && (__ANDROID_API__ <= 29)
-#define USE_DLSYM
-#endif
-
-#ifdef USE_DLSYM
-#include <dlfcn.h>
-#endif
-
-namespace android {
-namespace base {
-
-#ifdef USE_DLSYM
-
-const std::optional<LibLogFunctions>& GetLibLogFunctions() {
-  static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
-    void* liblog_handle = dlopen("liblog.so", RTLD_NOW);
-    if (liblog_handle == nullptr) {
-      return {};
-    }
-
-    LibLogFunctions real_liblog_functions = {};
-
-#define DLSYM(name)                                                                   \
-  real_liblog_functions.name =                                                        \
-      reinterpret_cast<decltype(LibLogFunctions::name)>(dlsym(liblog_handle, #name)); \
-  if (real_liblog_functions.name == nullptr) {                                        \
-    return {};                                                                        \
-  }
-
-    DLSYM(__android_log_set_logger)
-    DLSYM(__android_log_write_log_message)
-    DLSYM(__android_log_logd_logger)
-    DLSYM(__android_log_stderr_logger)
-    DLSYM(__android_log_set_aborter)
-    DLSYM(__android_log_call_aborter)
-    DLSYM(__android_log_default_aborter)
-    DLSYM(__android_log_set_minimum_priority);
-    DLSYM(__android_log_get_minimum_priority);
-    DLSYM(__android_log_set_default_tag);
-#undef DLSYM
-
-    return real_liblog_functions;
-  }();
-
-  return liblog_functions;
-}
-
-#else
-
-const std::optional<LibLogFunctions>& GetLibLogFunctions() {
-  static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
-    return LibLogFunctions{
-        .__android_log_set_logger = __android_log_set_logger,
-        .__android_log_write_log_message = __android_log_write_log_message,
-        .__android_log_logd_logger = __android_log_logd_logger,
-        .__android_log_stderr_logger = __android_log_stderr_logger,
-        .__android_log_set_aborter = __android_log_set_aborter,
-        .__android_log_call_aborter = __android_log_call_aborter,
-        .__android_log_default_aborter = __android_log_default_aborter,
-        .__android_log_set_minimum_priority = __android_log_set_minimum_priority,
-        .__android_log_get_minimum_priority = __android_log_get_minimum_priority,
-        .__android_log_set_default_tag = __android_log_set_default_tag,
-    };
-  }();
-  return liblog_functions;
-}
-
-#endif
-
-}  // namespace base
-}  // namespace android
diff --git a/liblog_symbols.h b/liblog_symbols.h
deleted file mode 100644
index 2e6b47f..0000000
--- a/liblog_symbols.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (C) 2020 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include <optional>
-
-#include <android/log.h>
-
-namespace android {
-namespace base {
-
-struct LibLogFunctions {
-  void (*__android_log_set_logger)(__android_logger_function logger);
-  void (*__android_log_write_log_message)(struct __android_log_message* log_message);
-
-  void (*__android_log_logd_logger)(const struct __android_log_message* log_message);
-  void (*__android_log_stderr_logger)(const struct __android_log_message* log_message);
-
-  void (*__android_log_set_aborter)(__android_aborter_function aborter);
-  void (*__android_log_call_aborter)(const char* abort_message);
-  void (*__android_log_default_aborter)(const char* abort_message);
-  int32_t (*__android_log_set_minimum_priority)(int32_t priority);
-  int32_t (*__android_log_get_minimum_priority)();
-  void (*__android_log_set_default_tag)(const char* tag);
-};
-
-const std::optional<LibLogFunctions>& GetLibLogFunctions();
-
-}  // namespace base
-}  // namespace android
diff --git a/logging.cpp b/logging.cpp
index d6ecc78..54f3fcc 100644
--- a/logging.cpp
+++ b/logging.cpp
@@ -60,7 +60,6 @@
 #include <android-base/strings.h>
 #include <android-base/threads.h>
 
-#include "liblog_symbols.h"
 #include "logging_splitters.h"
 
 namespace android {
@@ -214,9 +213,8 @@
 static std::string* gDefaultTag;
 
 void SetDefaultTag(const std::string& tag) {
-  static auto& liblog_functions = GetLibLogFunctions();
-  if (liblog_functions) {
-    liblog_functions->__android_log_set_default_tag(tag.c_str());
+  if (__builtin_available(android 30, *)) {
+    __android_log_set_default_tag(tag.c_str());
   } else {
     std::lock_guard<std::recursive_mutex> lock(TagLock());
     if (gDefaultTag != nullptr) {
@@ -318,11 +316,10 @@
   int32_t lg_id = LogIdTolog_id_t(id);
   int32_t priority = LogSeverityToPriority(severity);
 
-  static auto& liblog_functions = GetLibLogFunctions();
-  if (liblog_functions) {
+  if (__builtin_available(android 30, *)) {
     __android_log_message log_message = {sizeof(__android_log_message),     lg_id, priority, tag,
                                          static_cast<const char*>(nullptr), 0,     message};
-    liblog_functions->__android_log_logd_logger(&log_message);
+    __android_log_logd_logger(&log_message);
   } else {
     __android_log_buf_print(lg_id, priority, tag, "%s", message);
   }
@@ -401,9 +398,8 @@
   LogFunction old_logger = std::move(Logger());
   Logger() = std::move(logger);
 
-  static auto& liblog_functions = GetLibLogFunctions();
-  if (liblog_functions) {
-    liblog_functions->__android_log_set_logger([](const struct __android_log_message* log_message) {
+  if (__builtin_available(android 30, *)) {
+    __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);
 
@@ -418,10 +414,8 @@
   AbortFunction old_aborter = std::move(Aborter());
   Aborter() = std::move(aborter);
 
-  static auto& liblog_functions = GetLibLogFunctions();
-  if (liblog_functions) {
-    liblog_functions->__android_log_set_aborter(
-        [](const char* abort_message) { Aborter()(abort_message); });
+  if (__builtin_available(android 30, *)) {
+    __android_log_set_aborter([](const char* abort_message) { Aborter()(abort_message); });
   }
   return old_aborter;
 }
@@ -508,9 +502,8 @@
 
   // Abort if necessary.
   if (data_->GetSeverity() == FATAL) {
-    static auto& liblog_functions = GetLibLogFunctions();
-    if (liblog_functions) {
-      liblog_functions->__android_log_call_aborter(msg.c_str());
+    if (__builtin_available(android 30, *)) {
+      __android_log_call_aborter(msg.c_str());
     } else {
       Aborter()(msg.c_str());
     }
@@ -523,12 +516,11 @@
 
 void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity severity, const char* tag,
                          const char* message) {
-  static auto& liblog_functions = GetLibLogFunctions();
   int32_t priority = LogSeverityToPriority(severity);
-  if (liblog_functions) {
+  if (__builtin_available(android 30, *)) {
     __android_log_message log_message = {
         sizeof(__android_log_message), LOG_ID_DEFAULT, priority, tag, file, line, message};
-    liblog_functions->__android_log_write_log_message(&log_message);
+    __android_log_write_log_message(&log_message);
   } else {
     if (tag == nullptr) {
       std::lock_guard<std::recursive_mutex> lock(TagLock());
@@ -544,20 +536,18 @@
 }
 
 LogSeverity GetMinimumLogSeverity() {
-  static auto& liblog_functions = GetLibLogFunctions();
-  if (liblog_functions) {
-    return PriorityToLogSeverity(liblog_functions->__android_log_get_minimum_priority());
+  if (__builtin_available(android 30, *)) {
+    return PriorityToLogSeverity(__android_log_get_minimum_priority());
   } else {
     return gMinimumLogSeverity;
   }
 }
 
 bool ShouldLog(LogSeverity severity, const char* tag) {
-  static auto& liblog_functions = GetLibLogFunctions();
   // Even though we're not using the R liblog functions in this function, if we're running on Q,
   // we need to fall back to using gMinimumLogSeverity, since __android_log_is_loggable() will not
   // take into consideration the value from SetMinimumLogSeverity().
-  if (liblog_functions) {
+  if (__builtin_available(android 30, *)) {
     int32_t priority = LogSeverityToPriority(severity);
     return __android_log_is_loggable(priority, tag, ANDROID_LOG_INFO);
   } else {
@@ -566,10 +556,9 @@
 }
 
 LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) {
-  static auto& liblog_functions = GetLibLogFunctions();
-  if (liblog_functions) {
+  if (__builtin_available(android 30, *)) {
     int32_t priority = LogSeverityToPriority(new_severity);
-    return PriorityToLogSeverity(liblog_functions->__android_log_set_minimum_priority(priority));
+    return PriorityToLogSeverity(__android_log_set_minimum_priority(priority));
   } else {
     LogSeverity old_severity = gMinimumLogSeverity;
     gMinimumLogSeverity = new_severity;