Skip to content
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

Redirect stdout/stderr in Windows template #56089

Merged
merged 1 commit into from May 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -27,6 +27,9 @@
<ClCompile Include="runner\flutter_window.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="runner\utils.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="runner\win32_window.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand All @@ -53,6 +56,9 @@
<ClInclude Include="runner\flutter_window.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="runner\utils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="runner\win32_window.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
Expand Up @@ -227,6 +227,7 @@
<ClCompile Include="runner\main.cpp" />
<ClCompile Include="flutter\generated_plugin_registrant.cc" />
<ClCompile Include="runner\run_loop.cpp" />
<ClCompile Include="runner\utils.cpp" />
<ClCompile Include="runner\window_configuration.cpp" />
<ClCompile Include="runner\win32_window.cpp" />
<ClCompile Include="runner\flutter_window.cpp" />
Expand All @@ -238,6 +239,7 @@
<ClInclude Include="flutter\generated_plugin_registrant.h" />
<ClInclude Include="runner\resource.h" />
<ClInclude Include="runner\run_loop.h" />
<ClInclude Include="runner\utils.h" />
<ClInclude Include="runner\win32_window.h" />
<ClInclude Include="runner\flutter_window.h" />
<ClInclude Include="runner\window_configuration.h" />
Expand Down
Expand Up @@ -4,19 +4,19 @@

#include "flutter_window.h"
#include "run_loop.h"
#include "utils.h"
#include "window_configuration.h"

int APIENTRY wWinMain(_In_ HINSTANCE instance,
_In_opt_ HINSTANCE prev,
_In_ wchar_t* command_line,
_In_ int show_command) {
int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
_In_ wchar_t *command_line, _In_ int show_command) {
// Attach to console when present (e.g., 'flutter run') or create a
// new console when running with a debugger.
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
::AllocConsole();
CreateAndAttachConsole();
}

// Initialize COM, so that it is available for use in the library and/or plugins.
// Initialize COM, so that it is available for use in the library and/or
// plugins.
::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);

RunLoop run_loop;
Expand Down
22 changes: 22 additions & 0 deletions packages/flutter_tools/templates/app/windows.tmpl/runner/utils.cpp
@@ -0,0 +1,22 @@
#include "utils.h"

#include <flutter_windows.h>
#include <io.h>
#include <stdio.h>
#include <windows.h>

#include <iostream>

void CreateAndAttachConsole() {
if (::AllocConsole()) {
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();
FlutterDesktopResyncOutputStreams();
}
}
@@ -0,0 +1,8 @@
#ifndef CONSOLE_UTILS_H_
#define CONSOLE_UTILS_H_

// Creates a console for the process, and redirects stdout and stderr to
// it for both the runner and the Flutter library.
void CreateAndAttachConsole();

#endif // CONSOLE_UTILS_H_