Fix writing to stdout when redirected to NUL on Windows (#2080)

This commit is contained in:
Victor Zverovich 2020-12-30 12:20:17 -08:00
parent cdc5ef6710
commit 9ec5592bb5

View File

@ -2768,12 +2768,13 @@ FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
if (_isatty(fd)) {
detail::utf8_to_utf16 u16(string_view(buffer.data(), buffer.size()));
auto written = detail::dword();
if (!detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)),
u16.c_str(), static_cast<uint32_t>(u16.size()),
&written, nullptr)) {
FMT_THROW(format_error("failed to write to console"));
if (detail::WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)),
u16.c_str(), static_cast<uint32_t>(u16.size()),
&written, nullptr)) {
return;
}
return;
// Fallback to fwrite on failure. It can happen if the output has been
// redirected to NUL.
}
#endif
detail::fwrite_fully(buffer.data(), 1, buffer.size(), f);