From 7dd34549cd028882bd3f9ac9c4c049ff1ace7133 Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Tue, 22 Sep 2026 03:38:31 -0500 Subject: [PATCH] make stderr unconditionally unbuffered windows `freopen` opens the file in _buffered_ mode, which is undesirable for a log file --- docs/changelog.txt | 1 + library/Core.cpp | 3 +++ 2 files changed, 4 insertions(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index 8b4ab870bb..9878e46453 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -105,6 +105,7 @@ Template for new versions: ## Misc Improvements - Added ``Coord2d`` and ``Coord3d`` C++ templates, providing a standard set operations for 2-tuples and 3-tuples of any numeric type - Renamed the ``stockpiles`` tool tag to ``stockpile`` so it no longer collides with the `stockpiles` plugin in `gui/launcher` +- Writes to ``stderr.log`` are no longer buffered on Windows, so log output is not lost if DF crashes ## Documentation - Document ``autodump-destroy-here``. Remove mention of ``autodump destroy`` able to revert destruction. diff --git a/library/Core.cpp b/library/Core.cpp index 9ff34386c8..a17e5bf33c 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1114,6 +1114,9 @@ bool Core::InitMainThread(std::filesystem::path path) { fprintf(stderr, "dfhack: redirecting stderr to stderr.log\n"); if (!freopen("stderr.log", "w", stderr)) std::cerr << "Could not redirect stderr to stderr.log" << std::endl; + // freopen makes stderr fully buffered on Windows; restore unbuffered + // writes so that stderr.log is up to date (e.g. in the event of a crash) + setvbuf(stderr, NULL, _IONBF, 0); std::cerr << "DFHack build: " << Version::git_description() << std::endl; if (strlen(Version::dfhack_run_url())) {