[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
This commit is contained in:
Chris Bracken
2022-03-06 12:31:29 -08:00
committed by GitHub
parent 3484dc171b
commit 4f4fcb224e
3 changed files with 43 additions and 3 deletions

View File

@@ -6,6 +6,7 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
#include <algorithm>
#include <iostream>
#include <vector>
#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 = {

View File

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

View File

@@ -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();