diff options
author | 2024-03-20 22:35:43 +0000 | |
---|---|---|
committer | 2024-03-21 21:59:16 +0000 | |
commit | f35cfe0b2f0a15ceb4df35e3f7ef2b1c19529564 (patch) | |
tree | a7391dd88333e4cc3c7bb6f0eebdfd9351a5b10c | |
parent | ea107cc4079e295d3cf0e72d3975da0fc8016495 (diff) |
system/common: Migrate to libbluetooth_log
Test: m com.android.btservices
Bug: 305066880
Flag: EXEMPT, mechanical refactor
Change-Id: I04bae75c77e7fd421884f5ff122c794cf229d44e
-rw-r--r-- | system/audio/Android.bp | 3 | ||||
-rw-r--r-- | system/common/BUILD.gn | 1 | ||||
-rw-r--r-- | system/common/leaky_bonded_queue_unittest.cc | 11 | ||||
-rw-r--r-- | system/common/lru.h | 7 | ||||
-rw-r--r-- | system/common/message_loop_thread.cc | 34 | ||||
-rw-r--r-- | system/common/message_loop_thread.h | 7 | ||||
-rw-r--r-- | system/common/message_loop_thread_unittest.cc | 28 | ||||
-rw-r--r-- | system/common/metric_id_allocator.cc | 37 | ||||
-rw-r--r-- | system/common/metrics.cc | 198 | ||||
-rw-r--r-- | system/common/metrics_linux.cc | 3 | ||||
-rw-r--r-- | system/common/repeating_timer.cc | 27 | ||||
-rw-r--r-- | system/common/stop_watch_legacy.cc | 33 | ||||
-rw-r--r-- | system/common/test/thread_performance_test.cc | 27 | ||||
-rw-r--r-- | system/stack/mmc/BUILD.gn | 20 |
14 files changed, 243 insertions, 193 deletions
diff --git a/system/audio/Android.bp b/system/audio/Android.bp index 9eb0883791..f41e44ca40 100644 --- a/system/audio/Android.bp +++ b/system/audio/Android.bp @@ -28,6 +28,7 @@ cc_library_static { ], shared_libs: [ "libchrome", + "liblog", ], static_libs: [ "libbase", @@ -64,6 +65,7 @@ cc_library_host_shared { "libbluetooth_headers", ], static_libs: [ + "libbase", "libbluetooth_hci_pdl", "libbluetooth_log", "libbt-common", @@ -72,6 +74,7 @@ cc_library_host_shared { "libevent", "libflatbuffers-cpp", "libgmock", + "liblog", ], stl: "libc++_static", generated_headers: [ diff --git a/system/common/BUILD.gn b/system/common/BUILD.gn index 58c3a9d40a..52c14aaa0f 100644 --- a/system/common/BUILD.gn +++ b/system/common/BUILD.gn @@ -62,6 +62,7 @@ if (use.test) { "//bt/system:external_gtest_main", "//bt/system:external_gmock_main", "//bt/system:target_defaults", + "//bt/system/log:log_defaults", ] libs = [ diff --git a/system/common/leaky_bonded_queue_unittest.cc b/system/common/leaky_bonded_queue_unittest.cc index b2ff35b96b..c2e6abfed7 100644 --- a/system/common/leaky_bonded_queue_unittest.cc +++ b/system/common/leaky_bonded_queue_unittest.cc @@ -15,16 +15,17 @@ * limitations under the License. * ******************************************************************************/ -#include <gmock/gmock.h> -#include <gtest/gtest.h> +#include "common/leaky_bonded_queue.h" #include <base/logging.h> - -#include "common/leaky_bonded_queue.h" +#include <bluetooth/log.h> +#include <gmock/gmock.h> +#include <gtest/gtest.h> namespace testing { using bluetooth::common::LeakyBondedQueue; +using namespace bluetooth; #define ITEM_EQ(a, b) \ do { \ @@ -72,7 +73,7 @@ TEST(LeakyBondedQueueTest, TestEnqueueDequeue) { ITEM_EQ(item3_3, item3); EXPECT_THAT(item4_4, NotNull()); ITEM_EQ(item4_4, item4); - LOG(INFO) << "All done release items"; + log::info("All done release items"); EXPECT_CALL(*item2_2, Destruct()).Times(1); delete item2_2; EXPECT_CALL(*item3_3, Destruct()).Times(1); diff --git a/system/common/lru.h b/system/common/lru.h index 91d564819a..1c685a6185 100644 --- a/system/common/lru.h +++ b/system/common/lru.h @@ -18,6 +18,9 @@ #pragma once +#include <base/logging.h> +#include <bluetooth/log.h> + #include <functional> #include <iterator> #include <list> @@ -26,8 +29,6 @@ #include <thread> #include <unordered_map> -#include <base/logging.h> - #include "check.h" namespace bluetooth { @@ -48,7 +49,7 @@ class LegacyLruCache { : capacity_(capacity) { if (capacity_ == 0) { // don't allow invalid capacity - LOG(FATAL) << log_tag << " unable to have 0 LRU Cache capacity"; + log::fatal("{} unable to have 0 LRU Cache capacity", log_tag); } } diff --git a/system/common/message_loop_thread.cc b/system/common/message_loop_thread.cc index 9eb305f5c2..dcfc8c5175 100644 --- a/system/common/message_loop_thread.cc +++ b/system/common/message_loop_thread.cc @@ -21,6 +21,7 @@ #include <base/logging.h> #include <base/strings/stringprintf.h> #include <base/time/time.h> +#include <bluetooth/log.h> #include <sys/syscall.h> #include <unistd.h> @@ -62,7 +63,7 @@ void MessageLoopThread::StartUp() { { std::lock_guard<std::recursive_mutex> api_lock(api_mutex_); if (thread_ != nullptr) { - LOG(WARNING) << __func__ << ": thread " << *this << " is already started"; + log::warn("thread {} is already started", *this); return; } @@ -84,15 +85,14 @@ bool MessageLoopThread::DoInThreadDelayed(const base::Location& from_here, std::lock_guard<std::recursive_mutex> api_lock(api_mutex_); if (message_loop_ == nullptr) { - LOG(ERROR) << __func__ << ": message loop is null for thread " << *this - << ", from " << from_here.ToString(); + log::error("message loop is null for thread {}, from {}", *this, + from_here.ToString()); return false; } if (!message_loop_->task_runner()->PostDelayedTask( from_here, std::move(task), timeDeltaFromMicroseconds(delay))) { - LOG(ERROR) << __func__ - << ": failed to post task to message loop for thread " << *this - << ", from " << from_here.ToString(); + log::error("failed to post task to message loop for thread {}, from {}", + *this, from_here.ToString()); return false; } return true; @@ -102,15 +102,15 @@ void MessageLoopThread::ShutDown() { { std::lock_guard<std::recursive_mutex> api_lock(api_mutex_); if (thread_ == nullptr) { - LOG(INFO) << __func__ << ": thread " << *this << " is already stopped"; + log::info("thread {} is already stopped", *this); return; } if (message_loop_ == nullptr) { - LOG(INFO) << __func__ << ": message_loop_ is null. Already stopping"; + log::info("message_loop_ is null. Already stopping"); return; } if (shutting_down_) { - LOG(INFO) << __func__ << ": waiting for thread to join"; + log::info("waiting for thread to join"); return; } shutting_down_ = true; @@ -161,7 +161,7 @@ bool MessageLoopThread::EnableRealTimeScheduling() { std::lock_guard<std::recursive_mutex> api_lock(api_mutex_); if (!IsRunning()) { - LOG(ERROR) << __func__ << ": thread " << *this << " is not running"; + log::error("thread {} is not running", *this); return false; } @@ -169,10 +169,10 @@ bool MessageLoopThread::EnableRealTimeScheduling() { kRealTimeFifoSchedulingPriority}; int rc = sched_setscheduler(linux_tid_, SCHED_FIFO, &rt_params); if (rc != 0) { - LOG(ERROR) << __func__ << ": unable to set SCHED_FIFO priority " - << kRealTimeFifoSchedulingPriority << " for linux_tid " - << std::to_string(linux_tid_) << ", thread " << *this - << ", error: " << strerror(errno); + log::error( + "unable to set SCHED_FIFO priority {} for linux_tid {}, thread {}, " + "error: {}", + kRealTimeFifoSchedulingPriority, linux_tid_, *this, strerror(errno)); return false; } return true; @@ -187,8 +187,7 @@ void MessageLoopThread::Run(std::promise<void> start_up_promise) { { std::lock_guard<std::recursive_mutex> api_lock(api_mutex_); - LOG(INFO) << __func__ << ": message loop starting for thread " - << thread_name_; + log::info("message loop starting for thread {}", thread_name_); base::PlatformThread::SetName(thread_name_); message_loop_ = new btbase::AbstractMessageLoop(); run_loop_ = new base::RunLoop(); @@ -208,8 +207,7 @@ void MessageLoopThread::Run(std::promise<void> start_up_promise) { message_loop_ = nullptr; delete run_loop_; run_loop_ = nullptr; - LOG(INFO) << __func__ << ": message loop finished for thread " - << thread_name_; + log::info("message loop finished for thread {}", thread_name_); } } diff --git a/system/common/message_loop_thread.h b/system/common/message_loop_thread.h index 5c8b00efa8..3c43dafe3d 100644 --- a/system/common/message_loop_thread.h +++ b/system/common/message_loop_thread.h @@ -20,6 +20,7 @@ #include <base/location.h> #include <base/run_loop.h> #include <base/threading/platform_thread.h> +#include <bluetooth/log.h> #include <unistd.h> #include <future> @@ -217,5 +218,9 @@ inline std::ostream& operator<<(std::ostream& os, } } // namespace common - } // namespace bluetooth + +namespace fmt { +template <> +struct formatter<bluetooth::common::MessageLoopThread> : ostream_formatter {}; +} // namespace fmt diff --git a/system/common/message_loop_thread_unittest.cc b/system/common/message_loop_thread_unittest.cc index dec3045e91..e904e3fcc5 100644 --- a/system/common/message_loop_thread_unittest.cc +++ b/system/common/message_loop_thread_unittest.cc @@ -15,18 +15,19 @@ */ #include "message_loop_thread.h" -#include <condition_variable> -#include <memory> -#include <mutex> - -#include <gtest/gtest.h> - #include <base/functional/bind.h> #include <base/threading/platform_thread.h> +#include <bluetooth/log.h> +#include <gtest/gtest.h> #include <sys/capability.h> #include <syscall.h> +#include <condition_variable> +#include <memory> +#include <mutex> + using bluetooth::common::MessageLoopThread; +using namespace bluetooth; /** * Unit tests to verify MessageLoopThread. Must have CAP_SYS_NICE capability. @@ -70,8 +71,8 @@ class MessageLoopThreadTest : public ::testing::Test { .version = _LINUX_CAPABILITY_VERSION_3}; struct __user_cap_data_struct linux_user_data[2] = {}; if (capget(&linux_user_header, linux_user_data) != 0) { - LOG(ERROR) << "Failed to get capability for current thread, error: " - << strerror(errno); + log::error("Failed to get capability for current thread, error: {}", + strerror(errno)); // Log record in XML RecordProperty("MessageLoopThreadTestCannotGetCapabilityReason", strerror(errno)); @@ -181,8 +182,9 @@ TEST_F(MessageLoopThreadTest, test_set_realtime_priority_success) { if (CanSetCurrentThreadPriority()) { FAIL() << "Cannot set real time priority even though we have permission"; } else { - LOG(WARNING) << "Allowing EnableRealTimeScheduling to fail because we" - " don't have CAP_SYS_NICE capability"; + log::warn( + "Allowing EnableRealTimeScheduling to fail because we don't have " + "CAP_SYS_NICE capability"); // Log record in XML RecordProperty("MessageLoopThreadTestConditionalSuccess", "Mark test as success even though EnableRealTimeScheduling" @@ -246,17 +248,17 @@ TEST_F(MessageLoopThreadTest, test_to_string_method) { MessageLoopThread message_loop_thread(name); std::string thread_string_before_start = message_loop_thread.ToString(); ASSERT_FALSE(thread_string_before_start.empty()); - LOG(INFO) << "Before start: " << message_loop_thread; + log::info("Before start: {}", message_loop_thread); message_loop_thread.StartUp(); std::string thread_string_running = message_loop_thread.ToString(); ASSERT_FALSE(thread_string_running.empty()); - LOG(INFO) << "Running: " << message_loop_thread; + log::info("Running: {}", message_loop_thread); // String representation should look different when thread is not running ASSERT_STRNE(thread_string_running.c_str(), thread_string_before_start.c_str()); message_loop_thread.ShutDown(); std::string thread_string_after_shutdown = message_loop_thread.ToString(); - LOG(INFO) << "After shutdown: " << message_loop_thread; + log::info("After shutdown: {}", message_loop_thread); // String representation should look the same when thread is not running ASSERT_STREQ(thread_string_after_shutdown.c_str(), thread_string_before_start.c_str()); diff --git a/system/common/metric_id_allocator.cc b/system/common/metric_id_allocator.cc index 09b9a2647f..ea4ac00614 100644 --- a/system/common/metric_id_allocator.cc +++ b/system/common/metric_id_allocator.cc @@ -19,6 +19,7 @@ #include "metric_id_allocator.h" #include <base/logging.h> +#include <bluetooth/log.h> #include <functional> #include <mutex> @@ -58,9 +59,9 @@ bool MetricIdAllocator::Init( // init paired_devices_map if (paired_device_map.size() > kMaxNumPairedDevicesInMemory) { - LOG(FATAL) - << LOGGING_TAG - << "Paired device map is bigger than kMaxNumPairedDevicesInMemory"; + log::fatal( + "{}Paired device map is bigger than kMaxNumPairedDevicesInMemory", + LOGGING_TAG); // fail loudly to let caller know return false; } @@ -68,7 +69,7 @@ bool MetricIdAllocator::Init( next_id_ = kMinId; for (const auto& p : paired_device_map) { if (p.second < kMinId || p.second > kMaxId) { - LOG(FATAL) << LOGGING_TAG << "Invalid Bluetooth Metric Id in config"; + log::fatal("{}Invalid Bluetooth Metric Id in config", LOGGING_TAG); } auto evicted = paired_device_cache_.Put(p.first, p.second); if (evicted) { @@ -130,7 +131,7 @@ int MetricIdAllocator::AllocateId(const RawAddress& mac_address) { next_id_++; if (next_id_ > kMaxId) { next_id_ = kMinId; - LOG(WARNING) << LOGGING_TAG << "Bluetooth metric id overflow."; + log::warn("{}Bluetooth metric id overflow.", LOGGING_TAG); } } id = next_id_++; @@ -154,14 +155,15 @@ bool MetricIdAllocator::SaveDevice(const RawAddress& mac_address) { return true; } if (!temporary_device_cache_.Get(mac_address, &id)) { - LOG(ERROR) << LOGGING_TAG - << "Failed to save device because device is not in " - << "temporary_device_cache_"; + log::error( + "{}Failed to save device because device is not in " + "temporary_device_cache_", + LOGGING_TAG); return false; } if (!temporary_device_cache_.Remove(mac_address)) { - LOG(ERROR) << LOGGING_TAG - << "Failed to remove device from temporary_device_cache_"; + log::error("{}Failed to remove device from temporary_device_cache_", + LOGGING_TAG); return false; } auto evicted = paired_device_cache_.Put(mac_address, id); @@ -169,8 +171,8 @@ bool MetricIdAllocator::SaveDevice(const RawAddress& mac_address) { ForgetDevicePostprocess(evicted->first, evicted->second); } if (!save_id_callback_(mac_address, id)) { - LOG(ERROR) << LOGGING_TAG - << "Callback returned false after saving the device"; + log::error("{}Callback returned false after saving the device", + LOGGING_TAG); return false; } return true; @@ -181,14 +183,15 @@ void MetricIdAllocator::ForgetDevice(const RawAddress& mac_address) { std::lock_guard<std::mutex> lock(id_allocator_mutex_); int id = 0; if (!paired_device_cache_.Get(mac_address, &id)) { - LOG(ERROR) << LOGGING_TAG - << "Failed to forget device because device is not in " - << "paired_device_cache_"; + log::error( + "{}Failed to forget device because device is not in " + "paired_device_cache_", + LOGGING_TAG); return; } if (!paired_device_cache_.Remove(mac_address)) { - LOG(ERROR) << LOGGING_TAG - << "Failed to remove device from paired_device_cache_"; + log::error("{}Failed to remove device from paired_device_cache_", + LOGGING_TAG); return; } ForgetDevicePostprocess(mac_address, id); diff --git a/system/common/metrics.cc b/system/common/metrics.cc index b9a1e2d180..9397aae1f7 100644 --- a/system/common/metrics.cc +++ b/system/common/metrics.cc @@ -20,6 +20,7 @@ #include <base/base64.h> #include <base/logging.h> +#include <bluetooth/log.h> #include <frameworks/proto_logging/stats/enums/bluetooth/le/enums.pb.h> #include <include/hardware/bt_av.h> #include <statslog_bt.h> @@ -45,8 +46,25 @@ #include "time_util.h" #include "types/raw_address.h" -namespace bluetooth { +namespace fmt { +template <> +struct formatter<android::bluetooth::DirectionEnum> + : enum_formatter<android::bluetooth::DirectionEnum> {}; +template <> +struct formatter<android::bluetooth::SocketConnectionstateEnum> + : enum_formatter<android::bluetooth::SocketConnectionstateEnum> {}; +template <> +struct formatter<android::bluetooth::SocketRoleEnum> + : enum_formatter<android::bluetooth::SocketRoleEnum> {}; +template <> +struct formatter<android::bluetooth::AddressTypeEnum> + : enum_formatter<android::bluetooth::AddressTypeEnum> {}; +template <> +struct formatter<android::bluetooth::DeviceInfoSrcEnum> + : enum_formatter<android::bluetooth::DeviceInfoSrcEnum> {}; +} // namespace fmt +namespace bluetooth { namespace common { using bluetooth::metrics::BluetoothMetricsProto::A2DPSession; @@ -460,11 +478,11 @@ void BluetoothMetricsLogger::LogHeadsetProfileRfcConnection( void BluetoothMetricsLogger::WriteString(std::string* serialized) { std::lock_guard<std::recursive_mutex> lock(pimpl_->bluetooth_log_lock_); - LOG(INFO) << __func__ << ": building metrics"; + log::info("building metrics"); Build(); - LOG(INFO) << __func__ << ": serializing metrics"; + log::info("serializing metrics"); if (!pimpl_->bluetooth_log_->SerializeToString(serialized)) { - LOG(ERROR) << __func__ << ": error serializing metrics"; + log::error("error serializing metrics"); } // Always clean up log objects pimpl_->bluetooth_log_->Clear(); @@ -481,9 +499,7 @@ void BluetoothMetricsLogger::WriteBase64(int fd) { ssize_t ret; OSI_NO_INTR(ret = write(fd, protoBase64.c_str(), protoBase64.size())); if (ret == -1) { - LOG(ERROR) << __func__ - << ": error writing to dumpsys fd: " << strerror(errno) << " (" - << std::to_string(errno) << ")"; + log::error("error writing to dumpsys fd: {} ({})", strerror(errno), errno); } } @@ -595,12 +611,13 @@ void LogLinkLayerConnectionEvent(const RawAddress* address, connection_handle, direction, link_type, hci_cmd, hci_event, hci_ble_event, cmd_status, reason_code, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed to log status " << loghex(cmd_status) - << ", reason " << loghex(reason_code) << " from cmd " - << loghex(hci_cmd) << ", event " << loghex(hci_event) - << ", ble_event " << loghex(hci_ble_event) << " for " - << address << ", handle " << connection_handle << ", type " - << loghex(link_type) << ", error " << ret; + log::warn( + "failed to log status {}, reason {} from cmd {}, event {}, ble_event " + "{} for {}, handle {}, type {}, error {}", + loghex(cmd_status), loghex(reason_code), loghex(hci_cmd), + loghex(hci_event), loghex(hci_ble_event), + ADDRESS_TO_LOGGABLE_STR(*address), connection_handle, loghex(link_type), + ret); } } @@ -608,8 +625,7 @@ void LogHciTimeoutEvent(uint32_t hci_cmd) { int ret = stats_write(BLUETOOTH_HCI_TIMEOUT_REPORTED, static_cast<int64_t>(hci_cmd)); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for opcode " << loghex(hci_cmd) - << ", error " << ret; + log::warn("failed for opcode {}, error {}", loghex(hci_cmd), ret); } } @@ -618,10 +634,11 @@ void LogRemoteVersionInfo(uint16_t handle, uint8_t status, uint8_t version, int ret = stats_write(BLUETOOTH_REMOTE_VERSION_INFO_REPORTED, handle, status, version, manufacturer_name, subversion); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for handle " << handle << ", status " - << loghex(status) << ", version " << loghex(version) - << ", manufacturer_name " << loghex(manufacturer_name) - << ", subversion " << loghex(subversion) << ", error " << ret; + log::warn( + "failed for handle {}, status {}, version {}, manufacturer_name {}, " + "subversion {}, error {}", + handle, loghex(status), loghex(version), loghex(manufacturer_name), + loghex(subversion), ret); } } @@ -642,10 +659,11 @@ void LogA2dpAudioUnderrunEvent(const RawAddress& address, stats_write(BLUETOOTH_A2DP_AUDIO_UNDERRUN_REPORTED, bytes_field, encoding_interval_nanos, num_missing_pcm_bytes, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for " << address - << ", encoding_interval_nanos " << encoding_interval_nanos - << ", num_missing_pcm_bytes " << num_missing_pcm_bytes - << ", error " << ret; + log::warn( + "failed for {}, encoding_interval_nanos {}, num_missing_pcm_bytes {}, " + "error {}", + ADDRESS_TO_LOGGABLE_STR(address), encoding_interval_nanos, + num_missing_pcm_bytes, ret); } } @@ -669,12 +687,13 @@ void LogA2dpAudioOverrunEvent(const RawAddress& address, num_dropped_encoded_frames, num_dropped_encoded_bytes, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed to log for " << address - << ", encoding_interval_nanos " << encoding_interval_nanos - << ", num_dropped_buffers " << num_dropped_buffers - << ", num_dropped_encoded_frames " - << num_dropped_encoded_frames << ", num_dropped_encoded_bytes " - << num_dropped_encoded_bytes << ", error " << ret; + log::warn( + "failed to log for {}, encoding_interval_nanos {}, num_dropped_buffers " + "{}, num_dropped_encoded_frames {}, num_dropped_encoded_bytes {}, " + "error {}", + ADDRESS_TO_LOGGABLE_STR(address), encoding_interval_nanos, + num_dropped_buffers, num_dropped_encoded_frames, + num_dropped_encoded_bytes, ret); } } @@ -692,10 +711,11 @@ void LogA2dpPlaybackEvent(const RawAddress& address, int playback_state, int ret = stats_write(BLUETOOTH_A2DP_PLAYBACK_STATE_CHANGED, bytes_field, playback_state, audio_coding_mode, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed to log for " << address - << ", playback_state " << playback_state - << ", audio_coding_mode " << audio_coding_mode << ", error " - << ret; + log::warn( + "failed to log for {}, playback_state {}, audio_coding_mode {}, error " + "{}", + ADDRESS_TO_LOGGABLE_STR(address), playback_state, audio_coding_mode, + ret); } } @@ -713,9 +733,9 @@ void LogReadRssiResult(const RawAddress& address, uint16_t handle, int ret = stats_write(BLUETOOTH_DEVICE_RSSI_REPORTED, bytes_field, handle, cmd_status, rssi, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for " << address << ", handle " - << handle << ", status " << loghex(cmd_status) << ", rssi " - << rssi << " dBm, error " << ret; + log::warn("failed for {}, handle {}, status {}, rssi {} dBm, error {}", + ADDRESS_TO_LOGGABLE_STR(address), handle, loghex(cmd_status), + rssi, ret); } } @@ -735,10 +755,11 @@ void LogReadFailedContactCounterResult(const RawAddress& address, stats_write(BLUETOOTH_DEVICE_FAILED_CONTACT_COUNTER_REPORTED, bytes_field, handle, cmd_status, failed_contact_counter, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for " << address << ", handle " - << handle << ", status " << loghex(cmd_status) - << ", failed_contact_counter " << failed_contact_counter - << " packets, error " << ret; + log::warn( + "failed for {}, handle {}, status {}, failed_contact_counter {} " + "packets, error {}", + ADDRESS_TO_LOGGABLE_STR(address), handle, loghex(cmd_status), + failed_contact_counter, ret); } } @@ -757,10 +778,11 @@ void LogReadTxPowerLevelResult(const RawAddress& address, uint16_t handle, int ret = stats_write(BLUETOOTH_DEVICE_TX_POWER_LEVEL_REPORTED, bytes_field, handle, cmd_status, transmit_power_level, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for " << address << ", handle " - << handle << ", status " << loghex(cmd_status) - << ", transmit_power_level " << transmit_power_level - << " packets, error " << ret; + log::warn( + "failed for {}, handle {}, status {}, transmit_power_level {} packets, " + "error {}", + ADDRESS_TO_LOGGABLE_STR(address), handle, loghex(cmd_status), + transmit_power_level, ret); } } @@ -781,10 +803,10 @@ void LogSmpPairingEvent(const RawAddress& address, uint8_t smp_cmd, stats_write(BLUETOOTH_SMP_PAIRING_EVENT_REPORTED, obfuscated_id_field, smp_cmd, direction, smp_fail_reason, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for " << address << ", smp_cmd " - << loghex(smp_cmd) << ", direction " << direction - << ", smp_fail_reason " << loghex(smp_fail_reason) - << ", error " << ret; + log::warn( + "failed for {}, smp_cmd {}, direction {}, smp_fail_reason {}, error {}", + ADDRESS_TO_LOGGABLE_STR(address), loghex(smp_cmd), direction, + loghex(smp_fail_reason), ret); } } @@ -804,9 +826,12 @@ void LogClassicPairingEvent(const RawAddress& address, uint16_t handle, uint32_t obfuscated_id_field, handle, hci_cmd, hci_event, cmd_status, reason_code, event_value, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for " << address << ", handle " << handle << ", hci_cmd " << loghex(hci_cmd) - << ", hci_event " << loghex(hci_event) << ", cmd_status " << loghex(cmd_status) << ", reason " - << loghex(reason_code) << ", event_value " << event_value << ", error " << ret; + log::warn( + "failed for {}, handle {}, hci_cmd {}, hci_event {}, cmd_status {}, " + "reason {}, event_value {}, error {}", + ADDRESS_TO_LOGGABLE_STR(address), handle, loghex(hci_cmd), + loghex(hci_event), loghex(cmd_status), loghex(reason_code), event_value, + ret); } } @@ -828,9 +853,9 @@ void LogSdpAttribute(const RawAddress& address, uint16_t protocol_uuid, stats_write(BLUETOOTH_SDP_ATTRIBUTE_REPORTED, obfuscated_id_field, protocol_uuid, attribute_id, attribute_field, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for " << address << ", protocol_uuid " - << loghex(protocol_uuid) << ", attribute_id " - << loghex(attribute_id) << ", error " << ret; + log::warn("failed for {}, protocol_uuid {}, attribute_id {}, error {}", + ADDRESS_TO_LOGGABLE_STR(address), loghex(protocol_uuid), + loghex(attribute_id), ret); } } @@ -854,11 +879,11 @@ void LogSocketConnectionState( obfuscated_id_field, port, type, connection_state, tx_bytes, rx_bytes, uid, server_port, socket_role, metric_id); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for " << address << ", port " << port - << ", type " << type << ", state " << connection_state - << ", tx_bytes " << tx_bytes << ", rx_bytes " << rx_bytes - << ", uid " << uid << ", server_port " << server_port - << ", socket_role " << socket_role << ", error " << ret; + log::warn( + "failed for {}, port {}, type {}, state {}, tx_bytes {}, rx_bytes {}, " + "uid {}, server_port {}, socket_role {}, error {}", + ADDRESS_TO_LOGGABLE_STR(address), port, type, connection_state, + tx_bytes, rx_bytes, uid, server_port, socket_role, ret); } } @@ -886,15 +911,13 @@ void LogManufacturerInfo(const RawAddress& address, hardware_version.c_str(), software_version.c_str(), metric_id, address_type, address.address[5], address.address[4], address.address[3]); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for " << address << ", source_type " - << source_type << ", source_name " << source_name - << ", manufacturer " << manufacturer << ", model " << model - << ", hardware_version " << hardware_version - << ", software_version " << software_version - << " MAC address type " << address_type - << " MAC address prefix " << address.address[5] << " " - << address.address[4] << " " << address.address[3] << ", error " - << ret; + log::warn( + "failed for {}, source_type {}, source_name {}, manufacturer {}, model " + "{}, hardware_version {}, software_version {} MAC address type {} MAC " + "address prefix {} {} {}, error {}", + ADDRESS_TO_LOGGABLE_STR(address), source_type, source_name, + manufacturer, model, hardware_version, software_version, address_type, + address.address[5], address.address[4], address.address[3], ret); } } @@ -911,9 +934,9 @@ void LogBluetoothHalCrashReason(const RawAddress& address, uint32_t error_code, int ret = stats_write(BLUETOOTH_HAL_CRASH_REASON_REPORTED, 0, obfuscated_id_field, error_code, vendor_error_code); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for " << address << ", error_code " - << loghex(error_code) << ", vendor_error_code " - << loghex(vendor_error_code) << ", error " << ret; + log::warn("failed for {}, error_code {}, vendor_error_code {}, error {}", + ADDRESS_TO_LOGGABLE_STR(address), loghex(error_code), + loghex(vendor_error_code), ret); } } @@ -945,31 +968,26 @@ void LogLeAudioConnectionSessionReported( device_connection_status, device_disconnection_status, device_metric_id, streaming_offset_nanos, streaming_duration_nanos, streaming_context_type); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for group " << group_metric_id - << "device_connecting_offset_nanos[" - << device_connecting_offset_nanos.size() << "], " - << "device_connected_offset_nanos[" - << device_connected_offset_nanos.size() << "], " - << "device_connection_duration_nanos[" - << device_connection_duration_nanos.size() << "], " - << "device_connection_status[" - << device_connection_status.size() << "], " - << "device_disconnection_status[" - << device_disconnection_status.size() << "], " - << "device_metric_id[" << device_metric_id.size() << "], " - << "streaming_offset_nanos[" << streaming_offset_nanos.size() - << "], " - << "streaming_duration_nanos[" - << streaming_duration_nanos.size() << "], " - << "streaming_context_type[" << streaming_context_type.size() - << "]"; + log::warn( + "failed for group {}device_connecting_offset_nanos[{}], " + "device_connected_offset_nanos[{}], " + "device_connection_duration_nanos[{}], device_connection_status[{}], " + "device_disconnection_status[{}], device_metric_id[{}], " + "streaming_offset_nanos[{}], streaming_duration_nanos[{}], " + "streaming_context_type[{}]", + group_metric_id, device_connecting_offset_nanos.size(), + device_connected_offset_nanos.size(), + device_connection_duration_nanos.size(), + device_connection_status.size(), device_disconnection_status.size(), + device_metric_id.size(), streaming_offset_nanos.size(), + streaming_duration_nanos.size(), streaming_context_type.size()); } } void LogLeAudioBroadcastSessionReported(int64_t duration_nanos) { int ret = stats_write(LE_AUDIO_BROADCAST_SESSION_REPORTED, duration_nanos); if (ret < 0) { - LOG(WARNING) << __func__ << ": failed for duration=" << duration_nanos; + log::warn("failed for duration={}", duration_nanos); } } diff --git a/system/common/metrics_linux.cc b/system/common/metrics_linux.cc index 883a645e30..707e0a54b2 100644 --- a/system/common/metrics_linux.cc +++ b/system/common/metrics_linux.cc @@ -17,6 +17,7 @@ ******************************************************************************/ #include <base/logging.h> +#include <bluetooth/log.h> #include "leaky_bonded_queue.h" #include "metrics.h" @@ -29,7 +30,7 @@ namespace common { void A2dpSessionMetrics::Update(const A2dpSessionMetrics& metrics) {} bool A2dpSessionMetrics::operator==(const A2dpSessionMetrics& rhs) const { - LOG(INFO) << "UNIMPLEMENTED"; + log::info("UNIMPLEMENTED"); return true; } diff --git a/system/common/repeating_timer.cc b/system/common/repeating_timer.cc index 9abea46b72..39825eb3e1 100644 --- a/system/common/repeating_timer.cc +++ b/system/common/repeating_timer.cc @@ -16,11 +16,12 @@ #include "repeating_timer.h" -#include "message_loop_thread.h" -#include "time_util.h" - #include <base/functional/callback.h> #include <base/logging.h> +#include <bluetooth/log.h> + +#include "message_loop_thread.h" +#include "time_util.h" namespace bluetooth { @@ -43,8 +44,7 @@ bool RepeatingTimer::SchedulePeriodic( const base::Location& from_here, base::RepeatingClosure task, std::chrono::microseconds period) { if (period < kMinimumPeriod) { - LOG(ERROR) << __func__ << ": period must be at least " - << kMinimumPeriod.count(); + log::error("period must be at least {}", kMinimumPeriod.count()); return false; } @@ -52,7 +52,7 @@ bool RepeatingTimer::SchedulePeriodic( uint64_t time_next_task_us = time_now_us + period.count(); std::lock_guard<std::recursive_mutex> api_lock(api_mutex_); if (thread == nullptr) { - LOG(ERROR) << __func__ << ": thread must be non-null"; + log::error("thread must be non-null"); return false; } CancelAndWait(); @@ -66,9 +66,8 @@ bool RepeatingTimer::SchedulePeriodic( if (!thread->DoInThreadDelayed( from_here, task_wrapper_.callback(), std::chrono::microseconds(time_until_next_us))) { - LOG(ERROR) << __func__ - << ": failed to post task to message loop for thread " << *thread - << ", from " << from_here.ToString(); + log::error("failed to post task to message loop for thread {}, from {}", + *thread, from_here.ToString()); expected_time_next_task_us_ = 0; task_wrapper_.Cancel(); message_loop_thread_ = nullptr; @@ -132,8 +131,7 @@ bool RepeatingTimer::IsScheduled() const { // This runs on message loop thread void RepeatingTimer::RunTask() { if (message_loop_thread_ == nullptr || !message_loop_thread_->IsRunning()) { - LOG(ERROR) << __func__ - << ": message_loop_thread_ is null or is not running"; + log::error("message_loop_thread_ is null or is not running"); return; } CHECK_EQ(message_loop_thread_->GetThreadId(), @@ -159,9 +157,10 @@ void RepeatingTimer::RunTask() { auto task_time_us = static_cast<int64_t>(time_after_task_us - time_before_task_us); if (task_time_us > period_.count()) { - LOG(ERROR) << __func__ << ": Periodic task execution took " << task_time_us - << " microseconds, longer than interval " << period_.count() - << " microseconds"; + log::error( + "Periodic task execution took {} microseconds, longer than interval {} " + "microseconds", + task_time_us, period_.count()); } } diff --git a/system/common/stop_watch_legacy.cc b/system/common/stop_watch_legacy.cc index 6b783cba03..eb0a05e0d1 100644 --- a/system/common/stop_watch_legacy.cc +++ b/system/common/stop_watch_legacy.cc @@ -19,6 +19,7 @@ #include "common/stop_watch_legacy.h" #include <base/logging.h> +#include <bluetooth/log.h> #include <iomanip> #include <mutex> @@ -39,12 +40,12 @@ static std::recursive_mutex stopwatch_log_mutex; void StopWatchLegacy::RecordLog(StopWatchLog log) { std::unique_lock<std::recursive_mutex> lock(stopwatch_log_mutex, std::defer_lock); if (!lock.try_lock()) { - LOG_INFO("try_lock fail. log content: %s, took %zu us", log.message.c_str(), - static_cast<size_t>( - std::chrono::duration_cast<std::chrono::microseconds>( - stopwatch_logs[current_buffer_index].end_timestamp - - stopwatch_logs[current_buffer_index].start_timestamp) - .count())); + log::info("try_lock fail. log content: {}, took {} us", log.message, + static_cast<size_t>( + std::chrono::duration_cast<std::chrono::microseconds>( + stopwatch_logs[current_buffer_index].end_timestamp - + stopwatch_logs[current_buffer_index].start_timestamp) + .count())); return; } if (current_buffer_index >= LOG_BUFFER_LENGTH) { @@ -57,8 +58,8 @@ void StopWatchLegacy::RecordLog(StopWatchLog log) { void StopWatchLegacy::DumpStopWatchLog() { std::lock_guard<std::recursive_mutex> lock(stopwatch_log_mutex); - LOG_INFO("=-----------------------------------="); - LOG_INFO("bluetooth stopwatch log history:"); + log::info("=-----------------------------------="); + log::info("bluetooth stopwatch log history:"); for (int i = 0; i < LOG_BUFFER_LENGTH; i++) { if (current_buffer_index >= LOG_BUFFER_LENGTH) { current_buffer_index = 0; @@ -76,16 +77,16 @@ void StopWatchLegacy::DumpStopWatchLog() { ss << std::put_time(std::localtime(&now_time_t), "%Y-%m-%d %H:%M:%S"); ss << '.' << std::setfill('0') << std::setw(3) << millis.count(); std::string start_timestamp = ss.str(); - LOG_INFO("%s: %s: took %zu us", start_timestamp.c_str(), - stopwatch_logs[current_buffer_index].message.c_str(), - static_cast<size_t>( - std::chrono::duration_cast<std::chrono::microseconds>( - stopwatch_logs[current_buffer_index].end_timestamp - - stopwatch_logs[current_buffer_index].start_timestamp) - .count())); + log::info("{}: {}: took {} us", start_timestamp, + stopwatch_logs[current_buffer_index].message, + static_cast<size_t>( + std::chrono::duration_cast<std::chrono::microseconds>( + stopwatch_logs[current_buffer_index].end_timestamp - + stopwatch_logs[current_buffer_index].start_timestamp) + .count())); current_buffer_index++; } - LOG_INFO("=-----------------------------------="); + log::info("=-----------------------------------="); } StopWatchLegacy::StopWatchLegacy(std::string text) diff --git a/system/common/test/thread_performance_test.cc b/system/common/test/thread_performance_test.cc index 84eecdf216..7b573064aa 100644 --- a/system/common/test/thread_performance_test.cc +++ b/system/common/test/thread_performance_test.cc @@ -18,9 +18,11 @@ #include <base/logging.h> #include <base/run_loop.h> #include <base/threading/thread.h> +#include <bluetooth/log.h> #include <gmock/gmock.h> #include <gtest/gtest.h> #include <unistd.h> + #include <chrono> #include <future> #include <iostream> @@ -32,6 +34,7 @@ #include "osi/include/thread.h" using bluetooth::common::MessageLoopThread; +using namespace bluetooth; #define NUM_MESSAGES_TO_SEND 100000 @@ -135,8 +138,8 @@ TEST_F(OsiThreadMessageLoopPerformanceTest, message_loop_speed_test) { std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time); - LOG(INFO) << "OsiThreadMessageLoopPerformanceTest, " << duration.count() - << " ms, " << NUM_MESSAGES_TO_SEND << " messages"; + log::info("OsiThreadMessageLoopPerformanceTest, {} ms, {} messages", + duration.count(), NUM_MESSAGES_TO_SEND); } class StlThreadMessageLoopPerformanceTest : public MessageLoopPerformanceTest { @@ -180,8 +183,8 @@ TEST_F(StlThreadMessageLoopPerformanceTest, stl_thread_speed_test) { std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time); - LOG(INFO) << "StlThreadMessageLoopPerformanceTest, " << duration.count() - << " ms, " << NUM_MESSAGES_TO_SEND << " messages"; + log::info("StlThreadMessageLoopPerformanceTest, {} ms, {} messages", + duration.count(), NUM_MESSAGES_TO_SEND); } class PosixThreadMessageLoopPerformanceTest @@ -226,8 +229,8 @@ TEST_F(PosixThreadMessageLoopPerformanceTest, stl_thread_speed_test) { std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time); - LOG(INFO) << "PosixThreadMessageLoopPerformanceTest, " << duration.count() - << " ms, " << NUM_MESSAGES_TO_SEND << " messages"; + log::info("PosixThreadMessageLoopPerformanceTest, {} ms, {} messages", + duration.count(), NUM_MESSAGES_TO_SEND); } class ReactorPerformanceTest : public PerformanceTest { @@ -268,8 +271,8 @@ TEST_F(ReactorPerformanceTest, reactor_thread_speed_test) { start_time); fixed_queue_unregister_dequeue(bt_msg_queue_); - LOG(INFO) << "ReactorPerformanceTest, " << duration.count() << " ms, " - << NUM_MESSAGES_TO_SEND << " messages"; + log::info("ReactorPerformanceTest, {} ms, {} messages", duration.count(), + NUM_MESSAGES_TO_SEND); } class WorkerThreadPerformanceTest : public PerformanceTest { @@ -317,8 +320,8 @@ TEST_F(WorkerThreadPerformanceTest, worker_thread_speed_test) { std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time); - LOG(INFO) << "WorkerThreadPerformanceTest, " << duration.count() << " ms, " - << NUM_MESSAGES_TO_SEND << " messages"; + log::info("WorkerThreadPerformanceTest, {} ms, {} messages", duration.count(), + NUM_MESSAGES_TO_SEND); } class LibChromeThreadPerformanceTest : public PerformanceTest { @@ -365,6 +368,6 @@ TEST_F(LibChromeThreadPerformanceTest, worker_thread_speed_test) { std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time); - LOG(INFO) << "LibChromeThreadPerformanceTest, " << duration.count() << " ms, " - << NUM_MESSAGES_TO_SEND << " messages"; + log::info("LibChromeThreadPerformanceTest, {} ms, {} messages", + duration.count(), NUM_MESSAGES_TO_SEND); } diff --git a/system/stack/mmc/BUILD.gn b/system/stack/mmc/BUILD.gn index 8111db73b6..293803b5a9 100644 --- a/system/stack/mmc/BUILD.gn +++ b/system/stack/mmc/BUILD.gn @@ -82,7 +82,10 @@ install_config("install_minijail_config") { } source_set("libmmc") { - configs += [ ":target_defaults" ] + configs += [ + ":target_defaults", + "//bt/system/log:log_defaults", + ] sources = [ "daemon/service.cc", ] @@ -97,8 +100,17 @@ source_set("libmmc") { } executable("mmc_service") { - configs += [ ":target_defaults" ] - deps = [ ":libmmc" ] + configs += [ + ":target_defaults", + "//bt/system/log:log_defaults", + ] + deps = [ + ":libmmc", + "//bt/system/log:libbluetooth_log", + ] + libs = [ + "fmt", + ] sources = [ "main.cc" ] } @@ -117,6 +129,7 @@ if (use.test) { ] configs += [ ":target_defaults", + "//bt/system/log:log_defaults", "//bt/system:external_gtest_main", ] deps = [ @@ -141,6 +154,7 @@ if (use.test) { ] configs += [ ":target_defaults", + "//bt/system/log:log_defaults", "//bt/system:external_gtest_main", ] deps = [ |