Skip to content

Commit 1d1e2be

Browse files
adierkingrokhinip
authored andcommitted
dispatch: fix logging to a file on Windows
Port the dispatch_debug test to Windows and fix a bug that it found related to logfile initialization. _dispatch_logv_init()'s Windows implementation uses _fdopen() and fprintf() to write the log header. However, this gives ownership of the file to stdio, so fclose() closes the descriptor and causes future writes to trigger CRT assertion failures. Avoid this by using snprintf() and _write() instead. Signed-off-by: Kim Topley <ktopley@apple.com>
1 parent bdcfeb0 commit 1d1e2be

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/init.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,15 +1111,19 @@ _dispatch_logv_init(void *context DISPATCH_UNUSED)
11111111
dispatch_log_basetime = _dispatch_uptime();
11121112
#endif
11131113
#if defined(_WIN32)
1114-
FILE *pLogFile = _fdopen(dispatch_logfile, "w");
1115-
11161114
char szProgramName[MAX_PATH + 1] = {0};
11171115
GetModuleFileNameA(NULL, szProgramName, MAX_PATH);
11181116

1119-
fprintf(pLogFile, "=== log file opened for %s[%lu] at "
1120-
"%ld.%06u ===\n", szProgramName, GetCurrentProcessId(),
1121-
tv.tv_sec, (int)tv.tv_usec);
1122-
fclose(pLogFile);
1117+
char szMessage[512];
1118+
int len = snprintf(szMessage, sizeof(szMessage),
1119+
"=== log file opened for %s[%lu] at %ld.%06u ===",
1120+
szProgramName, GetCurrentProcessId(), tv.tv_sec,
1121+
(int)tv.tv_usec);
1122+
if (len > 0) {
1123+
len = MIN(len, sizeof(szMessage) - 1);
1124+
_write(dispatch_logfile, szMessage, len);
1125+
_write(dispatch_logfile, "\n", 1);
1126+
}
11231127
#else
11241128
dprintf(dispatch_logfile, "=== log file opened for %s[%u] at "
11251129
"%ld.%06u ===\n", getprogname() ?: "", getpid(),
File renamed without changes.

0 commit comments

Comments
 (0)