Allow embedders to set a custom log tag (flutter/engine#25435)

Embedders making use of the embedder API always ended up with a
hardcoded log tag of "flutter". Some embedders may wish to set a
different log tag. For example, the Fuchsia embedder sets their log tag
to a launch URL followed by "flutter".

If unset, we continue to default to "flutter".

Fixes https://github.com/flutter/flutter/issues/79819
This commit is contained in:
Chris Bracken
2021-04-05 17:35:54 -07:00
committed by GitHub
parent 1aca49f5f6
commit bc3084eabc
5 changed files with 42 additions and 1 deletions

View File

@@ -962,6 +962,9 @@ FlutterEngineResult FlutterEngineInitialize(size_t version,
callback(tag.c_str(), message.c_str(), user_data);
};
}
if (SAFE_ACCESS(args, log_tag, nullptr) != nullptr) {
settings.log_tag = SAFE_ACCESS(args, log_tag, nullptr);
}
flutter::PlatformViewEmbedder::UpdateSemanticsNodesCallback
update_semantics_nodes_callback = nullptr;

View File

@@ -1513,6 +1513,13 @@ typedef struct {
// thread and embedders must re-thread if necessary. Performing blocking calls
// in this callback may introduce application jank.
FlutterLogMessageCallback log_message_callback;
// A tag string associated with application log messages.
//
// A log message tag string that can be used convey application, subsystem,
// or component name to embedder's logger. This string will be passed to to
// callbacks on `log_message_callback`. Defaults to "flutter" if unspecified.
const char* log_tag;
} FlutterProjectArgs;
#ifndef FLUTTER_ENGINE_NO_PROTOTYPES

View File

@@ -217,6 +217,11 @@ void EmbedderConfigBuilder::SetLogMessageCallbackHook() {
EmbedderTestContext::GetLogMessageCallbackHook();
}
void EmbedderConfigBuilder::SetLogTag(std::string tag) {
log_tag_ = std::move(tag);
project_args_.log_tag = log_tag_.c_str();
}
void EmbedderConfigBuilder::SetLocalizationCallbackHooks() {
project_args_.compute_platform_resolved_locale_callback =
EmbedderTestContext::GetComputePlatformResolvedLocaleCallbackHook();

View File

@@ -72,8 +72,12 @@ class EmbedderConfigBuilder {
void SetSemanticsCallbackHooks();
// Used to set a custom log message handler.
void SetLogMessageCallbackHook();
// Used to set a custom log tag.
void SetLogTag(std::string tag);
void SetLocalizationCallbackHooks();
void SetDartEntrypoint(std::string entrypoint);
@@ -117,6 +121,7 @@ class EmbedderConfigBuilder {
FlutterCompositor compositor_ = {};
std::vector<std::string> command_line_arguments_;
std::vector<std::string> dart_entrypoint_arguments_;
std::string log_tag_;
UniqueEngine SetupEngine(bool run) const;

View File

@@ -453,7 +453,8 @@ TEST_F(EmbedderTest, InvalidPlatformMessages) {
}
//------------------------------------------------------------------------------
/// Tests that setting a custom log callback works as expected.
/// Tests that setting a custom log callback works as expected and defaults to
/// using tag "flutter".
TEST_F(EmbedderTest, CanSetCustomLogMessageCallback) {
fml::AutoResetWaitableEvent callback_latch;
auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
@@ -471,6 +472,26 @@ TEST_F(EmbedderTest, CanSetCustomLogMessageCallback) {
callback_latch.Wait();
}
//------------------------------------------------------------------------------
/// Tests that setting a custom log tag works.
TEST_F(EmbedderTest, CanSetCustomLogTag) {
fml::AutoResetWaitableEvent callback_latch;
auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
EmbedderConfigBuilder builder(context);
builder.SetDartEntrypoint("custom_logger");
builder.SetSoftwareRendererConfig();
builder.SetLogTag("butterfly");
context.SetLogMessageCallback(
[&callback_latch](const char* tag, const char* message) {
EXPECT_EQ(std::string(tag), "butterfly");
EXPECT_EQ(std::string(message), "hello world");
callback_latch.Signal();
});
auto engine = builder.LaunchEngine();
ASSERT_TRUE(engine.is_valid());
callback_latch.Wait();
}
//------------------------------------------------------------------------------
/// Asserts behavior of FlutterProjectArgs::shutdown_dart_vm_when_done (which is
/// set to true by default in these unit-tests).