prefer std::format over fmt
Now that std::format is implemented we aren't using complex enough fmt
to warrant using it over the std version. Switch to std and remove the
dependency.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ib03cdb6a9db4d25de345bdb49b28157880c19bc1
diff --git a/chassis_state_manager.cpp b/chassis_state_manager.cpp
index bfa8ff8..189f2de 100644
--- a/chassis_state_manager.cpp
+++ b/chassis_state_manager.cpp
@@ -6,9 +6,6 @@
#include "xyz/openbmc_project/Common/error.hpp"
#include "xyz/openbmc_project/State/Shutdown/Power/error.hpp"
-#include <fmt/format.h>
-#include <fmt/printf.h>
-
#include <cereal/archives/json.hpp>
#include <org/freedesktop/UPower/Device/client.hpp>
#include <phosphor-logging/elog-errors.hpp>
@@ -22,6 +19,7 @@
#include <xyz/openbmc_project/State/Decorator/PowerSystemInputs/server.hpp>
#include <filesystem>
+#include <format>
#include <fstream>
namespace phosphor
@@ -76,10 +74,10 @@
{
systemdTargetTable = {
// Use the hard off target to ensure we shutdown immediately
- {Transition::Off, fmt::format(CHASSIS_STATE_HARD_POWEROFF_TGT_FMT, id)},
- {Transition::On, fmt::format(CHASSIS_STATE_POWERON_TGT_FMT, id)},
+ {Transition::Off, std::format(CHASSIS_STATE_HARD_POWEROFF_TGT_FMT, id)},
+ {Transition::On, std::format(CHASSIS_STATE_POWERON_TGT_FMT, id)},
{Transition::PowerCycle,
- fmt::format(CHASSIS_STATE_POWERCYCLE_TGT_FMT, id)}};
+ std::format(CHASSIS_STATE_POWERCYCLE_TGT_FMT, id)}};
}
// TODO - Will be rewritten once sdbusplus client bindings are in place
@@ -98,7 +96,7 @@
powerSysInputsPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>(
bus,
sdbusplus::bus::match::rules::propertiesChangedNamespace(
- fmt::format(
+ std::format(
"/xyz/openbmc_project/power/power_supplies/chassis{}/psus", id),
decoratorServer::PowerSystemInputs::interface),
[this](auto& msg) { this->powerSysInputsChangeEvent(msg); });
@@ -142,12 +140,12 @@
// Reset host sensors since system is off now
// Ensure Power Leds are off.
- startUnit(fmt::format(CHASSIS_BLACKOUT_TGT_FMT, id));
+ startUnit(std::format(CHASSIS_BLACKOUT_TGT_FMT, id));
setStateChangeTime();
// Generate file indicating AC loss occurred
std::string chassisLostPowerFileFmt =
- fmt::sprintf(CHASSIS_LOST_POWER_FILE, id);
+ std::format(CHASSIS_LOST_POWER_FILE, id);
fs::create_directories(BASE_FILE_DIR);
fs::path chassisPowerLossFile{chassisLostPowerFileFmt};
std::ofstream outfile(chassisPowerLossFile);
@@ -222,7 +220,7 @@
info("power status transitioned from {START_PWR_STATE} to Good and "
"chassis power is off, calling APR",
"START_PWR_STATE", initialPowerStatus);
- restartUnit(fmt::format(AUTO_POWER_RESTORE_SVC_FMT, this->id));
+ restartUnit(std::format(AUTO_POWER_RESTORE_SVC_FMT, this->id));
}
}
}
@@ -556,7 +554,7 @@
return 0;
}
- if ((newStateUnit == fmt::format(CHASSIS_STATE_POWEROFF_TGT_FMT, id)) &&
+ if ((newStateUnit == std::format(CHASSIS_STATE_POWEROFF_TGT_FMT, id)) &&
(newStateResult == "done") &&
(!stateActive(systemdTargetTable[Transition::On])))
{
@@ -577,13 +575,10 @@
// This file is used to indicate to chassis related systemd services
// that the chassis is already on and they should skip running.
// Once the chassis state is back to on we can clear this file.
- auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0);
- size++; // null
- std::unique_ptr<char[]> chassisFile(new char[size]);
- std::snprintf(chassisFile.get(), size, CHASSIS_ON_FILE, 0);
- if (std::filesystem::exists(chassisFile.get()))
+ auto chassisFile = std::format(CHASSIS_ON_FILE, 0);
+ if (std::filesystem::exists(chassisFile))
{
- std::filesystem::remove(chassisFile.get());
+ std::filesystem::remove(chassisFile);
}
}
@@ -654,7 +649,7 @@
fs::path Chassis::serializePOH()
{
- fs::path path{fmt::format(POH_COUNTER_PERSIST_PATH, id)};
+ fs::path path{std::format(POH_COUNTER_PERSIST_PATH, id)};
std::ofstream os(path.c_str(), std::ios::binary);
cereal::JSONOutputArchive oarchive(os);
oarchive(pohCounter());
@@ -663,7 +658,7 @@
bool Chassis::deserializePOH(uint32_t& pohCounter)
{
- fs::path path{fmt::format(POH_COUNTER_PERSIST_PATH, id)};
+ fs::path path{std::format(POH_COUNTER_PERSIST_PATH, id)};
try
{
if (fs::exists(path))
@@ -710,7 +705,7 @@
void Chassis::serializeStateChangeTime()
{
- fs::path path{fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)};
+ fs::path path{std::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)};
std::ofstream os(path.c_str(), std::ios::binary);
cereal::JSONOutputArchive oarchive(os);
@@ -720,7 +715,7 @@
bool Chassis::deserializeStateChangeTime(uint64_t& time, PowerState& state)
{
- fs::path path{fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)};
+ fs::path path{std::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)};
try
{
diff --git a/chassis_state_manager_main.cpp b/chassis_state_manager_main.cpp
index af572f1..aecf9a9 100644
--- a/chassis_state_manager_main.cpp
+++ b/chassis_state_manager_main.cpp
@@ -2,7 +2,6 @@
#include "chassis_state_manager.hpp"
-#include <fmt/format.h>
#include <getopt.h>
#include <sdbusplus/bus.hpp>
@@ -10,6 +9,7 @@
#include <cstdlib>
#include <exception>
#include <filesystem>
+#include <format>
#include <iostream>
constexpr auto LEGACY_POH_COUNTER_PERSIST_PATH =
@@ -55,9 +55,9 @@
fs::path legacyPohPath{LEGACY_POH_COUNTER_PERSIST_PATH};
fs::path legacyStateChangePath{LEGACY_STATE_CHANGE_PERSIST_PATH};
- fs::path newPohPath{fmt::format(POH_COUNTER_PERSIST_PATH, chassisId)};
+ fs::path newPohPath{std::format(POH_COUNTER_PERSIST_PATH, chassisId)};
fs::path newStateChangePath{
- fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, chassisId)};
+ std::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, chassisId)};
if (fs::exists(legacyPohPath))
{
fs::rename(legacyPohPath, newPohPath);
diff --git a/discover_system_state.cpp b/discover_system_state.cpp
index 92d0e76..0a20bb3 100644
--- a/discover_system_state.cpp
+++ b/discover_system_state.cpp
@@ -6,8 +6,6 @@
#include "xyz/openbmc_project/Common/error.hpp"
#include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp"
-#include <fmt/format.h>
-#include <fmt/printf.h>
#include <getopt.h>
#include <systemd/sd-bus.h>
diff --git a/host_check.cpp b/host_check.cpp
index d866085..c3e37c8 100644
--- a/host_check.cpp
+++ b/host_check.cpp
@@ -13,6 +13,7 @@
#include <cstdio>
#include <cstdlib>
+#include <format>
#include <fstream>
#include <iostream>
#include <ranges>
@@ -195,11 +196,8 @@
info("Host is running!");
// Create file for host instance and create in filesystem to
// indicate to services that host is running
- auto size = std::snprintf(nullptr, 0, HOST_RUNNING_FILE, 0);
- size++; // null
- std::unique_ptr<char[]> buf(new char[size]);
- std::snprintf(buf.get(), size, HOST_RUNNING_FILE, 0);
- std::ofstream outfile(buf.get());
+ std::string hostFile = std::format(HOST_RUNNING_FILE, 0);
+ std::ofstream outfile(hostFile);
outfile.close();
return true;
}
diff --git a/host_reset_recovery.cpp b/host_reset_recovery.cpp
index a8d3842..3040ebf 100644
--- a/host_reset_recovery.cpp
+++ b/host_reset_recovery.cpp
@@ -11,6 +11,7 @@
#include <xyz/openbmc_project/State/Boot/Progress/client.hpp>
#include <cstdlib>
+#include <format>
#include <fstream>
#include <string>
@@ -114,12 +115,9 @@
// completed and the phosphor-chassis-state-manager code has processed it.
bool isChassisTargetComplete()
{
- auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0);
- size++; // null
- std::unique_ptr<char[]> buf(new char[size]);
- std::snprintf(buf.get(), size, CHASSIS_ON_FILE, 0);
+ auto chassisFile = std::format(CHASSIS_ON_FILE, 0);
- std::ifstream f(buf.get());
+ std::ifstream f(chassisFile);
return !f.good();
}
diff --git a/host_state_manager.cpp b/host_state_manager.cpp
index dd631dc..cf8105f 100644
--- a/host_state_manager.cpp
+++ b/host_state_manager.cpp
@@ -5,7 +5,6 @@
#include "host_check.hpp"
#include "utils.hpp"
-#include <fmt/format.h>
#include <stdio.h>
#include <systemd/sd-bus.h>
@@ -23,6 +22,7 @@
#include <xyz/openbmc_project/State/Host/error.hpp>
#include <filesystem>
+#include <format>
#include <fstream>
#include <iostream>
#include <map>
@@ -87,32 +87,32 @@
void Host::createSystemdTargetMaps()
{
stateTargetTable = {
- {HostState::Off, fmt::format("obmc-host-stop@{}.target", id)},
- {HostState::Running, fmt::format("obmc-host-startmin@{}.target", id)},
- {HostState::Quiesced, fmt::format("obmc-host-quiesce@{}.target", id)},
+ {HostState::Off, std::format("obmc-host-stop@{}.target", id)},
+ {HostState::Running, std::format("obmc-host-startmin@{}.target", id)},
+ {HostState::Quiesced, std::format("obmc-host-quiesce@{}.target", id)},
{HostState::DiagnosticMode,
- fmt::format("obmc-host-diagnostic-mode@{}.target", id)}};
+ std::format("obmc-host-diagnostic-mode@{}.target", id)}};
transitionTargetTable = {
- {Transition::Off, fmt::format("obmc-host-shutdown@{}.target", id)},
- {Transition::On, fmt::format("obmc-host-start@{}.target", id)},
- {Transition::Reboot, fmt::format("obmc-host-reboot@{}.target", id)},
+ {Transition::Off, std::format("obmc-host-shutdown@{}.target", id)},
+ {Transition::On, std::format("obmc-host-start@{}.target", id)},
+ {Transition::Reboot, std::format("obmc-host-reboot@{}.target", id)},
// Some systems do not support a warm reboot so just map the reboot
// requests to our normal cold reboot in that case
#if ENABLE_WARM_REBOOT
{Transition::GracefulWarmReboot,
- fmt::format("obmc-host-warm-reboot@{}.target", id)},
+ std::format("obmc-host-warm-reboot@{}.target", id)},
{Transition::ForceWarmReboot,
- fmt::format("obmc-host-force-warm-reboot@{}.target", id)}
+ std::format("obmc-host-force-warm-reboot@{}.target", id)}
};
#else
{Transition::GracefulWarmReboot,
- fmt::format("obmc-host-reboot@{}.target", id)},
+ std::format("obmc-host-reboot@{}.target", id)},
{Transition::ForceWarmReboot,
- fmt::format("obmc-host-reboot@{}.target", id)}
+ std::format("obmc-host-reboot@{}.target", id)}
};
#endif
- hostCrashTarget = fmt::format("obmc-host-crash@{}.target", id);
+ hostCrashTarget = std::format("obmc-host-crash@{}.target", id);
}
const std::string& Host::getTarget(HostState state)
@@ -298,13 +298,10 @@
// This file is used to indicate to host related systemd services
// that the host is already running and they should skip running.
// Once the host state is back to running we can clear this file.
- auto size = std::snprintf(nullptr, 0, HOST_RUNNING_FILE, 0);
- size++; // null
- std::unique_ptr<char[]> hostFile(new char[size]);
- std::snprintf(hostFile.get(), size, HOST_RUNNING_FILE, 0);
- if (std::filesystem::exists(hostFile.get()))
+ std::string hostFile = std::format(HOST_RUNNING_FILE, 0);
+ if (std::filesystem::exists(hostFile))
{
- std::filesystem::remove(hostFile.get());
+ std::filesystem::remove(hostFile);
}
}
else if ((newStateUnit == getTarget(server::Host::HostState::Quiesced)) &&
@@ -366,7 +363,7 @@
fs::path Host::serialize()
{
- fs::path path{fmt::format(HOST_STATE_PERSIST_PATH, id)};
+ fs::path path{std::format(HOST_STATE_PERSIST_PATH, id)};
std::ofstream os(path.c_str(), std::ios::binary);
cereal::JSONOutputArchive oarchive(os);
oarchive(*this);
@@ -375,7 +372,7 @@
bool Host::deserialize()
{
- fs::path path{fmt::format(HOST_STATE_PERSIST_PATH, id)};
+ fs::path path{std::format(HOST_STATE_PERSIST_PATH, id)};
try
{
if (fs::exists(path))
diff --git a/host_state_manager_main.cpp b/host_state_manager_main.cpp
index 484d6e5..e036ff5 100644
--- a/host_state_manager_main.cpp
+++ b/host_state_manager_main.cpp
@@ -2,7 +2,6 @@
#include "host_state_manager.hpp"
-#include <fmt/format.h>
#include <getopt.h>
#include <sdbusplus/bus.hpp>
@@ -10,6 +9,7 @@
#include <cstdlib>
#include <exception>
#include <filesystem>
+#include <format>
#include <iostream>
constexpr auto LEGACY_HOST_STATE_PERSIST_PATH =
@@ -53,7 +53,7 @@
// exist, rename it to the new file format of host0.
fs::path legacyPath{LEGACY_HOST_STATE_PERSIST_PATH};
- fs::path newPath{fmt::format(HOST_STATE_PERSIST_PATH, hostId)};
+ fs::path newPath{std::format(HOST_STATE_PERSIST_PATH, hostId)};
if (fs::exists(legacyPath))
{
fs::rename(legacyPath, newPath);
diff --git a/meson.build b/meson.build
index 26ffb74..cd01c3a 100644
--- a/meson.build
+++ b/meson.build
@@ -75,10 +75,10 @@
'BASE_FILE_DIR', '/run/openbmc/')
conf.set_quoted(
- 'CHASSIS_LOST_POWER_FILE', '/run/openbmc/chassis@%d-lost-power')
+ 'CHASSIS_LOST_POWER_FILE', '/run/openbmc/chassis@{}-lost-power')
conf.set_quoted(
- 'HOST_RUNNING_FILE', '/run/openbmc/host@%d-on')
+ 'HOST_RUNNING_FILE', '/run/openbmc/host@{}-on')
conf.set_quoted(
'CHASSIS_ON_FILE', '/run/openbmc/chassis@%d-on')
@@ -95,7 +95,6 @@
phosphorlogging = dependency('phosphor-logging')
phosphordbusinterfaces = dependency('phosphor-dbus-interfaces')
libgpiod = dependency('libgpiod', version : '>=1.4.1')
-fmt = dependency('fmt')
if cpp.has_header('CLI/CLI.hpp')
CLI11 = declare_dependency()
@@ -129,7 +128,6 @@
'utils.cpp',
dependencies: [
cereal,
- fmt,
libgpiod,
phosphordbusinterfaces,
phosphorlogging,
@@ -160,7 +158,6 @@
'utils.cpp',
dependencies: [
cereal,
- fmt,
libgpiod,
nlohmann_json_dep,
phosphordbusinterfaces,
@@ -176,7 +173,6 @@
'chassis_check_power_status.cpp',
'utils.cpp',
dependencies: [
- fmt,
libgpiod,
phosphordbusinterfaces,
phosphorlogging,
@@ -191,7 +187,6 @@
'bmc_state_manager_main.cpp',
'utils.cpp',
dependencies: [
- fmt,
libgpiod,
phosphordbusinterfaces,
phosphorlogging,
@@ -208,7 +203,6 @@
'utils.cpp',
dependencies: [
cereal,
- fmt,
libgpiod,
phosphorlogging,
sdbusplus,
@@ -264,7 +258,7 @@
'secure_boot_check.cpp',
'utils.cpp',
dependencies: [
- fmt, sdbusplus,
+ sdbusplus,
phosphorlogging, libgpiod
],
implicit_include_directories: true,
diff --git a/utils.cpp b/utils.cpp
index 332ddc0..e4f7121 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -2,8 +2,6 @@
#include "utils.hpp"
-#include <fmt/format.h>
-#include <fmt/printf.h>
#include <gpiod.h>
#include <phosphor-logging/lg2.hpp>
@@ -14,6 +12,7 @@
#include <chrono>
#include <filesystem>
+#include <format>
namespace phosphor
{
@@ -230,8 +229,8 @@
bool checkACLoss(size_t& chassisId)
{
- std::string chassisLostPowerFileFmt = fmt::sprintf(CHASSIS_LOST_POWER_FILE,
- chassisId);
+ std::string chassisLostPowerFileFmt = std::format(CHASSIS_LOST_POWER_FILE,
+ chassisId);
std::filesystem::path chassisPowerLossFile{chassisLostPowerFileFmt};
if (std::filesystem::exists(chassisPowerLossFile))