From 4f4fcb224e158575f881f53afe3f495afbd45b62 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Sun, 6 Mar 2022 12:31:29 -0800 Subject: [PATCH] [macos] Register log handler (flutter/engine#31861) Previously, the macOS embedder relied on the deprecated built-in log handling in lib/ui/ui_dart_state.cc. Instead, we now register a dedicated log handler for the macOS embedder. This is used, for example, to output Dart 'print' calls. While unlikely that we'll break the internal generic fallback logging, it's preferable to keep platform-specific functionality in the embedder. Issue: https://github.com/flutter/flutter/issues/99636 --- .../macos/framework/Source/FlutterEngine.mm | 8 ++++++ .../framework/Source/FlutterEngineTest.mm | 25 +++++++++++++++++++ .../Source/fixtures/flutter_desktop_test.dart | 13 +++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 33b922804d..5a2d81c03f 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -6,6 +6,7 @@ #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h" #include +#include #include #import "flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.h" @@ -298,6 +299,13 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi flutterArguments.dart_entrypoint_argc = dartEntrypointArgs.size(); flutterArguments.dart_entrypoint_argv = dartEntrypointArgs.data(); flutterArguments.root_isolate_create_callback = _project.rootIsolateCreateCallback; + flutterArguments.log_message_callback = [](const char* tag, const char* message, + void* user_data) { + if (tag && tag[0]) { + std::cout << tag << ": "; + } + std::cout << message << std::endl; + }; static size_t sTaskRunnerIdentifiers = 0; const FlutterTaskRunnerDescription cocoa_task_runner_description = { diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm index 4341f89fb1..dd95e33d32 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm @@ -58,6 +58,31 @@ TEST_F(FlutterEngineTest, MessengerSend) { EXPECT_TRUE(called); } +TEST_F(FlutterEngineTest, CanLogToStdout) { + // Replace stdout stream buffer with our own. + std::stringstream buffer; + std::streambuf* old_buffer = std::cout.rdbuf(); + std::cout.rdbuf(buffer.rdbuf()); + + // Launch the test entrypoint. + FlutterEngine* engine = GetFlutterEngine(); + EXPECT_TRUE([engine runWithEntrypoint:@"canLogToStdout"]); + EXPECT_TRUE(engine.running); + + // Block until completion of print statement. + fml::AutoResetWaitableEvent latch; + AddNativeCallback("SignalNativeTest", + CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { latch.Signal(); })); + latch.Wait(); + + // Restore old stdout stream buffer. + std::cout.rdbuf(old_buffer); + + // Verify hello world was written to stdout. + std::string logs = buffer.str(); + EXPECT_TRUE(logs.find("Hello logging") != std::string::npos); +} + TEST_F(FlutterEngineTest, CanToggleAccessibility) { FlutterEngine* engine = GetFlutterEngine(); // Capture the update callbacks before the embedder API initializes. diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart index c7d3cdbc08..1caf434606 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart @@ -3,8 +3,17 @@ // found in the LICENSE file. import 'dart:ui'; -void main() { +void signalNativeTest() native 'SignalNativeTest'; + +void main() { +} + +@pragma('vm:entry-point') +void canLogToStdout() { + // Emit hello world message to output then signal the test. + print('Hello logging'); + signalNativeTest(); } Picture CreateSimplePicture() { @@ -29,8 +38,6 @@ void can_composite_platform_views() { PlatformDispatcher.instance.scheduleFrame(); } -void signalNativeTest() native 'SignalNativeTest'; - @pragma('vm:entry-point') void native_callback() { signalNativeTest();