Fix flushing C++ iostreams before calling write_console() (#3689)

This change correctly implements https://wg21.link/P2539/ for both
C streams and C++ iostreams.

Fixes #3688.
This commit is contained in:
Dimitrij Mijoski
2023-10-25 22:13:31 +02:00
committed by GitHub
parent 3b7f58a8b3
commit 2a2c6e676f
3 changed files with 37 additions and 20 deletions

View File

@ -1425,16 +1425,13 @@ FMT_FUNC std::string vformat(string_view fmt, format_args args) {
namespace detail { namespace detail {
#if !defined(_WIN32) || defined(FMT_WINDOWS_NO_WCHAR) #if !defined(_WIN32) || defined(FMT_WINDOWS_NO_WCHAR)
FMT_FUNC bool write_console(std::FILE*, string_view) { return false; } FMT_FUNC bool write_console(int, string_view) { return false; }
#else #else
using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>; using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>;
extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( // extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( //
void*, const void*, dword, dword*, void*); void*, const void*, dword, dword*, void*);
FMT_FUNC bool write_console(std::FILE* f, string_view text) { FMT_FUNC bool write_console(int fd, string_view text) {
int fd = _fileno(f);
if (!_isatty(fd)) return false;
std::fflush(f);
auto u16 = utf8_to_utf16(text); auto u16 = utf8_to_utf16(text);
return WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), u16.c_str(), return WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), u16.c_str(),
static_cast<dword>(u16.size()), nullptr, nullptr) != 0; static_cast<dword>(u16.size()), nullptr, nullptr) != 0;
@ -1451,7 +1448,14 @@ FMT_FUNC void vprint_mojibake(std::FILE* f, string_view fmt, format_args args) {
#endif #endif
FMT_FUNC void print(std::FILE* f, string_view text) { FMT_FUNC void print(std::FILE* f, string_view text) {
if (!write_console(f, text)) fwrite_fully(text.data(), text.size(), f); #ifdef _WIN32
int fd = _fileno(f);
if (_isatty(fd)) {
std::fflush(f);
if (write_console(fd, text)) return;
}
#endif
fwrite_fully(text.data(), text.size(), f);
} }
} // namespace detail } // namespace detail

View File

@ -1038,7 +1038,7 @@ struct is_contiguous<basic_memory_buffer<T, SIZE, Allocator>> : std::true_type {
FMT_END_EXPORT FMT_END_EXPORT
namespace detail { namespace detail {
FMT_API bool write_console(std::FILE* f, string_view text); FMT_API bool write_console(int fd, string_view text);
FMT_API void print(std::FILE*, string_view); FMT_API void print(std::FILE*, string_view);
} // namespace detail } // namespace detail

View File

@ -10,9 +10,12 @@
#include <fstream> // std::filebuf #include <fstream> // std::filebuf
#if defined(_WIN32) && defined(__GLIBCXX__) #ifdef _WIN32
# include <ext/stdio_filebuf.h> # ifdef __GLIBCXX__
# include <ext/stdio_sync_filebuf.h> # include <ext/stdio_filebuf.h>
# include <ext/stdio_sync_filebuf.h>
# endif
# include <io.h>
#endif #endif
#include "format.h" #include "format.h"
@ -38,21 +41,31 @@ auto get_file(std::filebuf&) -> FILE*;
#endif #endif
inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) { inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
FILE* f = nullptr;
#if FMT_MSC_VERSION #if FMT_MSC_VERSION
if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf())) if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
if (FILE* f = get_file(*buf)) return write_console(f, data); f = get_file(*buf);
#elif defined(_WIN32) && defined(__GLIBCXX__) else
auto* rdbuf = os.rdbuf(); return false;
FILE* c_file; #elif defined(_WIN32) && defined(__GLIBCXX__)
if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf)) auto* rdbuf = os.rdbuf();
c_file = sfbuf->file(); if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf)) f = sfbuf->file();
c_file = fbuf->file(); else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
f = fbuf->file();
else else
return false; return false;
if (c_file) return write_console(c_file, data);
#else #else
ignore_unused(os, data); ignore_unused(os, data, f);
#endif
#ifdef _WIN32
if (f) {
int fd = _fileno(f);
if (_isatty(fd)) {
os.flush();
return write_console(fd, data);
}
}
#endif #endif
return false; return false;
} }