From 0bfe5e62ff36739b46bf0d1a6f5b8754640c7537 Mon Sep 17 00:00:00 2001 From: "Hong Jen Yee (PCMan)" Date: Sun, 18 Nov 2018 22:13:35 +0800 Subject: [PATCH] For Windows, make sure the file handle created by os::fopen_s() is never inherited by child processes launched using CreateProcess(). Otherwise, file rotation will fail since the old log file is still opened by the child process and we cannot rename it. --- include/spdlog/details/os.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 5e11da60..655c28fc 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -153,6 +153,13 @@ inline bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mod #else *fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); #endif + + // Make sure the file handle is never inherited by child processes created using CreateProcess(). + // Otherwise, file rotation will fail since the file is left open by the child process. + auto fn = _fileno(*fp); + auto handle = HANDLE(_get_osfhandle(fn)); + ::SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0); + #else // unix *fp = fopen((filename.c_str()), mode.c_str()); #endif