浏览代码

On Windows, when stdout/stderr has been redirected, use fprintf instead of WriteConsoleW() to allow capturing log output from tools. Closes #703.

Lasse Öörni 10 年之前
父节点
当前提交
ff8f4b7525
共有 1 个文件被更改,包括 14 次插入6 次删除
  1. 14 6
      Source/Urho3D/Core/ProcessUtils.cpp

+ 14 - 6
Source/Urho3D/Core/ProcessUtils.cpp

@@ -162,12 +162,20 @@ void PrintUnicode(const String& str, bool error)
 {
     #if !defined(ANDROID) && !defined(IOS)
     #ifdef WIN32
-    HANDLE stream = GetStdHandle(error ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
-    if (stream == INVALID_HANDLE_VALUE)
-        return;
-    WString strW(str);
-    DWORD charsWritten;
-    WriteConsoleW(stream, strW.CString(), strW.Length(), &charsWritten, 0);
+    // If the output stream has been redirected, use fprintf instead of WriteConsoleW,
+    // though it means that proper Unicode output will not work
+    FILE* out = error ? stderr : stdout;
+    if (!_isatty(_fileno(out)))
+        fprintf(out, "%s", str.CString());
+    else
+    {
+        HANDLE stream = GetStdHandle(error ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
+        if (stream == INVALID_HANDLE_VALUE)
+            return;
+        WString strW(str);
+        DWORD charsWritten;
+        WriteConsoleW(stream, strW.CString(), strW.Length(), &charsWritten, 0);
+    }
     #else
     fprintf(error ? stderr : stdout, "%s", str.CString());
     #endif