diff options
author | 2024-11-22 23:00:23 +0000 | |
---|---|---|
committer | 2025-01-16 09:52:20 +0000 | |
commit | 24658260758abbea64dab58331bf3cfbaaf9c989 (patch) | |
tree | e697bd82e0531c774c6cf9d1684bd30de15d6918 /tools | |
parent | 4702097b062e8cd87074800a54363916a37c1ffd (diff) |
RootCanal: Migrate from {fmt} to std::format
Bug: 378773651
Test: m root-canal
Change-Id: I9bd3ed6e875c43ada35d6c77915f98fe4a885e6d
Diffstat (limited to 'tools')
-rw-r--r-- | tools/rootcanal/Android.bp | 2 | ||||
-rw-r--r-- | tools/rootcanal/include/hci/address.h | 35 | ||||
-rw-r--r-- | tools/rootcanal/include/hci/address_with_type.h | 31 | ||||
-rw-r--r-- | tools/rootcanal/include/log.h | 19 | ||||
-rw-r--r-- | tools/rootcanal/lib/log.cc | 77 | ||||
-rw-r--r-- | tools/rootcanal/model/controller/acl_connection_handler.cc | 10 | ||||
-rw-r--r-- | tools/rootcanal/model/controller/dual_mode_controller.cc | 4 | ||||
-rw-r--r-- | tools/rootcanal/model/hci/h4_data_channel_packetizer.cc | 2 | ||||
-rw-r--r-- | tools/rootcanal/model/hci/h4_parser.cc | 2 | ||||
-rw-r--r-- | tools/rootcanal/model/hci/hci_socket_transport.cc | 4 | ||||
-rw-r--r-- | tools/rootcanal/model/setup/test_channel_transport.cc | 4 |
11 files changed, 86 insertions, 104 deletions
diff --git a/tools/rootcanal/Android.bp b/tools/rootcanal/Android.bp index 69fc4bc456..1545f22aaa 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++20", + cpp_std: "c++23", cflags: [ "-DGOOGLE_PROTOBUF_NO_RTTI", "-Wall", diff --git a/tools/rootcanal/include/hci/address.h b/tools/rootcanal/include/hci/address.h index fc694f3fe0..f3dedb2cb1 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,38 +107,17 @@ struct hash<bluetooth::hci::Address> { } // namespace std template <> -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; +struct std::formatter<bluetooth::hci::Address> { + template <class parse_context> + constexpr auto parse(parse_context& ctx) -> parse_context::iterator { + return ctx.begin(); } // 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 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]); + 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]); } }; diff --git a/tools/rootcanal/include/hci/address_with_type.h b/tools/rootcanal/include/hci/address_with_type.h index f9560f4466..0e0a1de590 100644 --- a/tools/rootcanal/include/hci/address_with_type.h +++ b/tools/rootcanal/include/hci/address_with_type.h @@ -16,11 +16,10 @@ #pragma once -#include <fmt/core.h> - #include <cstddef> #include <cstdint> #include <cstring> +#include <format> #include <functional> #include <ostream> #include <sstream> @@ -138,34 +137,16 @@ struct hash<bluetooth::hci::AddressWithType> { } // namespace std template <> -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; +struct std::formatter<bluetooth::hci::AddressWithType> { + template <class parse_context> + constexpr auto parse(parse_context& ctx) -> parse_context::iterator { + return ctx.begin(); } // 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 { - 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())); + return std::format_to(ctx.out(), "{}[{}]", a.GetAddress(), AddressTypeText(a.GetAddressType())); } }; diff --git a/tools/rootcanal/include/log.h b/tools/rootcanal/include/log.h index 9367f5bc7a..0e5a75f099 100644 --- a/tools/rootcanal/include/log.h +++ b/tools/rootcanal/include/log.h @@ -16,10 +16,7 @@ #pragma once -#include <fmt/core.h> -#include <fmt/format.h> -#include <fmt/printf.h> - +#include <format> #include <optional> namespace rootcanal::log { @@ -35,18 +32,18 @@ enum Verbosity { void SetLogColorEnable(bool); void VLog(Verbosity verb, char const* file, int line, std::optional<int> instance, - char const* format, fmt::format_args args); + char const* format, std::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, fmt::make_format_args(args...)); + VLog(verb, file, line, instance, format, std::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, fmt::make_format_args(args...)); + VLog(verb, file, line, {}, format, std::make_format_args(args...)); } #define DEBUG(...) \ @@ -70,10 +67,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, fmt::sprintf(__VA_ARGS__)), \ +#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__)), \ false) } // namespace rootcanal::log diff --git a/tools/rootcanal/lib/log.cc b/tools/rootcanal/lib/log.cc index dc843f3fbb..0335679a3a 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,29 +32,48 @@ 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<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<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::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, +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, }; void VLog(Verbosity verb, char const* file, int line, std::optional<int> instance, - char const* format, fmt::format_args args) { + char const* format, std::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); @@ -71,26 +90,30 @@ 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); - fmt::print("root-canal {} {} {:<35.35} ", verbosity_tag[verb], time_str, file_str); + std::ostream_iterator<char> out(std::cout); + std::format_to(out, "root-canal {} {} {:<40} ", verbosity_tag[verb], time_str, + reinterpret_cast<char*>(file_str)); if (instance.has_value() && enable_log_color) { - 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(" "); + 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); } else if (instance.has_value()) { - fmt::print(" {:>2} ", *instance); + std::format_to(out, " {:>2} ", *instance); } else { - fmt::print(" "); + std::format_to(out, " "); } if (enable_log_color) { - fmt::text_style style = text_style[verb]; - fmt::vprint(stdout, style, format, args); + std::format_to(out, "{}", text_style[verb]); + std::vformat_to(out, format, args); + std::format_to(out, "\033[0m"); } else { - fmt::vprint(stdout, format, args); + std::vformat_to(out, format, args); } - fmt::print("\n"); + std::format_to(out, "\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 bdcd1f72f4..9b44b06568 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 %d", handle); + ASSERT_LOG(HasHandle(handle), "Unknown handle {}", handle); return acl_connections_.at(handle); } AddressWithType AclConnectionHandler::GetAddress(uint16_t handle) const { - ASSERT_LOG(HasHandle(handle), "Unknown handle %hd", handle); + ASSERT_LOG(HasHandle(handle), "Unknown handle {}", 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 %hd", handle); + ASSERT_LOG(HasScoHandle(handle), "Unknown SCO handle {}", handle); return sco_connections_.at(handle).GetAddress(); } AddressWithType AclConnectionHandler::GetOwnAddress(uint16_t handle) const { - ASSERT_LOG(HasHandle(handle), "Unknown handle %hd", handle); + ASSERT_LOG(HasHandle(handle), "Unknown handle {}", handle); return acl_connections_.at(handle).GetOwnAddress(); } AddressWithType AclConnectionHandler::GetResolvedAddress(uint16_t handle) const { - ASSERT_LOG(HasHandle(handle), "Unknown handle %hd", handle); + ASSERT_LOG(HasHandle(handle), "Unknown handle {}", 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 15f788b438..e50eeffe8d 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, fmt::format("{}:{} - {}() invalid packet", __FILE__, __LINE__, __func__))) { \ + view, std::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, - fmt::format("unsupported APCF opcode {:#x}", + std::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 aa8edbe0ad..3815377d85 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 {}: {}", fmt::underlying(h4_parser_.CurrentState()), strerror(errno)); + FATAL("Read error in {}: {}", std::to_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 c3aa76ddd2..c7ff04f9d9 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 {}", fmt::underlying(hci_packet_type_)); + FATAL("Unimplemented packet type {}", std::to_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 d7127f561b..4e88930cf7 100644 --- a/tools/rootcanal/model/hci/hci_socket_transport.cc +++ b/tools/rootcanal/model/hci/hci_socket_transport.cc @@ -16,6 +16,8 @@ #include "hci_socket_transport.h" +#include <utility> + #include "log.h" namespace rootcanal { @@ -56,7 +58,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 {}", fmt::underlying(packet_type)); + INFO("Closed socket. Dropping packet of type {}", std::to_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 d59cbb507f..9f0dd3b9db 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 = %zd errno = %d", written, errno); + ASSERT_LOG(written == 4, "What happened? written = {} errno = {}", written, errno); written = socket->Send(reinterpret_cast<const uint8_t*>(response.c_str()), size); - ASSERT_LOG(written == static_cast<int>(size), "What happened? written = %zd errno = %d", written, + ASSERT_LOG(written == static_cast<int>(size), "What happened? written = {} errno = {}", written, errno); } |