Enable libbase logging for whole stack

Currently, only C++ code in the service/ folder can use libbase logging.
This patch makes sure that logging can be used and is properly
configured for usage when the stack is running as part of Bluetooth.apk.

Bug: 31806042
Change-Id: I1f8be79ba9999b53ece0b5217a893b4bd20ecafc
diff --git a/conf/bt_stack.conf b/conf/bt_stack.conf
index a0afcb5..6654943 100644
--- a/conf/bt_stack.conf
+++ b/conf/bt_stack.conf
@@ -40,6 +40,12 @@
 TRC_BNEP=2
 TRC_PAN=2
 
+# This is Log configuration for new C++ code using LOG() macros.
+# See libchrome/base/logging.h for description on how to configure your logs.
+# sample configuration:
+#LoggingV=--v=0
+#LoggingVModule=--vmodule=*/btm/*=1,btm_ble_multi*=2,btif_*=1
+
 # PTS testing helpers
 
 # Secure connections only mode.
diff --git a/main/Android.mk b/main/Android.mk
index 417c0cf..11a61d7 100644
--- a/main/Android.mk
+++ b/main/Android.mk
@@ -4,11 +4,28 @@
 # ========================================================
 include $(CLEAR_VARS)
 
+LOCAL_CPP_EXTENSION := .cc
+
+#
+# Workaround for libchrome and -DNDEBUG usage.
+#
+# Test whether the original TARGET_GLOBAL_CFLAGS contains -DNDEBUG.
+# This is needed as a workaround to make sure that
+# libchrome and local files calling logging::InitLogging()
+# are consistent with the usage of -DNDEBUG .
+# ========================================================
+ifneq (,$(findstring NDEBUG,$(TARGET_GLOBAL_CFLAGS)))
+  btmain_orig_TARGET_NDEBUG := -DBT_LIBCHROME_NDEBUG
+else
+  btmain_orig_TARGET_NDEBUG :=
+endif
+
 # platform specific
 LOCAL_SRC_FILES := \
 	bte_main.c \
 	bte_init.c \
 	bte_logmsg.c \
+	bte_init_cpp_logging.cc \
 	bte_conf.c \
 	stack_config.c
 
@@ -101,7 +118,7 @@
     libbt-hci \
     libbt-vendor
 
-LOCAL_CFLAGS += $(bluetooth_CFLAGS) -DBUILDCFG
+LOCAL_CFLAGS += $(bluetooth_CFLAGS) -DBUILDCFG $(btmain_orig_TARGET_NDEBUG)
 LOCAL_CONLYFLAGS += $(bluetooth_CONLYFLAGS)
 LOCAL_CPPFLAGS += $(bluetooth_CPPFLAGS)
 
diff --git a/main/bte_init_cpp_logging.cc b/main/bte_init_cpp_logging.cc
new file mode 100644
index 0000000..cd531bb
--- /dev/null
+++ b/main/bte_init_cpp_logging.cc
@@ -0,0 +1,60 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2016 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.
+ *
+ ******************************************************************************/
+
+#ifdef BT_LIBCHROME_NDEBUG
+#define NDEBUG 1
+#endif
+
+#include <base/command_line.h>
+#include "main_int.h"
+
+void init_cpp_logging(config_t *config) {
+  // Command line and log level might be also configured in service/main.cpp
+  // when running the bluetoothtbd daemon. If it's already configured, skip
+  // configuring.
+  if (base::CommandLine::InitializedForCurrentProcess()) return;
+
+  const char *loggingV =
+      config_get_string(config, CONFIG_DEFAULT_SECTION, "LoggingV", NULL);
+  const char *loggingVModule =
+      config_get_string(config, CONFIG_DEFAULT_SECTION, "LoggingVModule", NULL);
+
+  int argc = 1;
+  const char *argv[] = {"bt_stack", NULL, NULL};
+
+  if (loggingV != NULL) {
+    argv[argc] = loggingV;
+    argc++;
+  }
+
+  if (loggingVModule != NULL) {
+    argv[argc] = loggingVModule;
+    argc++;
+  }
+
+  // Init command line object with logging switches
+  base::CommandLine::Init(argc, argv);
+
+  logging::LoggingSettings log_settings;
+  if (!logging::InitLogging(log_settings)) {
+    LOG(ERROR) << "Failed to set up logging";
+  }
+
+  // Android already logs thread_id, proc_id, timestamp, so disable those.
+  logging::SetLogItems(false, false, false, false);
+}
\ No newline at end of file
diff --git a/main/bte_logmsg.c b/main/bte_logmsg.c
index f988011..dd92b9d 100644
--- a/main/bte_logmsg.c
+++ b/main/bte_logmsg.c
@@ -40,6 +40,7 @@
 #include "port_api.h"
 #include "sdp_api.h"
 #include "stack_config.h"
+#include "main_int.h"
 
 #if (AVDT_INCLUDED == TRUE)
 #include "avdt_api.h"
@@ -231,12 +232,15 @@
 }
 
 static future_t *init(void) {
+
   const stack_config_t *stack_config = stack_config_get_interface();
   if (!stack_config->get_trace_config_enabled()) {
     LOG_INFO(LOG_TAG, "using compile default trace settings");
     return NULL;
   }
 
+  init_cpp_logging(stack_config->get_all());
+
   load_levels_from_config(stack_config->get_all());
   return NULL;
 }
diff --git a/main/main_int.h b/main/main_int.h
new file mode 100644
index 0000000..dc821bc
--- /dev/null
+++ b/main/main_int.h
@@ -0,0 +1,35 @@
+/******************************************************************************
+ *
+ *  Copyright (C) 2016 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.
+ *
+ ******************************************************************************/
+
+#ifndef MAIN_INT_H
+#define MAIN_INT_H
+
+#include "osi/include/config.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Initiates the logging for C++ */
+void init_cpp_logging(config_t *config);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // MAIN_INT_H
\ No newline at end of file