Migrate from fmt/format to std/format

This commit is contained in:
CommanderRedYT
2025-04-01 20:37:37 +02:00
parent 228f632606
commit 8e9ee55512
4 changed files with 38 additions and 42 deletions

View File

@@ -24,7 +24,6 @@ set(dependencies
cpputils
espchrono
espcpputils
fmt
)
idf_component_register(

View File

@@ -19,6 +19,8 @@
#include <functional>
#include <atomic>
#include <utility>
#include <format>
#include <cstring>
// esp-idf includes
#include <esp_log.h>
@@ -44,7 +46,6 @@
#endif
// 3rdparty lib includes
#include <fmt/core.h>
#include <strutils.h>
#include <delayedconstruction.h>
#include <wrappers/event_group.h>
@@ -817,7 +818,7 @@ void update(const config &config)
std::string sta_error_t::toString() const
{
return fmt::format("{} WIFI_STA_DISCONNECTED ssid=\"{}\" bssid={} reason={}({})",
return std::format("{} WIFI_STA_DISCONNECTED ssid=\"{}\" bssid={} reason={}({})",
timestamp.time_since_epoch().count(),
ssid, wifi_stack::toString(bssid),
std::to_underlying(reason), wifi_stack::toString(reason));
@@ -880,7 +881,7 @@ std::expected<void, std::string> begin_scan(const sta_config &sta_config)
return std::unexpected("invalid scan settings (not active nor passive)!");
if (const auto result = esp_wifi_scan_start(&scan_config, false) != ESP_OK)
return std::unexpected(fmt::format("esp_wifi_scan_start() failed with: {}", esp_err_to_name(result)));
return std::unexpected(std::format("esp_wifi_scan_start() failed with: {}", esp_err_to_name(result)));
scanStarted = espchrono::millis_clock::now();
@@ -936,7 +937,7 @@ std::expected<wifi_ap_record_t, std::string> get_sta_ap_info()
else
{
ESP_LOGW(TAG, "esp_wifi_sta_get_ap_info() failed with %s", esp_err_to_name(result));
return std::unexpected(fmt::format("esp_wifi_sta_get_ap_info() failed with {}", esp_err_to_name(result)));
return std::unexpected(std::format("esp_wifi_sta_get_ap_info() failed with {}", esp_err_to_name(result)));
}
}
@@ -948,7 +949,7 @@ mac_or_error get_mac_addr(wifi_interface_t ifx)
else
{
ESP_LOGW(TAG, "esp_wifi_get_mac() failed with %s", esp_err_to_name(result));
return std::unexpected(fmt::format("esp_wifi_get_mac() failed with {}", esp_err_to_name(result)));
return std::unexpected(std::format("esp_wifi_get_mac() failed with {}", esp_err_to_name(result)));
}
}
@@ -961,7 +962,7 @@ mac_or_error get_default_mac_addr()
else
{
//ESP_LOGE(TAG, "esp_efuse_mac_get_default() failed with %s", esp_err_to_name(result));
return std::unexpected(fmt::format("esp_efuse_mac_get_default() failed with {}", esp_err_to_name(result)));
return std::unexpected(std::format("esp_efuse_mac_get_default() failed with {}", esp_err_to_name(result)));
}
}();
@@ -977,7 +978,7 @@ mac_or_error get_custom_mac_addr()
else
{
//ESP_LOGE(TAG, "esp_efuse_mac_get_custom() failed with %s", esp_err_to_name(result));
return std::unexpected(fmt::format("esp_efuse_mac_get_custom() failed with {}", esp_err_to_name(result)));
return std::unexpected(std::format("esp_efuse_mac_get_custom() failed with {}", esp_err_to_name(result)));
}
}();
@@ -992,7 +993,7 @@ mac_or_error get_base_mac_addr()
else
{
ESP_LOGE(TAG, "esp_base_mac_addr_get() failed with %s", esp_err_to_name(result));
return std::unexpected(fmt::format("esp_base_mac_addr_get() failed with {}", esp_err_to_name(result)));
return std::unexpected(std::format("esp_base_mac_addr_get() failed with {}", esp_err_to_name(result)));
}
}
@@ -1003,7 +1004,7 @@ std::expected<void, std::string> set_base_mac_addr(mac_t mac_addr)
else
{
ESP_LOGE(TAG, "esp_base_mac_addr_set() failed with %s", esp_err_to_name(result));
return std::unexpected(fmt::format("esp_base_mac_addr_set() failed with {}", esp_err_to_name(result)));
return std::unexpected(std::format("esp_base_mac_addr_set() failed with {}", esp_err_to_name(result)));
}
}
@@ -1015,7 +1016,7 @@ std::expected<esp_netif_ip_info_t, std::string> get_ip_info(esp_netif_t *esp_net
else
{
ESP_LOGE(TAG, "esp_netif_get_ip_info() failed with %s", esp_err_to_name(result));
return std::unexpected(fmt::format("tcpip_adapter_get_ip_info() failed with {}", esp_err_to_name(result)));
return std::unexpected(std::format("tcpip_adapter_get_ip_info() failed with {}", esp_err_to_name(result)));
}
}
@@ -1024,14 +1025,14 @@ std::expected<std::string_view, std::string> get_hostname_for_interface(esp_inte
if (const auto netif = esp_netifs[interf])
return get_hostname_for_interface(netif);
else
return std::unexpected(fmt::format("netif for {} is invalid", std::to_underlying(interf)));
return std::unexpected(std::format("netif for {} is invalid", std::to_underlying(interf)));
}
std::expected<std::string_view, std::string> get_hostname_for_interface(esp_netif_t *esp_netif)
{
const char *hostname{};
if (const auto result = esp_netif_get_hostname(esp_netif, &hostname))
return std::unexpected(fmt::format("esp_netif_get_hostname() failed with {}", esp_err_to_name(result)));
return std::unexpected(std::format("esp_netif_get_hostname() failed with {}", esp_err_to_name(result)));
if (!hostname)
return std::unexpected("esp_netif_get_hostname() returned a nullptr string");
@@ -2128,7 +2129,7 @@ std::expected<void, std::string> applyBaseMac(const mac_t &mac)
return {};
else
{
const auto msg = fmt::format("set_base_mac_addr() {} failed: {}", toString(mac), result.error());
const auto msg = std::format("set_base_mac_addr() {} failed: {}", toString(mac), result.error());
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return std::unexpected(msg);
}
@@ -2153,7 +2154,7 @@ std::expected<mac_t, std::string> expectedBaseMac(const config &config)
return *mac;
else
{
const auto msg = fmt::format("no base mac fuse or override set and get_default_mac_addr() failed: {}", mac.error());
const auto msg = std::format("no base mac fuse or override set and get_default_mac_addr() failed: {}", mac.error());
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return std::unexpected(msg);
}
@@ -2820,7 +2821,7 @@ std::expected<void, std::string> eth_begin(const config &config, const eth_confi
if (const auto result = esp_eth_driver_install(&eth_config, &eth_handle); result != ESP_OK)
{
auto msg = fmt::format("esp_eth_driver_install() failed with {}", esp_err_to_name(result));
auto msg = std::format("esp_eth_driver_install() failed with {}", esp_err_to_name(result));
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return std::unexpected(std::move(msg));
}
@@ -2843,7 +2844,7 @@ std::expected<void, std::string> eth_begin(const config &config, const eth_confi
if (const auto result = esp_netif_attach(esp_netifs[ESP_IF_ETH], ptr); result != ESP_OK)
{
auto msg = fmt::format("esp_netif_attach() failed with {}", esp_err_to_name(result));
auto msg = std::format("esp_netif_attach() failed with {}", esp_err_to_name(result));
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return std::unexpected(std::move(msg));
}
@@ -2854,7 +2855,7 @@ std::expected<void, std::string> eth_begin(const config &config, const eth_confi
{
if (const auto result = esp_eth_start(eth_handle); result != ESP_OK)
{
auto msg = fmt::format("esp_eth_start() failed with {}", esp_err_to_name(result));
auto msg = std::format("esp_eth_start() failed with {}", esp_err_to_name(result));
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return std::unexpected(std::move(msg));
}

View File

@@ -4,13 +4,11 @@
#include <cstdio>
#include <bitset>
#include <utility>
#include <format>
// esp-idf includes
#include <esp_log.h>
// 3rdparty lib includes
#include <fmt/core.h>
namespace wifi_stack {
namespace {
constexpr const char * const TAG = "WIFI_STACK";
@@ -72,7 +70,7 @@ std::string toString(wifi_auth_mode_t authMode)
default:;
}
ESP_LOGW(TAG, "Unknown wifi_auth_mode_t(%i)", std::to_underlying(authMode));
return fmt::format("Unknown wifi_auth_mode_t({})", std::to_underlying(authMode));
return std::format("Unknown wifi_auth_mode_t({})", std::to_underlying(authMode));
}
std::string toString(wifi_cipher_type_t cipherType)
@@ -94,7 +92,7 @@ std::string toString(wifi_cipher_type_t cipherType)
case WIFI_CIPHER_TYPE_UNKNOWN: return "UNKNOWN";
}
ESP_LOGW(TAG, "Unknown wifi_cipher_type_t(%i)", std::to_underlying(cipherType));
return fmt::format("Unknown wifi_cipher_type_t({})", std::to_underlying(cipherType));
return std::format("Unknown wifi_cipher_type_t({})", std::to_underlying(cipherType));
}
std::string toString(wifi_bandwidth_t bandwidth)
@@ -106,7 +104,7 @@ std::string toString(wifi_bandwidth_t bandwidth)
default:;
}
ESP_LOGW(TAG, "Unknown wifi_bandwidth_t(%i)", std::to_underlying(bandwidth));
return fmt::format("Unknown wifi_bandwidth_t({})", std::to_underlying(bandwidth));
return std::format("Unknown wifi_bandwidth_t({})", std::to_underlying(bandwidth));
}
std::string toString(esp_interface_t interface)
@@ -120,7 +118,7 @@ std::string toString(esp_interface_t interface)
case ESP_IF_MAX: return "MAX";
}
ESP_LOGW(TAG, "Unknown esp_interface_t(%i)", std::to_underlying(interface));
return fmt::format("Unknown esp_interface_t({})", std::to_underlying(interface));
return std::format("Unknown esp_interface_t({})", std::to_underlying(interface));
}
std::string toString(esp_netif_dhcp_status_t status)
@@ -133,7 +131,7 @@ std::string toString(esp_netif_dhcp_status_t status)
case ESP_NETIF_DHCP_STATUS_MAX: return "STATUS_MAX";
}
ESP_LOGW(TAG, "Unknown esp_netif_dhcp_status_t(%i)", std::to_underlying(status));
return fmt::format("Unknown esp_netif_dhcp_status_t({})", std::to_underlying(status));
return std::format("Unknown esp_netif_dhcp_status_t({})", std::to_underlying(status));
}
const char * toString(wifi_err_reason_t reason)
@@ -229,12 +227,12 @@ template<> std::expected<mac_t, std::string> fromString<mac_t>(std::string_view
&result[0], &result[1], &result[2], &result[3], &result[4], &result[5]) == 6)
return result;
return std::unexpected(fmt::format("invalid format ({})", str));
return std::unexpected(std::format("invalid format ({})", str));
}
std::string toString(const mac_t &val)
{
return fmt::format("{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
return std::format("{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
val.at(0), val.at(1), val.at(2), val.at(3), val.at(4), val.at(5));
}
@@ -283,7 +281,7 @@ template<> std::expected<ip_address_t, std::string> fromString<ip_address_t>(std
std::string toString(ip_address_t val)
{
return fmt::format("{}.{}.{}.{}", val[0], val[1], val[2], val[3]);
return std::format("{}.{}.{}.{}", val[0], val[1], val[2], val[3]);
}
std::string toString(const std::optional<ip_address_t> &val)
@@ -337,7 +335,7 @@ std::string toString(ip_addr_t val)
default:;
}
//ESP_LOGW(TAG, "Unknown ipv%hhu", val.type);
return fmt::format("Unknown ipv{}", val.type);
return std::format("Unknown ipv{}", val.type);
}
std::string toString(const esp_ip_addr_t &val)
@@ -349,7 +347,7 @@ std::string toString(const esp_ip_addr_t &val)
default:;
}
ESP_LOGW(TAG, "Unknown ipv%hhu", val.type);
return fmt::format("Unknown ipv{}", val.type);
return std::format("Unknown ipv{}", val.type);
}
} // namespace wifi_stack

