From 6b0a4ea4eeed0b2e31e436d50e76a03729dfd62e Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sat, 4 Jul 2020 13:52:01 +0200 Subject: [PATCH] Extract send_cached_stderr into Util::send_to_stderr --- src/Util.cpp | 20 ++++++++++++++++++++ src/Util.hpp | 4 ++++ src/ccache.cpp | 28 ++++++---------------------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/Util.cpp b/src/Util.cpp index e0897f4a7..ebbcc483a 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -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 split_into_views(string_view input, const char* separators) { diff --git a/src/Util.hpp b/src/Util.hpp index 59e0852a2..c6c2657a4 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -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. diff --git a/src/ccache.cpp b/src/ccache.cpp index e0d1fde87..cd6ff77ff 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -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"); -- 2.47.2