Write the log file before stderr, and keep a blocked console from stopping the log and the exit.

A selection in the console window of a game started directly held back the write to stderr, and with it the log
file, the exit hook and the crash report. The log file now comes first, stderr goes past the C runtime's stream lock,
and the lines of an exit, a fatal error or a crash skip stderr while another thread is stuck in it. A game run turns
the console's QuickEdit mode off, and a closed console, Ctrl+C or Ctrl+Break ends the game the way a closed window
does, with the settings and saves written and the exit logged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 05:53:29 -05:00
parent 175033d49a
commit e682934bd0
7 changed files with 124 additions and 11 deletions

View File

@@ -1884,6 +1884,21 @@ int plat_stderr_path(char *out, size_t cap) {
return 1;
}
int plat_stderr_write(const void *p, size_t n) {
for (const char *c = p; n;) {
ssize_t k = write(2, c, n);
if (k < 0 && errno == EINTR) continue;
if (k <= 0) return 0;
c += k, n -= (size_t)k;
}
return 1;
}
int plat_console_setup(void (*on_close)(const char *what)) { // a Windows console's settings only
(void)on_close;
return 0;
}
// ---- process
#ifdef NX2NAVI_PGO_GEN

View File

@@ -462,6 +462,18 @@ int plat_os_version(char *out, size_t cap);
int plat_is_wine(void); // 1 when the Windows build runs under Wine; 0 elsewhere
// The file the process's stderr (the log) goes to, as a UTF-8 path; 0 when stderr is not a file (a console, a pipe).
int plat_stderr_path(char *out, size_t cap);
// Writes the bytes to stderr's file descriptor, past the C runtime's FILE stream (on Windows with its low-level _write,
// which keeps the text mode's line ends): a write that blocks, on a console that holds its output back, then holds no
// lock of the stderr stream, and fflush(stderr) or fflush(NULL) on another thread does not wait for it. 1 when all of
// them were written.
int plat_stderr_write(const void *p, size_t n);
// When stderr is a console window (Windows): turns its QuickEdit mode off, since a selection made in the window with
// the mouse holds back every write to it until the selection ends, and passes a close of the console, Ctrl+C and
// Ctrl+Break to on_close, on a thread of the system's, with the event's name. on_close must end the process: Windows
// ends it about 5 s after a close in any case. A second Ctrl+C or Ctrl+Break while on_close runs ends the process at
// once, as without the handler. plat_exit puts QuickEdit back as it was. 1 when stderr is a console; elsewhere 0, and
// nothing is changed.
int plat_console_setup(void (*on_close)(const char *what));
// ---- desktop: the in-game menu's "Copy system info" and "Open log folder"
int plat_clipboard_set(const char *utf8); // replaces the clipboard's text; 1 on success

View File

@@ -1197,6 +1197,52 @@ __attribute__((constructor)) static void pgo_flush_start(void) {
}
#endif
// ---- the console window (plat_console_setup)
static HANDLE con_in = INVALID_HANDLE_VALUE; // the console's input, while its QuickEdit mode is off; con_mode as it was
static DWORD con_mode;
static void (*con_close)(const char *what);
static volatile LONG con_closing;
// Runs on a thread the system starts for the event. A close returns only when on_close does, which it must not.
static BOOL WINAPI con_ctrl(DWORD ev) {
const char *what = ev == CTRL_CLOSE_EVENT ? "closed" : ev == CTRL_C_EVENT ? "Ctrl+C"
: ev == CTRL_BREAK_EVENT ? "Ctrl+Break" : NULL;
// log-off and shutdown reach a process with a window as WM_ENDSESSION (gfx_window_win.c), not here; a second event
// while on_close runs takes the default path, which ends the process at once
if (!what || !con_close || InterlockedExchange(&con_closing, 1)) return FALSE;
con_close(what);
return TRUE;
}
int plat_console_setup(void (*on_close)(const char *what)) {
HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
DWORD m;
if (!err || err == INVALID_HANDLE_VALUE || !GetConsoleMode(err, &m)) return 0;
// CONIN$: the console's input even when stdin was redirected. ENABLE_EXTENDED_FLAGS makes the call change the
// QuickEdit bit at all.
HANDLE in = CreateFileW(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if (in != INVALID_HANDLE_VALUE && GetConsoleMode(in, &con_mode) && (con_mode & ENABLE_QUICK_EDIT_MODE) &&
SetConsoleMode(in, (con_mode & ~(DWORD)ENABLE_QUICK_EDIT_MODE) | ENABLE_EXTENDED_FLAGS))
con_in = in;
else if (in != INVALID_HANDLE_VALUE)
CloseHandle(in);
con_close = on_close;
if (on_close) SetConsoleCtrlHandler(con_ctrl, TRUE);
return 1;
}
int plat_stderr_write(const void *p, size_t n) {
int fd = _fileno(stderr); // the descriptor fwrite(stderr) writes to; negative when the process has no stderr
if (fd < 0) return 0;
for (const char *c = p; n;) {
int k = _write(fd, c, n > (1u << 30) ? 1u << 30 : (unsigned)n);
if (k <= 0) return 0;
c += k, n -= (size_t)k;
}
return 1;
}
void (*plat_exit_hook)(void);
void plat_exit(int code) {
@@ -1204,6 +1250,8 @@ void plat_exit(int code) {
#ifdef NX2NAVI_PGO_GEN
__llvm_profile_dump();
#endif
// QuickEdit back on in a console that outlives the process (the one of the shell that started it)
if (con_in != INVALID_HANDLE_VALUE) SetConsoleMode(con_in, con_mode | ENABLE_EXTENDED_FLAGS);
fflush(NULL); // every open stream: nothing below runs the C runtime's own exit
// The process ends here, without the libraries' detach calls that ExitProcess makes. The callers have saved what
// is to be kept (gfx_present.c gfx_window_closed: settings, pipeline cache, profile, saves), and the game's other