From: Joel Rosdahl Date: Sat, 7 Feb 2026 18:11:52 +0000 (+0100) Subject: refactor: Improve PRINT macro to accept format string without arguments X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7c9ddcff4a1129c1769d0f562e86eb35ae2cdb05;p=thirdparty%2Fccache.git refactor: Improve PRINT macro to accept format string without arguments --- diff --git a/src/ccache/ccache.cpp b/src/ccache/ccache.cpp index e946b456..59e015cf 100644 --- a/src/ccache/ccache.cpp +++ b/src/ccache/ccache.cpp @@ -3148,8 +3148,9 @@ ccache_main(int argc, const char* const* argv) try { if (is_ccache_executable(argv[0])) { if (argc < 2) { - PRINT_RAW( + PRINT( stderr, + "{}", core::get_usage_text(util::pstr(fs::path(argv[0]).filename()).str())); exit(EXIT_FAILURE); } diff --git a/src/ccache/core/mainoptions.cpp b/src/ccache/core/mainoptions.cpp index c1faff3e..06691e97 100644 --- a/src/ccache/core/mainoptions.cpp +++ b/src/ccache/core/mainoptions.cpp @@ -305,7 +305,7 @@ print_compression_statistics(const Config& config, *incompressible_data_unit, }); - PRINT_RAW(stdout, table.render()); + PRINT(stdout, "{}", table.render()); } static void @@ -683,9 +683,9 @@ process_main_options(int argc, const char* const* argv) const auto [counters, last_updated] = storage::local::LocalStorage(config).get_all_statistics(); Statistics statistics(counters); - PRINT_RAW( - stdout, - statistics.format_machine_readable(config, last_updated, format)); + PRINT(stdout, + "{}", + statistics.format_machine_readable(config, last_updated, format)); break; } @@ -695,7 +695,7 @@ process_main_options(int argc, const char* const* argv) storage::local::LocalStorage(config).clean_all( [&](double progress) { progress_bar.update(progress); }); if (isatty(STDOUT_FILENO)) { - PRINT_RAW(stdout, "\n"); + PRINT(stdout, "\n"); } break; } @@ -706,7 +706,7 @@ process_main_options(int argc, const char* const* argv) storage::local::LocalStorage(config).wipe_all( [&](double progress) { progress_bar.update(progress); }); if (isatty(STDOUT_FILENO)) { - PRINT_RAW(stdout, "\n"); + PRINT(stdout, "\n"); } break; } @@ -724,7 +724,7 @@ process_main_options(int argc, const char* const* argv) config.set_value_in_file( util::pstr(config.config_path()), "max_files", arg); if (files == 0) { - PRINT_RAW(stdout, "Unset cache file limit\n"); + PRINT(stdout, "Unset cache file limit\n"); } else { PRINT(stdout, "Set cache file limit to {}\n", files); } @@ -738,7 +738,7 @@ process_main_options(int argc, const char* const* argv) config.set_value_in_file( util::pstr(config.config_path()), "max_size", arg); if (max_size == 0) { - PRINT_RAW(stdout, "Unset cache size limit\n"); + PRINT(stdout, "Unset cache size limit\n"); } else { PRINT(stdout, "Set cache size limit to {}\n", @@ -771,8 +771,9 @@ process_main_options(int argc, const char* const* argv) Statistics statistics(StatsLog(config.stats_log()).read()); const auto timestamp = DirEntry(config.stats_log(), DirEntry::LogOnError::yes).mtime(); - PRINT_RAW( + PRINT( stdout, + "{}", statistics.format_human_readable(config, timestamp, verbosity, true)); break; } @@ -784,8 +785,9 @@ process_main_options(int argc, const char* const* argv) Statistics statistics(StatsLog(config.stats_log()).read()); const auto timestamp = DirEntry(config.stats_log(), DirEntry::LogOnError::yes).mtime(); - PRINT_RAW(stdout, - statistics.format_machine_readable(config, timestamp, format)); + PRINT(stdout, + "{}", + statistics.format_machine_readable(config, timestamp, format)); break; } @@ -793,9 +795,10 @@ process_main_options(int argc, const char* const* argv) const auto [counters, last_updated] = storage::local::LocalStorage(config).get_all_statistics(); Statistics statistics(counters); - PRINT_RAW(stdout, - statistics.format_human_readable( - config, last_updated, verbosity, false)); + PRINT(stdout, + "{}", + statistics.format_human_readable( + config, last_updated, verbosity, false)); break; } @@ -819,8 +822,9 @@ process_main_options(int argc, const char* const* argv) case 'V': // --version { - PRINT_RAW(stdout, - get_version_text(util::pstr(fs::path(argv[0]).stem()).str())); + PRINT(stdout, + "{}", + get_version_text(util::pstr(fs::path(argv[0]).stem()).str())); break; } @@ -835,7 +839,7 @@ process_main_options(int argc, const char* const* argv) storage::local::LocalStorage(config).get_compression_statistics( threads, [&](double progress) { progress_bar.update(progress); }); if (isatty(STDOUT_FILENO)) { - PRINT_RAW(stdout, "\n\n"); + PRINT(stdout, "\n\n"); } print_compression_statistics(config, compression_statistics); break; @@ -855,7 +859,7 @@ process_main_options(int argc, const char* const* argv) case 'z': // --zero-stats storage::local::LocalStorage(config).zero_all_statistics(); - PRINT_RAW(stdout, "Statistics zeroed\n"); + PRINT(stdout, "Statistics zeroed\n"); break; default: @@ -874,7 +878,7 @@ process_main_options(int argc, const char* const* argv) evict_max_age, evict_namespace); if (isatty(STDOUT_FILENO)) { - PRINT_RAW(stdout, "\n"); + PRINT(stdout, "\n"); } } diff --git a/src/ccache/core/manifest.cpp b/src/ccache/core/manifest.cpp index b635a288..b43a7b62 100644 --- a/src/ccache/core/manifest.cpp +++ b/src/ccache/core/manifest.cpp @@ -465,7 +465,7 @@ Manifest::inspect(FILE* const stream) const stream, " Hash: {}\n", util::format_base16(m_file_infos[i].digest)); PRINT(stream, " File size: {}\n", m_file_infos[i].fsize); if (m_file_infos[i].mtime == util::TimePoint()) { - PRINT_RAW(stream, " Mtime: -\n"); + PRINT(stream, " Mtime: -\n"); } else { PRINT(stream, " Mtime: {}.{:09}\n", @@ -473,7 +473,7 @@ Manifest::inspect(FILE* const stream) const util::nsec_part(m_file_infos[i].mtime)); } if (m_file_infos[i].ctime == util::TimePoint()) { - PRINT_RAW(stream, " Ctime: -\n"); + PRINT(stream, " Ctime: -\n"); } else { PRINT(stream, " Ctime: {}.{:09}\n", @@ -485,11 +485,11 @@ Manifest::inspect(FILE* const stream) const PRINT(stream, "Results ({}):\n", m_results.size()); for (size_t i = 0; i < m_results.size(); ++i) { PRINT(stream, " {}:\n", i); - PRINT_RAW(stream, " File info indexes:"); + PRINT(stream, " File info indexes:"); for (uint32_t file_info_index : m_results[i].file_info_indexes) { PRINT(stream, " {}", file_info_index); } - PRINT_RAW(stream, "\n"); + PRINT(stream, "\n"); PRINT(stream, " Key: {}\n", util::format_base16(m_results[i].key)); } } diff --git a/src/ccache/storage/local/localstorage.cpp b/src/ccache/storage/local/localstorage.cpp index 3fe11a27..e4f7bf4b 100644 --- a/src/ccache/storage/local/localstorage.cpp +++ b/src/ccache/storage/local/localstorage.cpp @@ -973,7 +973,7 @@ LocalStorage::recompress(const std::optional level, thread_pool.shut_down(); if (isatty(STDOUT_FILENO)) { - PRINT_RAW(stdout, "\n\n"); + PRINT(stdout, "\n\n"); } const double old_ratio = @@ -1043,7 +1043,7 @@ LocalStorage::recompress(const std::optional level, *size_diff_unit, }); - PRINT_RAW(stdout, table.render()); + PRINT(stdout, "{}", table.render()); } // Private methods diff --git a/src/ccache/test_lockfile.cpp b/src/ccache/test_lockfile.cpp index 8232e2bd..55b761bb 100644 --- a/src/ccache/test_lockfile.cpp +++ b/src/ccache/test_lockfile.cpp @@ -31,9 +31,9 @@ int main(int argc, char** argv) { if (argc != 5) { - PRINT_RAW(stderr, - "Usage: test-lockfile PATH SECONDS " - " \n"); + PRINT(stderr, + "Usage: test-lockfile PATH SECONDS " + " \n"); return 1; } Config config; @@ -45,7 +45,7 @@ main(int argc, char** argv) const bool long_lived = std::string(argv[3]) == "long"; const bool blocking = std::string(argv[4]) == "blocking"; if (!seconds) { - PRINT_RAW(stderr, "Error: Failed to parse seconds\n"); + PRINT(stderr, "Error: Failed to parse seconds\n"); return 1; } @@ -53,10 +53,10 @@ main(int argc, char** argv) util::LockFile lock(path); bool acquired = false; if (blocking) { - PRINT_RAW(stdout, "Acquiring\n"); + PRINT(stdout, "Acquiring\n"); acquired = lock.acquire(); } else { - PRINT_RAW(stdout, "Trying to acquire\n"); + PRINT(stdout, "Trying to acquire\n"); acquired = lock.try_acquire(); } @@ -65,13 +65,13 @@ main(int argc, char** argv) return 1; } - PRINT_RAW(stdout, "Acquired\n"); + PRINT(stdout, "Acquired\n"); if (long_lived) { lock.make_long_lived(lock_manager); } PRINT(stdout, "Sleeping {} second{}\n", *seconds, *seconds == 1 ? "" : "s"); std::this_thread::sleep_for(std::chrono::seconds{*seconds}); - PRINT_RAW(stdout, "Releasing\n"); + PRINT(stdout, "Releasing\n"); lock.release(); - PRINT_RAW(stdout, "Released\n"); + PRINT(stdout, "Released\n"); } diff --git a/src/ccache/util/format.hpp b/src/ccache/util/format.hpp index eea6f27f..bc7aa0e6 100644 --- a/src/ccache/util/format.hpp +++ b/src/ccache/util/format.hpp @@ -29,11 +29,8 @@ #define FMT(format_, ...) fmt::format(format_, __VA_ARGS__) // Convenience macro for `fmt::print`. -#define PRINT(stream_, format_, ...) fmt::print(stream_, format_, __VA_ARGS__) - -// Convenience macro for calling `fmt::print` with a message that is not a -// format string. -#define PRINT_RAW(stream_, message_) fmt::print(stream_, "{}", message_) +#define PRINT(stream_, format_, ...) \ + fmt::print(stream_, format_ __VA_OPT__(, ) __VA_ARGS__) template<> struct fmt::formatter : fmt::formatter diff --git a/test/storage/client/main.cpp b/test/storage/client/main.cpp index 43da580c..633d6676 100644 --- a/test/storage/client/main.cpp +++ b/test/storage/client/main.cpp @@ -70,8 +70,8 @@ int cmd_get(storage::remote::Client& client, const std::vector& args) { if (args.size() != 3 || args[1] != "-o") { - PRINT_RAW(stderr, "Error: get requires: KEY -o OUTPUT\n"); - PRINT_RAW(stderr, " where OUTPUT is a file path or - for stdout\n"); + PRINT(stderr, "Error: get requires: KEY -o OUTPUT\n"); + PRINT(stderr, " where OUTPUT is a file path or - for stdout\n"); return 1; } @@ -121,10 +121,10 @@ cmd_put(storage::remote::Client& client, const std::vector& args) } if (args.size() - start_idx != 3) { - PRINT_RAW(stderr, - "Error: put requires: [--overwrite] KEY -i INPUT\n" - " or: [--overwrite] KEY -v VALUE\n" - " where INPUT is a file path or - for stdin\n"); + PRINT(stderr, + "Error: put requires: [--overwrite] KEY -i INPUT\n" + " or: [--overwrite] KEY -v VALUE\n" + " where INPUT is a file path or - for stdin\n"); return 1; } @@ -183,7 +183,7 @@ cmd_remove(storage::remote::Client& client, const std::vector& args) { if (args.size() != 1) { - PRINT_RAW(stderr, "Error: remove requires exactly 1 argument: KEY\n"); + PRINT(stderr, "Error: remove requires exactly 1 argument: KEY\n"); return 1; } @@ -214,7 +214,7 @@ int cmd_stop(storage::remote::Client& client, const std::vector& args) { if (!args.empty()) { - PRINT_RAW(stderr, "Error: stop takes no arguments\n"); + PRINT(stderr, "Error: stop takes no arguments\n"); return 1; } @@ -225,7 +225,7 @@ cmd_stop(storage::remote::Client& client, const std::vector& args) return 1; } - PRINT_RAW(stdout, "Helper stopped\n"); + PRINT(stdout, "Helper stopped\n"); return 0; } @@ -233,12 +233,12 @@ int cmd_ping(storage::remote::Client& client, const std::vector& args) { if (!args.empty()) { - PRINT_RAW(stderr, "Error: ping takes no arguments\n"); + PRINT(stderr, "Error: ping takes no arguments\n"); return 1; } // Connection and protocol verification already done in main. - PRINT_RAW(stdout, "Helper is reachable\n"); + PRINT(stdout, "Helper is reachable\n"); return 0; } @@ -291,8 +291,7 @@ main(int argc, char* argv[]) if (!client.has_capability( storage::remote::Client::Capability::get_put_remove_stop)) { - PRINT_RAW(stderr, - "Helper does not support get/put/remove/stop operations\n"); + PRINT(stderr, "Helper does not support get/put/remove/stop operations\n"); return 1; }