From 6594b50e57935dd66930ccd35dba7a1b4131399d Mon Sep 17 00:00:00 2001 From: memchr <118117622+memchr@users.noreply.github.com> Date: Wed, 20 Sep 2023 07:26:20 +0000 Subject: logging/format: use std::format_string to catch formatting string errors at compile time (#3377) * fix(log): use constexpr format string * deprecate getFormat --- hyprctl/main.cpp | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) (limited to 'hyprctl/main.cpp') diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index 6f418c05..aebc1cbb 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -106,20 +107,6 @@ std::vector instances() { return result; } -std::string getFormat(const char* fmt, ...) { - char* outputStr = nullptr; - - va_list args; - va_start(args, fmt); - vasprintf(&outputStr, fmt, args); - va_end(args); - - std::string output = std::string(outputStr); - free(outputStr); - - return output; -} - void request(std::string arg, int minArgs = 0) { const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0); @@ -248,19 +235,19 @@ void instancesRequest(bool json) { if (!json) { for (auto& el : inst) { - result += getFormat("instance %s:\n\ttime: %llu\n\tpid: %llu\n\twl socket: %s\n\n", el.id.c_str(), el.time, el.pid, el.wlSocket.c_str()); + result += std::format("instance {}:\n\ttime: {}\n\tpid: {}\n\twl socket: {}\n\n", el.id, el.time, el.pid, el.wlSocket); } } else { result += '['; for (auto& el : inst) { - result += getFormat(R"#( -{ - "instance": "%s", - "time": %llu, - "pid": %llu, - "wl_socket": "%s" -},)#", - el.id.c_str(), el.time, el.pid, el.wlSocket.c_str()); + result += std::format(R"#( +{{ + "instance": "{}", + "time": {}, + "pid": {}, + "wl_socket": "{}" +}},)#", + el.id, el.time, el.pid, el.wlSocket); } result.pop_back(); -- cgit v1.2.3