Skip to content

dispatch: fix logging to a file on Windows #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,15 +733,19 @@ _dispatch_logv_init(void *context DISPATCH_UNUSED)
dispatch_log_basetime = _dispatch_absolute_time();
#endif
#if defined(_WIN32)
FILE *pLogFile = _fdopen(dispatch_logfile, "w");

char szProgramName[MAX_PATH + 1] = {0};
GetModuleFileNameA(NULL, szProgramName, MAX_PATH);

fprintf(pLogFile, "=== log file opened for %s[%lu] at "
"%ld.%06u ===\n", szProgramName, GetCurrentProcessId(),
tv.tv_sec, (int)tv.tv_usec);
fclose(pLogFile);
char szMessage[512];
int len = snprintf(szMessage, sizeof(szMessage),
"=== log file opened for %s[%lu] at %ld.%06u ===",
szProgramName, GetCurrentProcessId(), tv.tv_sec,
(int)tv.tv_usec);
if (len > 0) {
len = MIN(len, sizeof(szMessage) - 1);
_write(dispatch_logfile, szMessage, len);
_write(dispatch_logfile, "\n", 1);
}
#else
dprintf(dispatch_logfile, "=== log file opened for %s[%u] at "
"%ld.%06u ===\n", getprogname() ?: "", getpid(),
Expand Down
4 changes: 4 additions & 0 deletions tests/dispatch_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
int
main(void)
{
#if defined(_WIN32)
_putenv_s("LIBDISPATCH_LOG", "stderr");
#else
setenv("LIBDISPATCH_LOG", "stderr", 1); // rdar://problem/8493990
#endif
dispatch_test_start("Dispatch Debug");

dispatch_queue_t main_q = dispatch_get_main_queue();
Expand Down