diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index d03302dd6b5fb..b1f673ca1a8cc 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -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; diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index 39d817bacc793..97e14e944f9ff 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -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 diff --git a/shell/platform/embedder/tests/embedder_config_builder.cc b/shell/platform/embedder/tests/embedder_config_builder.cc index c8f5224c29771..1b031ca1484ea 100644 --- a/shell/platform/embedder/tests/embedder_config_builder.cc +++ b/shell/platform/embedder/tests/embedder_config_builder.cc @@ -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(); diff --git a/shell/platform/embedder/tests/embedder_config_builder.h b/shell/platform/embedder/tests/embedder_config_builder.h index 52aa44932a7c1..40f9bfc85a560 100644 --- a/shell/platform/embedder/tests/embedder_config_builder.h +++ b/shell/platform/embedder/tests/embedder_config_builder.h @@ -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 command_line_arguments_; std::vector dart_entrypoint_arguments_; + std::string log_tag_; UniqueEngine SetupEngine(bool run) const; diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index f418ab8f3684d..293aab15c6eef 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -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).