diff --git a/src/native/clr/host/os-bridge.cc b/src/native/clr/host/os-bridge.cc index 797316a9f7d..0037a7549f5 100644 --- a/src/native/clr/host/os-bridge.cc +++ b/src/native/clr/host/os-bridge.cc @@ -9,6 +9,18 @@ using namespace xamarin::android; +namespace { + constexpr auto should_log_message_to_logcat (bool file_logging_enabled, bool logcat_enabled) noexcept -> bool + { + return !file_logging_enabled || logcat_enabled; + } + + static_assert (should_log_message_to_logcat (false, false)); + static_assert (should_log_message_to_logcat (false, true)); + static_assert (!should_log_message_to_logcat (true, false)); + static_assert (should_log_message_to_logcat (true, true)); +} + void OSBridge::initialize_on_onload (JavaVM *vm, JNIEnv *env) noexcept { abort_if_invalid_pointer_argument (env, "env"); @@ -164,9 +176,11 @@ void OSBridge::_monodroid_gref_logf (const char *format, ...) noexcept [[gnu::always_inline, gnu::flatten]] void OSBridge::log_it (LogCategories category, std::string_view const& line, FILE *to, const char *const from, bool logcat_enabled) noexcept { - log_writef (category, LogLevel::Info, "%.*s", static_cast(line.length ()), line.data ()); + if (should_log_message_to_logcat (to != nullptr, logcat_enabled)) { + log_writef (category, LogLevel::Info, "%.*s", static_cast(line.length ()), line.data ()); + } - // We skip logcat here when logging to file is enabled because _write_stack_trace will output to logcat as well, if enabled + // Without a file, only full reference logging (gref+/lref+) writes the stack trace to logcat. if (to == nullptr) { if (logcat_enabled) { _write_stack_trace (nullptr, from, category);