From 75b83661eaecceeb48c73891ced70904bf0ce01b Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Tue, 27 Sep 2016 18:24:59 -0700 Subject: [PATCH] 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 --- conf/bt_stack.conf | 6 +++++ main/Android.mk | 19 +++++++++++++- main/bte_init_cpp_logging.cc | 60 ++++++++++++++++++++++++++++++++++++++++++++ main/bte_logmsg.c | 4 +++ main/main_int.h | 35 ++++++++++++++++++++++++++ 5 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 main/bte_init_cpp_logging.cc create mode 100644 main/main_int.h diff --git a/conf/bt_stack.conf b/conf/bt_stack.conf index a0afcb546..6654943c2 100644 --- a/conf/bt_stack.conf +++ b/conf/bt_stack.conf @@ -40,6 +40,12 @@ TRC_GAP=2 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 417c0cf04..11a61d7c7 100644 --- a/main/Android.mk +++ b/main/Android.mk @@ -4,11 +4,28 @@ LOCAL_PATH:= $(call my-dir) # ======================================================== 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 @@ LOCAL_REQUIRED_MODULES := \ 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 000000000..cd531bb15 --- /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 +#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 f98801138..dd92b9ddc 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 void load_levels_from_config(const config_t *config) { } 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 000000000..dc821bc51 --- /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 -- 2.11.0