{
if (m_dest_file_type == FileType::stderr_output) {
LOG("Writing to file descriptor {}", STDERR_FILENO);
- Util::send_to_stderr(m_ctx, m_dest_data);
+ Util::send_to_fd(m_ctx, m_dest_data, STDERR_FILENO);
} else if (m_dest_file_type == FileType::dependency && !m_dest_path.empty()) {
write_dependency_file();
}
#include "Win32Util.hpp"
#include "fmtmacros.hpp"
+#include <Finalizer.hpp>
#include <core/exceptions.hpp>
#include <core/wincompat.hpp>
#include <util/path.hpp>
#endif
}
-#ifdef _WIN32
-// Stdout/stderr are normally opened in text mode, which would convert newlines
-// a second time, since we treat outputs as binary data.
-// Make sure to switch to binary mode.
-namespace {
-struct BinaryModeHelper
-{
- BinaryModeHelper(int fd_) : fd(fd_), oldmode(_setmode(fd, _O_BINARY))
- {
- }
- ~BinaryModeHelper()
- {
- _setmode(fd, oldmode);
- }
- int fd;
- int oldmode;
-};
-} // namespace
-#endif
-
void
send_to_fd(const Context& ctx, const std::string& text, int fd)
{
std::string modified_text;
#ifdef _WIN32
- BinaryModeHelper helper(fd);
+ // stdout/stderr are normally opened in text mode, which would convert
+ // newlines a second time since we treat output as binary data. Make sure to
+ // switch to binary mode.
+ int oldmode = _setmode(fd, _O_BINARY);
+ Finalizer binary_mode_restorer([=] { _setmode(fd, oldmode); });
#endif
if (ctx.args_info.strip_diagnostics_colors) {
#include <utility>
#include <vector>
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
class Context;
namespace Util {
bool same_program_name(nonstd::string_view program_name,
nonstd::string_view canonical_program_name);
-// Send `text` to file descriptor 'fd', optionally stripping ANSI color
+// Send `text` to file descriptor `fd`, optionally stripping ANSI color
// sequences if `ctx.args_info.strip_diagnostics_colors` is true and rewriting
// paths to absolute if `ctx.config.absolute_paths_in_stderr` is true. Throws
// `core::Error` on error.
void send_to_fd(const Context& ctx, const std::string& text, int fd);
-inline void
-send_to_stderr(const Context& ctx, const std::string& text)
-{
- send_to_fd(ctx, text, STDERR_FILENO);
-}
// Set the FD_CLOEXEC on file descriptor `fd`. This is a NOP on Windows.
void set_cloexec_flag(int fd);
LOG("Compiler gave exit status {}", *status);
// We can output stderr immediately instead of rerunning the compiler.
- Util::send_to_stderr(ctx, Util::read_file(tmp_stderr_path));
+ Util::send_to_fd(ctx, Util::read_file(tmp_stderr_path), STDERR_FILENO);
auto failure = Failure(Statistic::compile_failed);
failure.set_exit_code(*status);
}
// Everything OK.
- Util::send_to_stderr(ctx, Util::read_file(tmp_stderr_path));
+ Util::send_to_fd(ctx, Util::read_file(tmp_stderr_path), STDERR_FILENO);
return *result_key;
}