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
This commit is contained in:
stuartmorgan 2020-05-01 08:38:42 -07:00 committed by GitHub
parent fd03b6c704
commit 6027990460
2 changed files with 17 additions and 0 deletions

View File

@ -5,6 +5,7 @@
#include "flutter/shell/platform/windows/public/flutter_windows.h"
#include <assert.h>
#include <io.h>
#include <algorithm>
#include <chrono>
@ -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);

View File

@ -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.