View File

@@ -2,6 +2,7 @@
// system
#include <utility>
#include <format>
// esp-idf includes
#include <lwip/sockets.h>
@@ -9,9 +10,6 @@
#include <errno.h>
#include <esp_log.h>
// 3rdparty lib includes
#include <fmt/core.h>
// local includes
#include "espwifistack.h"
@@ -42,7 +40,7 @@ std::expected<void, std::string> UdpSender::send(esp_interface_t interf, uint16_
{
const auto interfPtr = esp_netifs[interf];
if (!interfPtr)
return std::unexpected(fmt::format("esp_netifs[{}] is invalid", std::to_underlying(interf)));
return std::unexpected(std::format("esp_netifs[{}] is invalid", std::to_underlying(interf)));
return send(interfPtr, port, buf);
}
@@ -54,7 +52,7 @@ std::expected<void, std::string> UdpSender::send(esp_netif_t *interf, uint16_t p
esp_netif_ip_info_t ip;
if (const auto result = esp_netif_get_ip_info(interf, &ip); result != ESP_OK)
return std::unexpected(fmt::format("esp_netif_get_ip_info() failed with {}", esp_err_to_name(result)));
return std::unexpected(std::format("esp_netif_get_ip_info() failed with {}", esp_err_to_name(result)));
return send(ip, port, buf);
}
@@ -77,9 +75,9 @@ std::expected<void, std::string> UdpSender::send(const struct sockaddr_in &recip
return std::unexpected("initializing failed, not ready to send");
if (const ssize_t sent = sendto(m_udp_server, buf.data(), buf.size(), 0, (const struct sockaddr*)&recipient, sizeof(recipient)); sent < 0)
return std::unexpected(fmt::format("send failed with {} (errno={})", sent, errno));
return std::unexpected(std::format("send failed with {} (errno={})", sent, errno));
else if (sent != buf.size())
return std::unexpected(fmt::format("sent bytes does not match, expected={}, sent={}", buf.size(), sent));
return std::unexpected(std::format("sent bytes does not match, expected={}, sent={}", buf.size(), sent));
return {};
}
@@ -90,9 +88,9 @@ std::expected<void, std::string> UdpSender::send(const struct sockaddr_in6 &reci
return std::unexpected("initializing failed, not ready to send");
if (const ssize_t sent = sendto(m_udp_server, buf.data(), buf.size(), 0, (const struct sockaddr*)&recipient, sizeof(recipient)); sent < 0)
return std::unexpected(fmt::format("send failed with {} (errno={})", sent, errno));
return std::unexpected(std::format("send failed with {} (errno={})", sent, errno));
else if (sent != buf.size())
return std::unexpected(fmt::format("sent bytes does not match, expected={}, sent={}", buf.size(), sent));
return std::unexpected(std::format("sent bytes does not match, expected={}, sent={}", buf.size(), sent));
return {};
}
@@ -122,7 +120,7 @@ std::expected<void, std::string> UdpSender::send(ip_addr_t ip, uint16_t port, st
return send(recipient, buf);
}
}
return std::unexpected(fmt::format("unsupported ip type {}", ip.type));
return std::unexpected(std::format("unsupported ip type {}", ip.type));
}
std::expected<void, std::string> UdpSender::send(esp_ip_addr_t ip, uint16_t port, std::string_view buf)
@@ -150,7 +148,7 @@ std::expected<void, std::string> UdpSender::send(esp_ip_addr_t ip, uint16_t port
return send(recipient, buf);
}
}
return std::unexpected(fmt::format("unsupported ip type {}", ip.type));
return std::unexpected(std::format("unsupported ip type {}", ip.type));
}
} // namespace wifi_stack