From 60279904609a8efc729cc66655adcfba513002ae Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Fri, 1 May 2020 08:38:42 -0700 Subject: [PATCH] Add an API for resyncing output streams (flutter/engine#18070) When creating a console on Windows, stdout/stderr aren't wired up to it. They need to be re-opened afeter the console is created, and that needs to be done separately in the engine due to the use of static runtime linking. This provides a helper method that the runner can call when creating a console so that output will work as expected. Part of https://github.com/flutter/flutter/issues/53169 --- .../shell/platform/windows/flutter_windows.cc | 12 ++++++++++++ .../shell/platform/windows/public/flutter_windows.h | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows.cc b/engine/src/flutter/shell/platform/windows/flutter_windows.cc index c53b055fffe..805ca6cd31a 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows.cc @@ -5,6 +5,7 @@ #include "flutter/shell/platform/windows/public/flutter_windows.h" #include +#include #include #include @@ -224,6 +225,17 @@ UINT FlutterDesktopGetDpiForMonitor(HMONITOR monitor) { return flutter::GetDpiForMonitor(monitor); } +void FlutterDesktopResyncOutputStreams() { + FILE* unused; + if (freopen_s(&unused, "CONOUT$", "w", stdout)) { + _dup2(_fileno(stdout), 1); + } + if (freopen_s(&unused, "CONOUT$", "w", stderr)) { + _dup2(_fileno(stdout), 2); + } + std::ios::sync_with_stdio(); +} + FlutterDesktopEngineRef FlutterDesktopRunEngine( const FlutterDesktopEngineProperties& engine_properties) { auto engine = RunFlutterEngine(nullptr, engine_properties); diff --git a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h index 7d9c4155ad3..633169a4cf4 100644 --- a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h +++ b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h @@ -110,6 +110,11 @@ FLUTTER_EXPORT UINT FlutterDesktopGetDpiForHWND(HWND hwnd); // DPI of 96 is returned. FLUTTER_EXPORT UINT FlutterDesktopGetDpiForMonitor(HMONITOR monitor); +// Reopens stdout and stderr and resysncs the standard library output streams. +// Should be called if output is being directed somewhere in the runner process +// (e.g., after an AllocConsole call). +FLUTTER_EXPORT void FlutterDesktopResyncOutputStreams(); + // Runs an instance of a headless Flutter engine. // // Returns a null pointer in the event of an error.