소스 검색

deploy-ng: fix redirecting GUI app output to log in VC2015/UCRT

Fixes #461
rdb 7 년 전
부모
커밋
2664a24e6a
1개의 변경된 파일19개의 추가작업 그리고 7개의 파일을 삭제
  1. 19 7
      pandatool/src/deploy-stub/deploy-stub.c

+ 19 - 7
pandatool/src/deploy-stub/deploy-stub.c

@@ -264,17 +264,29 @@ static int setup_logging(const char *path, int append) {
     SetFilePointer(handle, 0, NULL, FILE_END);
   }
 
-  fflush(stdout);
-  fflush(stderr);
-
-  int fd = _open_osfhandle((intptr_t)handle, _O_WRONLY | _O_TEXT | (append ? _O_APPEND : 0));
   SetStdHandle(STD_OUTPUT_HANDLE, handle);
-  _dup2(fd, 1);
-
   SetStdHandle(STD_ERROR_HANDLE, handle);
-  _dup2(fd, 2);
 
+  // If we are running under the UCRT in a GUI application, we can't be sure
+  // that we have valid fds for stdout and stderr, so we have to set them up.
+  // One way to do this is to reopen them to something silly (like NUL).
+  if (_fileno(stdout) < 0) {
+    _close(1);
+    _wfreopen(L"\\\\.\\NUL", L"w", stdout);
+  }
+
+  if (_fileno(stderr) < 0) {
+    _close(2);
+    _wfreopen(L"\\\\.\\NUL", L"w", stderr);
+  }
+
+  // Now replace the stdout and stderr file descriptors with one pointing to
+  // our desired handle.
+  int fd = _open_osfhandle((intptr_t)handle, _O_WRONLY | _O_TEXT | (append ? _O_APPEND : 0));
+  _dup2(fd, _fileno(stdout));
+  _dup2(fd, _fileno(stderr));
   _close(fd);
+
   return 1;
 #else
   // Does it start with a tilde?  Perform tilde expansion if so.