Merge "Ensure btm_log_history is constructed" am: acc4f79265 am: 51fe97fd15 am: 174fa64285 am: 5d8760c0e9 am: e55ad2eda1

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2078142

Bug: 229906389
Tag: #refactor
Test: gd/cert/run
BYPASS_LONG_LINES_REASON: Bluetooth likes 120 lines

Change-Id: I040adfcb85b36144367b5d316859c8568d3a821e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
(cherry picked from commit d8e5d239f6042a7ee4cdc31bda251146ea6bbe66)
diff --git a/system/stack/btm/btm_main.cc b/system/stack/btm/btm_main.cc
index 1344560..3d407ca 100644
--- a/system/stack/btm/btm_main.cc
+++ b/system/stack/btm/btm_main.cc
@@ -27,6 +27,7 @@
 
 #include "bt_target.h"
 #include "main/shim/dumpsys.h"
+#include "osi/include/log.h"
 #include "stack/btm/btm_int_types.h"
 #include "stack/include/btm_client_interface.h"
 #include "stack_config.h"
@@ -64,6 +65,11 @@
 
 static void btm_log_history(const std::string& tag, const char* addr,
                             const std::string& msg, const std::string& extra) {
+  if (btm_cb.history_ == nullptr) {
+    LOG_ERROR("BTM_LogHistory has not been constructed or already destroyed !");
+    return;
+  }
+
   btm_cb.history_->Push(
       "%-6s %-25s: %s %s", tag.substr(0, kMaxLogHistoryTagLength).c_str(),
       msg.substr(0, kMaxLogHistoryMsgLength).c_str(), addr, extra.c_str());