Skip to content

Commit 3b7f58a

Browse files
Roman-Koshelevvitaut
authored andcommitted
add buffer flush before direct write
1 parent e9bbd40 commit 3b7f58a

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

include/fmt/format-inl.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ FMT_FUNC void report_error(format_func func, int error_code,
7373
}
7474

7575
// A wrapper around fwrite that throws on error.
76-
inline void fwrite_fully(const void* ptr, size_t size, size_t count,
77-
FILE* stream) {
78-
size_t written = std::fwrite(ptr, size, count, stream);
76+
inline void fwrite_fully(const void* ptr, size_t count, FILE* stream) {
77+
size_t written = std::fwrite(ptr, 1, count, stream);
7978
if (written < count)
8079
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
8180
}
@@ -1433,13 +1432,12 @@ extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( //
14331432
void*, const void*, dword, dword*, void*);
14341433

14351434
FMT_FUNC bool write_console(std::FILE* f, string_view text) {
1436-
auto fd = _fileno(f);
1435+
int fd = _fileno(f);
14371436
if (!_isatty(fd)) return false;
1437+
std::fflush(f);
14381438
auto u16 = utf8_to_utf16(text);
1439-
auto written = dword();
14401439
return WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), u16.c_str(),
1441-
static_cast<uint32_t>(u16.size()), &written,
1442-
nullptr) != 0;
1440+
static_cast<dword>(u16.size()), nullptr, nullptr) != 0;
14431441
}
14441442
#endif
14451443

@@ -1448,12 +1446,12 @@ FMT_FUNC bool write_console(std::FILE* f, string_view text) {
14481446
FMT_FUNC void vprint_mojibake(std::FILE* f, string_view fmt, format_args args) {
14491447
auto buffer = memory_buffer();
14501448
detail::vformat_to(buffer, fmt, args);
1451-
fwrite_fully(buffer.data(), 1, buffer.size(), f);
1449+
fwrite_fully(buffer.data(), buffer.size(), f);
14521450
}
14531451
#endif
14541452

14551453
FMT_FUNC void print(std::FILE* f, string_view text) {
1456-
if (!write_console(f, text)) fwrite_fully(text.data(), 1, text.size(), f);
1454+
if (!write_console(f, text)) fwrite_fully(text.data(), text.size(), f);
14571455
}
14581456
} // namespace detail
14591457

0 commit comments

Comments
 (0)