summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
author Treehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com> 2025-01-17 14:15:02 -0800
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2025-01-17 14:15:02 -0800
commit52388826518826849097eb3f398fad036b108885 (patch)
tree948b8ed346ea2433e43a7e1c8bc3641c2fd3bee1 /tools
parent69dcde5b9e890e1110bf8cab31a18ca05a9d469e (diff)
parentc1cd146ba71f78ab558e254e66e553c631a0f75d (diff)
Merge "Revert "RootCanal: Migrate from {fmt} to std::format"" into main
Diffstat (limited to 'tools')
-rw-r--r--tools/rootcanal/Android.bp2
-rw-r--r--tools/rootcanal/include/hci/address.h35
-rw-r--r--tools/rootcanal/include/hci/address_with_type.h31
-rw-r--r--tools/rootcanal/include/log.h19
-rw-r--r--tools/rootcanal/lib/log.cc77
-rw-r--r--tools/rootcanal/model/controller/acl_connection_handler.cc10
-rw-r--r--tools/rootcanal/model/controller/dual_mode_controller.cc4
-rw-r--r--tools/rootcanal/model/hci/h4_data_channel_packetizer.cc2
-rw-r--r--tools/rootcanal/model/hci/h4_parser.cc2
-rw-r--r--tools/rootcanal/model/hci/hci_socket_transport.cc4
-rw-r--r--tools/rootcanal/model/setup/test_channel_transport.cc4
11 files changed, 104 insertions, 86 deletions
diff --git a/tools/rootcanal/Android.bp b/tools/rootcanal/Android.bp
index 1545f22aaa..69fc4bc456 100644
--- a/tools/rootcanal/Android.bp
+++ b/tools/rootcanal/Android.bp
@@ -38,7 +38,7 @@ cc_defaults {
misc_undefined: ["bounds"],
},
c_std: "c99",
- cpp_std: "c++23",
+ cpp_std: "c++20",
cflags: [
"-DGOOGLE_PROTOBUF_NO_RTTI",
"-Wall",
diff --git a/tools/rootcanal/include/hci/address.h b/tools/rootcanal/include/hci/address.h
index f3dedb2cb1..fc694f3fe0 100644
--- a/tools/rootcanal/include/hci/address.h
+++ b/tools/rootcanal/include/hci/address.h
@@ -16,12 +16,12 @@
#pragma once
+#include <fmt/core.h>
#include <packet_runtime.h>
#include <array>
#include <cstdint>
#include <cstring>
-#include <format>
#include <functional>
#include <initializer_list>
#include <optional>
@@ -107,17 +107,38 @@ struct hash<bluetooth::hci::Address> {
} // namespace std
template <>
-struct std::formatter<bluetooth::hci::Address> {
- template <class parse_context>
- constexpr auto parse(parse_context& ctx) -> parse_context::iterator {
- return ctx.begin();
+struct fmt::formatter<bluetooth::hci::Address> {
+ // Presentation format: 'x' - lowercase, 'X' - uppercase.
+ char presentation = 'x';
+
+ // Parses format specifications of the form ['x' | 'X'].
+ constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator {
+ // Parse the presentation format and store it in the formatter:
+ auto it = ctx.begin();
+ auto end = ctx.end();
+ if (it != end && (*it == 'x' || *it == 'X')) {
+ presentation = *it++;
+ }
+
+ // Check if reached the end of the range:
+ if (it != end && *it != '}') {
+ report_error("invalid format");
+ }
+
+ // Return an iterator past the end of the parsed range:
+ return it;
}
// Formats the address a using the parsed format specification (presentation)
// stored in this formatter.
auto format(const bluetooth::hci::Address& a,
format_context& ctx) const -> format_context::iterator {
- return std::format_to(ctx.out(), "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}", a.address[5],
- a.address[4], a.address[3], a.address[2], a.address[1], a.address[0]);
+ return presentation == 'x'
+ ? fmt::format_to(ctx.out(), "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
+ a.address[5], a.address[4], a.address[3], a.address[2],
+ a.address[1], a.address[0])
+ : fmt::format_to(ctx.out(), "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
+ a.address[5], a.address[4], a.address[3], a.address[2],
+ a.address[1], a.address[0]);
}
};
diff --git a/tools/rootcanal/include/hci/address_with_type.h b/tools/rootcanal/include/hci/address_with_type.h
index 0e0a1de590..f9560f4466 100644
--- a/tools/rootcanal/include/hci/address_with_type.h
+++ b/tools/rootcanal/include/hci/address_with_type.h
@@ -16,10 +16,11 @@
#pragma once
+#include <fmt/core.h>
+
#include <cstddef>
#include <cstdint>
#include <cstring>
-#include <format>
#include <functional>
#include <ostream>
#include <sstream>
@@ -137,16 +138,34 @@ struct hash<bluetooth::hci::AddressWithType> {
} // namespace std
template <>
-struct std::formatter<bluetooth::hci::AddressWithType> {
- template <class parse_context>
- constexpr auto parse(parse_context& ctx) -> parse_context::iterator {
- return ctx.begin();
+struct fmt::formatter<bluetooth::hci::AddressWithType> {
+ // Presentation format: 'x' - lowercase, 'X' - uppercase.
+ char presentation = 'x';
+
+ // Parses format specifications of the form ['x' | 'X'].
+ constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator {
+ // Parse the presentation format and store it in the formatter:
+ auto it = ctx.begin();
+ auto end = ctx.end();
+ if (it != end && (*it == 'x' || *it == 'X')) {
+ presentation = *it++;
+ }
+
+ // Check if reached the end of the range:
+ if (it != end && *it != '}') {
+ report_error("invalid format");
+ }
+
+ // Return an iterator past the end of the parsed range:
+ return it;
}
// Formats the address a using the parsed format specification (presentation)
// stored in this formatter.
auto format(const bluetooth::hci::AddressWithType& a,
format_context& ctx) const -> format_context::iterator {
- return std::format_to(ctx.out(), "{}[{}]", a.GetAddress(), AddressTypeText(a.GetAddressType()));
+ auto out = presentation == 'x' ? fmt::format_to(ctx.out(), "{:x}", a.GetAddress())
+ : fmt::format_to(ctx.out(), "{:X}", a.GetAddress());
+ return fmt::format_to(out, "[{}]", AddressTypeText(a.GetAddressType()));
}
};
diff --git a/tools/rootcanal/include/log.h b/tools/rootcanal/include/log.h
index 0e5a75f099..9367f5bc7a 100644
--- a/tools/rootcanal/include/log.h
+++ b/tools/rootcanal/include/log.h
@@ -16,7 +16,10 @@
#pragma once
-#include <format>
+#include <fmt/core.h>
+#include <fmt/format.h>
+#include <fmt/printf.h>
+
#include <optional>
namespace rootcanal::log {
@@ -32,18 +35,18 @@ enum Verbosity {
void SetLogColorEnable(bool);
void VLog(Verbosity verb, char const* file, int line, std::optional<int> instance,
- char const* format, std::format_args args);
+ char const* format, fmt::format_args args);
template <typename... Args>
static void Log(Verbosity verb, char const* file, int line, int instance, char const* format,
const Args&... args) {
- VLog(verb, file, line, instance, format, std::make_format_args(args...));
+ VLog(verb, file, line, instance, format, fmt::make_format_args(args...));
}
template <typename... Args>
static void Log(Verbosity verb, char const* file, int line, char const* format,
const Args&... args) {
- VLog(verb, file, line, {}, format, std::make_format_args(args...));
+ VLog(verb, file, line, {}, format, fmt::make_format_args(args...));
}
#define DEBUG(...) \
@@ -67,10 +70,10 @@ static void Log(Verbosity verb, char const* file, int line, char const* format,
"Check failed: {}", #x), \
false)
-#define ASSERT_LOG(x, ...) \
- __builtin_expect((x) != 0, true) || \
- (rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, __LINE__, \
- "Check failed: {}, {}", #x, std::format(__VA_ARGS__)), \
+#define ASSERT_LOG(x, ...) \
+ __builtin_expect((x) != 0, true) || \
+ (rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, __LINE__, \
+ "Check failed: {}, {}", #x, fmt::sprintf(__VA_ARGS__)), \
false)
} // namespace rootcanal::log
diff --git a/tools/rootcanal/lib/log.cc b/tools/rootcanal/lib/log.cc
index 0335679a3a..dc843f3fbb 100644
--- a/tools/rootcanal/lib/log.cc
+++ b/tools/rootcanal/lib/log.cc
@@ -16,15 +16,15 @@
#include "log.h"
+#include <fmt/color.h>
+#include <fmt/core.h>
+
#include <array>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
-#include <format>
-#include <iostream>
-#include <iterator>
#include <optional>
namespace rootcanal::log {
@@ -32,48 +32,29 @@ namespace rootcanal::log {
// Enable flag for log styling.
static bool enable_log_color = true;
-enum class color : uint32_t {
- aquamarine = 0x7FFFD4, // rgb(127,255,212)
- black = 0x000000, // rgb(0,0,0)
- blue_violet = 0x8A2BE2, // rgb(138,43,226)
- cadet_blue = 0x5F9EA0, // rgb(95,158,160)
- chartreuse = 0x7FFF00, // rgb(127,255,0)
- coral = 0xFF7F50, // rgb(255,127,80)
- dark_orange = 0xFF8C00, // rgb(255,140,0)
- deep_pink = 0xFF1493, // rgb(255,20,147)
- dim_gray = 0x696969, // rgb(105,105,105)
- floral_white = 0xFFFAF0, // rgb(255,250,240)
- golden_rod = 0xDAA520, // rgb(218,165,32)
- green_yellow = 0xADFF2F, // rgb(173,255,47)
- indian_red = 0xCD5C5C, // rgb(205,92,92)
- lemon_chiffon = 0xFFFACD, // rgb(255,250,205)
- medium_orchid = 0xBA55D3, // rgb(186,85,211)
- medium_sea_green = 0x3CB371, // rgb(60,179,113)
- medium_slate_blue = 0x7B68EE, // rgb(123,104,238)
- orange_red = 0xFF4500, // rgb(255,69,0)
- red = 0xFF0000, // rgb(255,0,0)
- turquoise = 0x40E0D0, // rgb(64,224,208)
- wheat = 0xF5DEB3, // rgb(245,222,179)
- yellow = 0xFFFF00, // rgb(255,255,0)
-};
-
void SetLogColorEnable(bool enable) { enable_log_color = enable; }
static std::array<char, 5> verbosity_tag = {'D', 'I', 'W', 'E', 'F'};
-static std::array<char const*, 5> text_style = {
- "\033[38;5;254m", "\033[38;5;15m", "\033[38;5;226m", "\033[38;5;160m", "\033[38;5;9m",
+static std::array<fmt::text_style, 5> text_style = {
+ fmt::fg(fmt::color::dim_gray),
+ fmt::fg(fmt::color::floral_white),
+ fmt::emphasis::bold | fmt::fg(fmt::color::yellow),
+ fmt::emphasis::bold | fmt::fg(fmt::color::orange_red),
+ fmt::emphasis::bold | fmt::fg(fmt::color::red),
};
-static std::array<color, 16> text_color = {
- color::cadet_blue, color::aquamarine, color::indian_red, color::blue_violet,
- color::chartreuse, color::medium_sea_green, color::deep_pink, color::medium_orchid,
- color::green_yellow, color::dark_orange, color::golden_rod, color::medium_slate_blue,
- color::coral, color::lemon_chiffon, color::wheat, color::turquoise,
+static std::array<fmt::color, 16> text_color = {
+ fmt::color::cadet_blue, fmt::color::aquamarine, fmt::color::indian_red,
+ fmt::color::blue_violet, fmt::color::chartreuse, fmt::color::medium_sea_green,
+ fmt::color::deep_pink, fmt::color::medium_orchid, fmt::color::green_yellow,
+ fmt::color::dark_orange, fmt::color::golden_rod, fmt::color::medium_slate_blue,
+ fmt::color::coral, fmt::color::lemon_chiffon, fmt::color::wheat,
+ fmt::color::turquoise,
};
void VLog(Verbosity verb, char const* file, int line, std::optional<int> instance,
- char const* format, std::format_args args) {
+ char const* format, fmt::format_args args) {
// Generate the time label.
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
@@ -90,30 +71,26 @@ void VLog(Verbosity verb, char const* file, int line, std::optional<int> instanc
char file_str[40]; // file:line limited to 40 characters
snprintf(file_str, sizeof(file_str), "%.35s:%d", file_name, line);
- std::ostream_iterator<char> out(std::cout);
- std::format_to(out, "root-canal {} {} {:<40} ", verbosity_tag[verb], time_str,
- reinterpret_cast<char*>(file_str));
+ fmt::print("root-canal {} {} {:<35.35} ", verbosity_tag[verb], time_str, file_str);
if (instance.has_value() && enable_log_color) {
- color instance_color = text_color[*instance % text_color.size()];
- std::format_to(out, "\033[38;5;0;48;2;{};{};{}m {:>2} \033[0m ",
- ((unsigned)instance_color >> 16) & 0xFF, ((unsigned)instance_color >> 8) & 0xFF,
- ((unsigned)instance_color >> 0) & 0xFF, *instance);
+ fmt::color instance_color = text_color[*instance % text_color.size()];
+ fmt::print(fmt::bg(instance_color) | fmt::fg(fmt::color::black), " {:>2} ", *instance);
+ fmt::print(" ");
} else if (instance.has_value()) {
- std::format_to(out, " {:>2} ", *instance);
+ fmt::print(" {:>2} ", *instance);
} else {
- std::format_to(out, " ");
+ fmt::print(" ");
}
if (enable_log_color) {
- std::format_to(out, "{}", text_style[verb]);
- std::vformat_to(out, format, args);
- std::format_to(out, "\033[0m");
+ fmt::text_style style = text_style[verb];
+ fmt::vprint(stdout, style, format, args);
} else {
- std::vformat_to(out, format, args);
+ fmt::vprint(stdout, format, args);
}
- std::format_to(out, "\n");
+ fmt::print("\n");
if (verb == Verbosity::kFatal) {
std::abort();
diff --git a/tools/rootcanal/model/controller/acl_connection_handler.cc b/tools/rootcanal/model/controller/acl_connection_handler.cc
index 9b44b06568..bdcd1f72f4 100644
--- a/tools/rootcanal/model/controller/acl_connection_handler.cc
+++ b/tools/rootcanal/model/controller/acl_connection_handler.cc
@@ -210,12 +210,12 @@ std::optional<uint16_t> AclConnectionHandler::GetAclConnectionHandle(
}
AclConnection& AclConnectionHandler::GetAclConnection(uint16_t handle) {
- ASSERT_LOG(HasHandle(handle), "Unknown handle {}", handle);
+ ASSERT_LOG(HasHandle(handle), "Unknown handle %d", handle);
return acl_connections_.at(handle);
}
AddressWithType AclConnectionHandler::GetAddress(uint16_t handle) const {
- ASSERT_LOG(HasHandle(handle), "Unknown handle {}", handle);
+ ASSERT_LOG(HasHandle(handle), "Unknown handle %hd", handle);
return acl_connections_.at(handle).GetAddress();
}
@@ -225,17 +225,17 @@ std::optional<AddressWithType> AclConnectionHandler::GetAddressSafe(uint16_t han
}
Address AclConnectionHandler::GetScoAddress(uint16_t handle) const {
- ASSERT_LOG(HasScoHandle(handle), "Unknown SCO handle {}", handle);
+ ASSERT_LOG(HasScoHandle(handle), "Unknown SCO handle %hd", handle);
return sco_connections_.at(handle).GetAddress();
}
AddressWithType AclConnectionHandler::GetOwnAddress(uint16_t handle) const {
- ASSERT_LOG(HasHandle(handle), "Unknown handle {}", handle);
+ ASSERT_LOG(HasHandle(handle), "Unknown handle %hd", handle);
return acl_connections_.at(handle).GetOwnAddress();
}
AddressWithType AclConnectionHandler::GetResolvedAddress(uint16_t handle) const {
- ASSERT_LOG(HasHandle(handle), "Unknown handle {}", handle);
+ ASSERT_LOG(HasHandle(handle), "Unknown handle %hd", handle);
return acl_connections_.at(handle).GetResolvedAddress();
}
diff --git a/tools/rootcanal/model/controller/dual_mode_controller.cc b/tools/rootcanal/model/controller/dual_mode_controller.cc
index e50eeffe8d..15f788b438 100644
--- a/tools/rootcanal/model/controller/dual_mode_controller.cc
+++ b/tools/rootcanal/model/controller/dual_mode_controller.cc
@@ -59,7 +59,7 @@ constexpr bool kLeApcfAdTypeFilterSupported = true;
#define CHECK_PACKET_VIEW(view) \
do { \
if (!CheckPacketView( \
- view, std::format("{}:{} - {}() invalid packet", __FILE__, __LINE__, __func__))) { \
+ view, fmt::format("{}:{} - {}() invalid packet", __FILE__, __LINE__, __func__))) { \
return; \
} \
} while (0)
@@ -2874,7 +2874,7 @@ void DualModeController::LeApcf(CommandView command) {
command_view.GetApcfOpcode(), std::vector<uint8_t>{}));
invalid_packet_handler_(id_, InvalidPacketReason::kUnsupported,
- std::format("unsupported APCF opcode {:#x}",
+ fmt::format("unsupported APCF opcode {:#x}",
static_cast<uint8_t>(command_view.GetApcfOpcode())),
command_view.bytes().bytes());
}
diff --git a/tools/rootcanal/model/hci/h4_data_channel_packetizer.cc b/tools/rootcanal/model/hci/h4_data_channel_packetizer.cc
index 3815377d85..aa8edbe0ad 100644
--- a/tools/rootcanal/model/hci/h4_data_channel_packetizer.cc
+++ b/tools/rootcanal/model/hci/h4_data_channel_packetizer.cc
@@ -85,7 +85,7 @@ void H4DataChannelPacketizer::OnDataReady(std::shared_ptr<AsyncDataChannel> sock
disconnect_cb_();
return;
}
- FATAL("Read error in {}: {}", std::to_underlying(h4_parser_.CurrentState()), strerror(errno));
+ FATAL("Read error in {}: {}", fmt::underlying(h4_parser_.CurrentState()), strerror(errno));
}
h4_parser_.Consume(buffer.data(), bytes_read);
}
diff --git a/tools/rootcanal/model/hci/h4_parser.cc b/tools/rootcanal/model/hci/h4_parser.cc
index c7ff04f9d9..c3aa76ddd2 100644
--- a/tools/rootcanal/model/hci/h4_parser.cc
+++ b/tools/rootcanal/model/hci/h4_parser.cc
@@ -83,7 +83,7 @@ void H4Parser::OnPacketReady() {
iso_cb_(packet_);
break;
default:
- FATAL("Unimplemented packet type {}", std::to_underlying(hci_packet_type_));
+ FATAL("Unimplemented packet type {}", fmt::underlying(hci_packet_type_));
}
// Get ready for the next type byte.
hci_packet_type_ = PacketType::UNKNOWN;
diff --git a/tools/rootcanal/model/hci/hci_socket_transport.cc b/tools/rootcanal/model/hci/hci_socket_transport.cc
index 4e88930cf7..d7127f561b 100644
--- a/tools/rootcanal/model/hci/hci_socket_transport.cc
+++ b/tools/rootcanal/model/hci/hci_socket_transport.cc
@@ -16,8 +16,6 @@
#include "hci_socket_transport.h"
-#include <utility>
-
#include "log.h"
namespace rootcanal {
@@ -58,7 +56,7 @@ void HciSocketTransport::Tick() { h4_.OnDataReady(socket_); }
void HciSocketTransport::Send(PacketType packet_type, const std::vector<uint8_t>& packet) {
if (!socket_ || !socket_->Connected()) {
- INFO("Closed socket. Dropping packet of type {}", std::to_underlying(packet_type));
+ INFO("Closed socket. Dropping packet of type {}", fmt::underlying(packet_type));
return;
}
uint8_t type = static_cast<uint8_t>(packet_type);
diff --git a/tools/rootcanal/model/setup/test_channel_transport.cc b/tools/rootcanal/model/setup/test_channel_transport.cc
index 9f0dd3b9db..d59cbb507f 100644
--- a/tools/rootcanal/model/setup/test_channel_transport.cc
+++ b/tools/rootcanal/model/setup/test_channel_transport.cc
@@ -106,9 +106,9 @@ void TestChannelTransport::SendResponse(std::shared_ptr<AsyncDataChannel> socket
WARNING("Unable to send a response. EBADF");
return;
}
- ASSERT_LOG(written == 4, "What happened? written = {} errno = {}", written, errno);
+ ASSERT_LOG(written == 4, "What happened? written = %zd errno = %d", written, errno);
written = socket->Send(reinterpret_cast<const uint8_t*>(response.c_str()), size);
- ASSERT_LOG(written == static_cast<int>(size), "What happened? written = {} errno = {}", written,
+ ASSERT_LOG(written == static_cast<int>(size), "What happened? written = %zd errno = %d", written,
errno);
}