Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f09b189

Browse files
authored
Allow embedders to set a custom log tag (#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 flutter/flutter#79819
1 parent 61dca0f commit f09b189

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

shell/platform/embedder/embedder.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,9 @@ FlutterEngineResult FlutterEngineInitialize(size_t version,
962962
callback(tag.c_str(), message.c_str(), user_data);
963963
};
964964
}
965+
if (SAFE_ACCESS(args, log_tag, nullptr) != nullptr) {
966+
settings.log_tag = SAFE_ACCESS(args, log_tag, nullptr);
967+
}
965968

966969
flutter::PlatformViewEmbedder::UpdateSemanticsNodesCallback
967970
update_semantics_nodes_callback = nullptr;

shell/platform/embedder/embedder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,13 @@ typedef struct {
15131513
// thread and embedders must re-thread if necessary. Performing blocking calls
15141514
// in this callback may introduce application jank.
15151515
FlutterLogMessageCallback log_message_callback;
1516+
1517+
// A tag string associated with application log messages.
1518+
//
1519+
// A log message tag string that can be used convey application, subsystem,
1520+
// or component name to embedder's logger. This string will be passed to to
1521+
// callbacks on `log_message_callback`. Defaults to "flutter" if unspecified.
1522+
const char* log_tag;
15161523
} FlutterProjectArgs;
15171524

15181525
#ifndef FLUTTER_ENGINE_NO_PROTOTYPES

shell/platform/embedder/tests/embedder_config_builder.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ void EmbedderConfigBuilder::SetLogMessageCallbackHook() {
217217
EmbedderTestContext::GetLogMessageCallbackHook();
218218
}
219219

220+
void EmbedderConfigBuilder::SetLogTag(std::string tag) {
221+
log_tag_ = std::move(tag);
222+
project_args_.log_tag = log_tag_.c_str();
223+
}
224+
220225
void EmbedderConfigBuilder::SetLocalizationCallbackHooks() {
221226
project_args_.compute_platform_resolved_locale_callback =
222227
EmbedderTestContext::GetComputePlatformResolvedLocaleCallbackHook();

shell/platform/embedder/tests/embedder_config_builder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ class EmbedderConfigBuilder {
7272

7373
void SetSemanticsCallbackHooks();
7474

75+
// Used to set a custom log message handler.
7576
void SetLogMessageCallbackHook();
7677

78+
// Used to set a custom log tag.
79+
void SetLogTag(std::string tag);
80+
7781
void SetLocalizationCallbackHooks();
7882

7983
void SetDartEntrypoint(std::string entrypoint);
@@ -117,6 +121,7 @@ class EmbedderConfigBuilder {
117121
FlutterCompositor compositor_ = {};
118122
std::vector<std::string> command_line_arguments_;
119123
std::vector<std::string> dart_entrypoint_arguments_;
124+
std::string log_tag_;
120125

121126
UniqueEngine SetupEngine(bool run) const;
122127

shell/platform/embedder/tests/embedder_unittests.cc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ TEST_F(EmbedderTest, InvalidPlatformMessages) {
453453
}
454454

455455
//------------------------------------------------------------------------------
456-
/// Tests that setting a custom log callback works as expected.
456+
/// Tests that setting a custom log callback works as expected and defaults to
457+
/// using tag "flutter".
457458
TEST_F(EmbedderTest, CanSetCustomLogMessageCallback) {
458459
fml::AutoResetWaitableEvent callback_latch;
459460
auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
@@ -471,6 +472,26 @@ TEST_F(EmbedderTest, CanSetCustomLogMessageCallback) {
471472
callback_latch.Wait();
472473
}
473474

475+
//------------------------------------------------------------------------------
476+
/// Tests that setting a custom log tag works.
477+
TEST_F(EmbedderTest, CanSetCustomLogTag) {
478+
fml::AutoResetWaitableEvent callback_latch;
479+
auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
480+
EmbedderConfigBuilder builder(context);
481+
builder.SetDartEntrypoint("custom_logger");
482+
builder.SetSoftwareRendererConfig();
483+
builder.SetLogTag("butterfly");
484+
context.SetLogMessageCallback(
485+
[&callback_latch](const char* tag, const char* message) {
486+
EXPECT_EQ(std::string(tag), "butterfly");
487+
EXPECT_EQ(std::string(message), "hello world");
488+
callback_latch.Signal();
489+
});
490+
auto engine = builder.LaunchEngine();
491+
ASSERT_TRUE(engine.is_valid());
492+
callback_latch.Wait();
493+
}
494+
474495
//------------------------------------------------------------------------------
475496
/// Asserts behavior of FlutterProjectArgs::shutdown_dart_vm_when_done (which is
476497
/// set to true by default in these unit-tests).

0 commit comments

Comments
 (0)