Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 828c7ea9 authored by Henri Chataing's avatar Henri Chataing
Browse files

system/gd/common: Migrate to libbluetooth_log

Test: m com.android.btservices
Bug: 305066880
Flag: EXEMPT, mechanical refactor
Change-Id: I24f43c7337a36c21ec663d27894136d909e9e45f
parent 8d9d39e2
Loading
Loading
Loading
Loading
+15 −14
Original line number Diff line number Diff line
@@ -17,6 +17,10 @@
 ******************************************************************************/
#define LOG_TAG "BluetoothMetricIdManager"

#include "common/metric_id_manager.h"

#include <bluetooth/log.h>

#include <functional>
#include <iterator>
#include <mutex>
@@ -24,7 +28,6 @@
#include <thread>

#include "os/log.h"
#include "common/metric_id_manager.h"

namespace bluetooth {
namespace common {
@@ -58,10 +61,10 @@ bool MetricIdManager::Init(

  // init paired_devices_map
  if (paired_device_map.size() > kMaxNumPairedDevicesInMemory) {
    LOG_ALWAYS_FATAL(
        "Paired device map has size %zu, which is bigger than "
        "kMaxNumPairedDevicesInMemory %zu",
        paired_device_map.size(), kMaxNumPairedDevicesInMemory);
    log::fatal(
        "Paired device map has size {}, which is bigger than kMaxNumPairedDevicesInMemory {}",
        paired_device_map.size(),
        kMaxNumPairedDevicesInMemory);
    // fail loudly to let caller know
    return false;
  }
@@ -69,9 +72,8 @@ bool MetricIdManager::Init(
  next_id_ = kMinId;
  for (const auto& p : paired_device_map) {
    if (p.second < kMinId || p.second > kMaxId) {
      LOG_ALWAYS_FATAL(
          "Invalid Bluetooth Metric Id in config. "
          "Id %d of %s is out of range [%d, %d]",
      log::fatal(
          "Invalid Bluetooth Metric Id in config. Id {} of {} is out of range [{}, {}]",
          p.second,
          ADDRESS_TO_LOGGABLE_CSTR(p.first),
          kMinId,
@@ -138,7 +140,7 @@ int MetricIdManager::AllocateId(const Address& mac_address) {
    next_id_++;
    if (next_id_ > kMaxId) {
      next_id_ = kMinId;
      LOG_WARN("Bluetooth metric id overflow.");
      log::warn("Bluetooth metric id overflow.");
    }
  }
  int id = next_id_++;
@@ -161,13 +163,12 @@ bool MetricIdManager::SaveDevice(const Address& mac_address) {
    return true;
  }
  if (!temporary_device_cache_.contains(mac_address)) {
    LOG_ERROR("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_");
    return false;
  }
  auto opt = temporary_device_cache_.extract(mac_address);
  if (!opt) {
    LOG_ERROR("Failed to remove device from temporary_device_cache_");
    log::error("Failed to remove device from temporary_device_cache_");
    return false;
  }
  int id = opt->second;
@@ -176,7 +177,7 @@ bool MetricIdManager::SaveDevice(const Address& mac_address) {
    ForgetDevicePostprocess(evicted->first, evicted->second);
  }
  if (!save_id_callback_(mac_address, id)) {
    LOG_ERROR("Callback returned false after saving the device");
    log::error("Callback returned false after saving the device");
    return false;
  }
  return true;
@@ -187,7 +188,7 @@ void MetricIdManager::ForgetDevice(const Address& mac_address) {
  std::lock_guard<std::mutex> lock(id_allocator_mutex_);
  auto opt = paired_device_cache_.extract(mac_address);
  if (!opt) {
    LOG_ERROR("Failed to remove device from paired_device_cache_");
    log::error("Failed to remove device from paired_device_cache_");
    return;
  }
  ForgetDevicePostprocess(mac_address, opt->second);
+16 −13
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@

#include "common/stop_watch.h"

#include <bluetooth/log.h>

#include <iomanip>
#include <mutex>
#include <sstream>
@@ -37,9 +39,10 @@ static std::recursive_mutex stopwatch_log_mutex;
void StopWatch::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>(
    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()));
@@ -55,8 +58,8 @@ void StopWatch::RecordLog(StopWatchLog log) {

void StopWatch::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;
@@ -74,17 +77,17 @@ void StopWatch::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(),
    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("=-----------------------------------=");
}

StopWatch::StopWatch(std::string text)
+13 −11
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#include "common/strings.h"

#include <bluetooth/log.h>

#include <algorithm>
#include <charconv>
#include <cstdlib>
@@ -54,11 +56,11 @@ bool IsValidHexString(const std::string& str) {

std::optional<std::vector<uint8_t>> FromHexString(const std::string& str) {
  if (str.size() % 2 != 0) {
    LOG_INFO("str size is not divisible by 2, size is %zu", str.size());
    log::info("str size is not divisible by 2, size is {}", str.size());
    return std::nullopt;
  }
  if (std::find_if_not(str.begin(), str.end(), IsHexDigit{}) != str.end()) {
    LOG_INFO("value contains none hex digit");
    log::info("value contains none hex digit");
    return std::nullopt;
  }
  std::vector<uint8_t> value;
@@ -67,7 +69,7 @@ std::optional<std::vector<uint8_t>> FromHexString(const std::string& str) {
    uint8_t v = 0;
    auto ret = std::from_chars(str.c_str() + i, str.c_str() + i + 2, v, 16);
    if (std::make_error_code(ret.ec)) {
      LOG_INFO("failed to parse hex char at index %zu", i);
      log::info("failed to parse hex char at index {}", i);
      return std::nullopt;
    }
    value.push_back(v);
@@ -115,15 +117,15 @@ std::optional<int64_t> Int64FromString(const std::string& str) {
  errno = 0;
  int64_t value = std::strtoll(str.c_str(), &ptr, 10);
  if (errno != 0) {
    LOG_INFO("cannot parse string '%s' with error '%s'", str.c_str(), strerror(errno));
    log::info("cannot parse string '{}' with error '{}'", str, strerror(errno));
    return std::nullopt;
  }
  if (ptr == str.c_str()) {
    LOG_INFO("string '%s' is empty or has wrong format", str.c_str());
    log::info("string '{}' is empty or has wrong format", str);
    return std::nullopt;
  }
  if (ptr != (str.c_str() + str.size())) {
    LOG_INFO("cannot parse whole string '%s'", str.c_str());
    log::info("cannot parse whole string '{}'", str);
    return std::nullopt;
  }
  return value;
@@ -135,22 +137,22 @@ std::string ToString(int64_t value) {

std::optional<uint64_t> Uint64FromString(const std::string& str) {
  if (str.find('-') != std::string::npos) {
    LOG_INFO("string '%s' contains minus sign, this function is for unsigned", str.c_str());
    log::info("string '{}' contains minus sign, this function is for unsigned", str);
    return std::nullopt;
  }
  char* ptr = nullptr;
  errno = 0;
  uint64_t value = std::strtoull(str.c_str(), &ptr, 10);
  if (errno != 0) {
    LOG_INFO("cannot parse string '%s' with error '%s'", str.c_str(), strerror(errno));
    log::info("cannot parse string '{}' with error '{}'", str, strerror(errno));
    return std::nullopt;
  }
  if (ptr == str.c_str()) {
    LOG_INFO("string '%s' is empty or has wrong format", str.c_str());
    log::info("string '{}' is empty or has wrong format", str);
    return std::nullopt;
  }
  if (ptr != (str.c_str() + str.size())) {
    LOG_INFO("cannot parse whole string '%s'", str.c_str());
    log::info("cannot parse whole string '{}'", str);
    return std::nullopt;
  }
  return value;
@@ -166,7 +168,7 @@ std::optional<bool> BoolFromString(const std::string& str) {
  } else if (str == "false") {
    return false;
  } else {
    LOG_INFO("string '%s' is neither true nor false", str.c_str());
    log::info("string '{}' is neither true nor false", str);
    return std::nullopt;
  }
}
+2 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include "common/strings.h"

#include <bluetooth/log.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

@@ -74,7 +75,7 @@ TEST(StringsTest, to_hex_string_from_number) {
    ASSERT_EQ(ToHexString(LONG_MIN), "LONG_MIN");
    ASSERT_EQ(ToHexString(LONG_MIN + 1L), "-0x7fffffffffffffff");
  } else {
    LOG_ERROR("Unknown architecture");
    bluetooth::log::error("Unknown architecture");
    ASSERT_TRUE(false);
  }
  ASSERT_EQ(ToHexString('a'), "0x61");
+3 −2
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include "common/testing/log_capture.h"

#include <bluetooth/log.h>
#include <fcntl.h>
#include <sys/stat.h>

@@ -28,8 +29,8 @@ namespace bluetooth {
namespace testing {

LogCapture::LogCapture() {
  LOG_INFO(
      "Log capture disabled for android build dup_fd:%d fd:%d original_stderr_fd:%d",
  log::info(
      "Log capture disabled for android build dup_fd:{} fd:{} original_stderr_fd:{}",
      dup_fd_,
      fd_,
      original_stderr_fd_);
Loading