]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Extract send_cached_stderr into Util::send_to_stderr
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 4 Jul 2020 11:52:01 +0000 (13:52 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 6 Jul 2020 17:42:41 +0000 (19:42 +0200)
src/Util.cpp
src/Util.hpp
src/ccache.cpp

index e0897f4a7a6ffe5c7970fe8b61ffc5e0ed6f9c6e..ebbcc483a40760912d98e93bd00bf3b0c4da51de 100644 (file)
@@ -739,6 +739,26 @@ remove_extension(string_view path)
   return path.substr(0, path.length() - get_extension(path).length());
 }
 
+void
+send_to_stderr(const std::string& text, bool strip_colors)
+{
+  const std::string* text_to_send = &text;
+  std::string stripped_text;
+
+  if (strip_colors) {
+    try {
+      stripped_text = Util::strip_ansi_csi_seqs(text);
+      text_to_send = &stripped_text;
+    } catch (const Error&) {
+      // Fall through
+    }
+  }
+
+  if (!write_fd(STDERR_FILENO, text_to_send->data(), text_to_send->length())) {
+    throw Error("Failed to write to stderr");
+  }
+}
+
 std::vector<string_view>
 split_into_views(string_view input, const char* separators)
 {
index 59e0852a2a12aa8b28a90463e9c4e38ad3d81161..c6c2657a44f3d0981ecb3d391a73f2920ad2df59 100644 (file)
@@ -302,6 +302,10 @@ std::string real_path(const std::string& path,
 // extension as determined by `get_extension()`.
 nonstd::string_view remove_extension(nonstd::string_view path);
 
+// Send `text` to STDERR_FILENO, optionally stripping ANSI color sequences if
+// `strip_colors` is true. Throws `Error` on error.
+void send_to_stderr(const std::string& text, bool strip_colors);
+
 // Split `input` into words at any of the characters listed in `separators`.
 // These words are a view into `input`; empty words are omitted. `separators`
 // must neither be the empty string nor a nullptr.
index e0d1fde87e60b0fe9149570f1dfd967859edc087..cd6ff77fff5e1c1a4c7f71fac35ca229a26bf48e 100644 (file)
@@ -737,25 +737,6 @@ execute(Context& ctx,
   return status;
 }
 
-// Send cached stderr, if any, to stderr.
-static void
-send_cached_stderr(const std::string& path_stderr, bool strip_colors)
-{
-  if (strip_colors) {
-    try {
-      auto stripped = Util::strip_ansi_csi_seqs(Util::read_file(path_stderr));
-      write_fd(STDERR_FILENO, stripped.data(), stripped.size());
-    } catch (const Error&) {
-      // Fall through
-    }
-  } else {
-    Fd fd_stderr(open(path_stderr.c_str(), O_RDONLY | O_BINARY));
-    if (fd_stderr) {
-      copy_fd(*fd_stderr, STDERR_FILENO);
-    }
-  }
-}
-
 // Create or update the manifest file.
 static void
 update_manifest_file(Context& ctx)
@@ -939,7 +920,8 @@ to_cache(Context& ctx,
     cc_log("Compiler gave exit status %d", status);
 
     // We can output stderr immediately instead of rerunning the compiler.
-    send_cached_stderr(tmp_stderr, ctx.args_info.strip_diagnostics_colors);
+    Util::send_to_stderr(Util::read_file(tmp_stderr),
+                         ctx.args_info.strip_diagnostics_colors);
 
     failed(STATS_STATUS, status);
   }
@@ -1033,7 +1015,8 @@ to_cache(Context& ctx,
   }
 
   // Everything OK.
-  send_cached_stderr(tmp_stderr, ctx.args_info.strip_diagnostics_colors);
+  Util::send_to_stderr(Util::read_file(tmp_stderr),
+                       ctx.args_info.strip_diagnostics_colors);
 }
 
 // Find the result name by running the compiler in preprocessor mode and
@@ -1733,7 +1716,8 @@ from_cache(Context& ctx, enum fromcache_call_mode mode)
 
   MTR_END("file", "file_get");
 
-  send_cached_stderr(tmp_stderr, ctx.args_info.strip_diagnostics_colors);
+  Util::send_to_stderr(Util::read_file(tmp_stderr),
+                       ctx.args_info.strip_diagnostics_colors);
 
   cc_log("Succeeded getting cached result");