]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Improve PRINT macro to accept format string without arguments
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 7 Feb 2026 18:11:52 +0000 (19:11 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 13 Feb 2026 21:17:42 +0000 (22:17 +0100)
src/ccache/ccache.cpp
src/ccache/core/mainoptions.cpp
src/ccache/core/manifest.cpp
src/ccache/storage/local/localstorage.cpp
src/ccache/test_lockfile.cpp
src/ccache/util/format.hpp
test/storage/client/main.cpp

index e946b4569f292086de56579520c64ee1ab9d7bda..59e015cfa76bd4b1cf710fd5948bfd82c32ba1ae 100644 (file)
@@ -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);
       }
index c1faff3e7810851f5b260e948ddc1d6b49c0d7d8..06691e97b0cc6a236f1ace0bf1b1ab81099f3215 100644 (file)
@@ -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");
     }
   }
 
index b635a2888d8ed20978b3f7736bc309a1a3c7eb22..b43a7b623a95c084097c20fc577147dea3e44d76 100644 (file)
@@ -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));
   }
 }
index 3fe11a27f708308e5e03daae56e6c083948d6974..e4f7bf4b678c76bf0195c723c469fe1f844ce058 100644 (file)
@@ -973,7 +973,7 @@ LocalStorage::recompress(const std::optional<int8_t> 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<int8_t> level,
     *size_diff_unit,
   });
 
-  PRINT_RAW(stdout, table.render());
+  PRINT(stdout, "{}", table.render());
 }
 
 // Private methods
index 8232e2bdcd904a75bcd578d6760ff6fcac403577..55b761bb2eafd6e7da610a500409a34eef60a7d0 100644 (file)
@@ -31,9 +31,9 @@ int
 main(int argc, char** argv)
 {
   if (argc != 5) {
-    PRINT_RAW(stderr,
-              "Usage: test-lockfile PATH SECONDS <short|long>"
-              " <blocking|non-blocking>\n");
+    PRINT(stderr,
+          "Usage: test-lockfile PATH SECONDS <short|long>"
+          " <blocking|non-blocking>\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");
 }
index eea6f27fa6a3304ee50a4fe62a2baaefa9dd1e72..bc7aa0e67bd5ff264a9a1c59e731902f8a1b77c3 100644 (file)
 #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<std::filesystem::path> : fmt::formatter<std::string_view>
index 43da580c133adf0a016b516774e5f0902f215cc3..633d6676aa079bb45c8e7684a6770acffcffc5c7 100644 (file)
@@ -70,8 +70,8 @@ int
 cmd_get(storage::remote::Client& client, const std::vector<std::string>& 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<std::string>& 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<std::string>& 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<std::string>& 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<std::string>& 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<std::string>& 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;
   }