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

Commit 56e56dff authored by Jack He's avatar Jack He
Browse files

GD-HCI: Add verbose logging for Tx and Rx packets

* Add StringFormat method
* Add verbose logging in GD Android HIDL client for every outgoing
  HCI transmission and incoming HCI transmission

Bug: 164973960
Test: make, atest bluetooth_test_gd, listen to music
Tag: #gd-refactor
Change-Id: I1b12c0180b835d9a0f3f641d80b62131e5b72fd4
parent 2c152b50
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ filegroup {
    srcs: [
        "init_flags.cc",
        "strings.cc",
        "stop_watch.cc",
    ],
}

+51 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 The 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.
 */

#include "common/stop_watch.h"

#include <iomanip>
#include <sstream>
#include <utility>

#include "os/log.h"

namespace bluetooth {
namespace common {

StopWatch::StopWatch(std::string text)
    : text_(std::move(text)), start_time_(std::chrono::high_resolution_clock::now()) {
  std::stringstream ss;
  auto now = std::chrono::system_clock::now();
  auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
  auto now_time_t = std::chrono::system_clock::to_time_t(now);
  ss << std::put_time(std::localtime(&now_time_t), "%Y-%m-%d %H:%M:%S");
  ss << '.' << std::setfill('0') << std::setw(3) << millis.count();
  start_timestamp_ = ss.str();
  LOG_INFO(" %s: %s:", start_timestamp_.c_str(), text_.c_str());
}

StopWatch::~StopWatch() {
  LOG_INFO(
      "%s: %s: took %zu us",
      start_timestamp_.c_str(),
      text_.c_str(),
      static_cast<size_t>(
          std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start_time_)
              .count()));
}

}  // namespace common
}  // namespace bluetooth
 No newline at end of file
+37 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 The 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.
 */

#pragma once

#include <chrono>
#include <string>

namespace bluetooth {
namespace common {

class StopWatch {
 public:
  StopWatch(std::string text);
  ~StopWatch();

 private:
  std::string text_;
  std::chrono::time_point<std::chrono::high_resolution_clock> start_time_;
  std::string start_timestamp_;
};

}  // namespace common
}  // namespace bluetooth
 No newline at end of file
+20 −0
Original line number Diff line number Diff line
@@ -70,5 +70,25 @@ std::string ToString(uint64_t value);
std::optional<bool> BoolFromString(const std::string& str);
std::string ToString(bool value);

// printf like formatting to std::string
// format must contains format information, to print a string use StringFormat("%s", str)
template <typename... Args>
std::string StringFormat(const std::string& format, Args... args) {
  auto size = std::snprintf(nullptr, 0, format.c_str(), args...);
  ASSERT_LOG(size >= 0, "return value %d, error %d, text '%s'", size, errno, strerror(errno));
  // Add 1 for terminating null byte
  char buffer[size + 1];
  auto actual_size = std::snprintf(buffer, sizeof(buffer), format.c_str(), args...);
  ASSERT_LOG(
      size == actual_size,
      "asked size %d, actual size %d, error %d, text '%s'",
      size,
      actual_size,
      errno,
      strerror(errno));
  // Exclude the terminating null byte
  return std::string(buffer, size);
}

}  // namespace common
}  // namespace bluetooth
 No newline at end of file
+8 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ namespace testing {
using bluetooth::common::BoolFromString;
using bluetooth::common::FromHexString;
using bluetooth::common::Int64FromString;
using bluetooth::common::StringFormat;
using bluetooth::common::StringJoin;
using bluetooth::common::StringSplit;
using bluetooth::common::StringTrim;
@@ -166,4 +167,11 @@ TEST(StringsTest, bool_from_and_to_string_test) {
  ASSERT_THAT(ToString(false), StrEq("false"));
}

TEST(StringsTest, string_format_test) {
  ASSERT_THAT(StringFormat("%s", "hello"), StrEq("hello"));
  ASSERT_THAT(StringFormat("%d", 42), StrEq("42"));
  ASSERT_THAT(StringFormat("%s world", "hello"), StrEq("hello world"));
  ASSERT_THAT(StringFormat("%d %.1f 0x%02x", 42, 43.123, 0x8), StrEq("42 43.1 0x08"));
}

}  // namespace testing
 No newline at end of file
Loading