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)
{
// 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.
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)
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);
}
}
// 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
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");