From 739021e4a8366a2fa782447c3c5501b9ea292470 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Thu, 2 May 2024 15:36:20 -0400 Subject: [PATCH] chrome: Support CefSettings logging config (see #3685) Also enables logging by default for Release builds (previously required the `--enable-logging` flag). Logging can still be disabled by passing the `--disable-logging` flag. --- .../chrome_content_browser_client_cef.cc | 2 + .../common/chrome/chrome_main_delegate_cef.cc | 154 ++++++++++++++++++ .../common/chrome/chrome_main_delegate_cef.h | 1 + patch/patch.cfg | 5 + patch/patches/chrome_common_logging.patch | 35 ++++ 5 files changed, 197 insertions(+) create mode 100644 patch/patches/chrome_common_logging.patch diff --git a/libcef/browser/chrome/chrome_content_browser_client_cef.cc b/libcef/browser/chrome/chrome_content_browser_client_cef.cc index fa807f2d2..c5d2140bd 100644 --- a/libcef/browser/chrome/chrome_content_browser_client_cef.cc +++ b/libcef/browser/chrome/chrome_content_browser_client_cef.cc @@ -123,6 +123,8 @@ void ChromeContentBrowserClientCef::AppendExtraCommandLineSwitches( switches::kMainBundlePath, #endif switches::kLocalesDirPath, + switches::kLogItems, + switches::kLogSeverity, switches::kResourcesDirPath, switches::kUserAgentProductAndVersion, }; diff --git a/libcef/common/chrome/chrome_main_delegate_cef.cc b/libcef/common/chrome/chrome_main_delegate_cef.cc index 26249675f..0575c57ba 100644 --- a/libcef/common/chrome/chrome_main_delegate_cef.cc +++ b/libcef/common/chrome/chrome_main_delegate_cef.cc @@ -43,6 +43,76 @@ namespace { base::LazyInstance::DestructorAtExit g_chrome_content_renderer_client = LAZY_INSTANCE_INITIALIZER; +void InitLogging(const base::CommandLine* command_line) { + logging::LogSeverity log_severity = logging::LOGGING_INFO; + + std::string log_severity_str = + command_line->GetSwitchValueASCII(switches::kLogSeverity); + if (!log_severity_str.empty()) { + if (base::EqualsCaseInsensitiveASCII(log_severity_str, + switches::kLogSeverity_Verbose)) { + log_severity = logging::LOGGING_VERBOSE; + } else if (base::EqualsCaseInsensitiveASCII( + log_severity_str, switches::kLogSeverity_Warning)) { + log_severity = logging::LOGGING_WARNING; + } else if (base::EqualsCaseInsensitiveASCII(log_severity_str, + switches::kLogSeverity_Error)) { + log_severity = logging::LOGGING_ERROR; + } else if (base::EqualsCaseInsensitiveASCII(log_severity_str, + switches::kLogSeverity_Fatal)) { + log_severity = logging::LOGGING_FATAL; + } else if (base::EqualsCaseInsensitiveASCII( + log_severity_str, switches::kLogSeverity_Disable)) { + log_severity = LOGSEVERITY_DISABLE; + } + } + + if (log_severity == LOGSEVERITY_DISABLE) { + // By default, ERROR and FATAL messages will always be output to stderr due + // to the kAlwaysPrintErrorLevel value in base/logging.cc. We change the log + // level here so that only FATAL messages are output. + logging::SetMinLogLevel(logging::LOGGING_FATAL); + } else { + logging::SetMinLogLevel(log_severity); + } + + // Customization of items automatically prepended to log lines. + std::string log_items_str = + command_line->GetSwitchValueASCII(switches::kLogItems); + if (!log_items_str.empty()) { + bool enable_log_of_process_id, enable_log_of_thread_id, + enable_log_of_time_stamp, enable_log_of_tick_count; + enable_log_of_process_id = enable_log_of_thread_id = + enable_log_of_time_stamp = enable_log_of_tick_count = false; + + for (const auto& cur_item_to_log : + base::SplitStringPiece(log_items_str, ",", base::TRIM_WHITESPACE, + base::SPLIT_WANT_NONEMPTY)) { + // if "none" mode is present, all items are disabled. + if (base::EqualsCaseInsensitiveASCII(cur_item_to_log, + switches::kLogItems_None)) { + enable_log_of_process_id = enable_log_of_thread_id = + enable_log_of_time_stamp = enable_log_of_tick_count = false; + break; + } else if (base::EqualsCaseInsensitiveASCII(cur_item_to_log, + switches::kLogItems_PId)) { + enable_log_of_process_id = true; + } else if (base::EqualsCaseInsensitiveASCII(cur_item_to_log, + switches::kLogItems_TId)) { + enable_log_of_thread_id = true; + } else if (base::EqualsCaseInsensitiveASCII( + cur_item_to_log, switches::kLogItems_TimeStamp)) { + enable_log_of_time_stamp = true; + } else if (base::EqualsCaseInsensitiveASCII( + cur_item_to_log, switches::kLogItems_TickCount)) { + enable_log_of_tick_count = true; + } + } + logging::SetLogItems(enable_log_of_process_id, enable_log_of_thread_id, + enable_log_of_time_stamp, enable_log_of_tick_count); + } +} + } // namespace ChromeMainDelegateCef::ChromeMainDelegateCef(CefMainRunnerHandler* runner, @@ -144,6 +214,70 @@ std::optional ChromeMainDelegateCef::BasicStartupComplete() { command_line->AppendSwitchASCII(switches::kLang, "en-US"); } + if (!command_line->HasSwitch(switches::kLogFile) && + settings_->log_file.length > 0) { + auto log_file = base::FilePath(CefString(&settings_->log_file)); + command_line->AppendSwitchPath(switches::kLogFile, log_file); + } + + if (!command_line->HasSwitch(switches::kLogSeverity) && + settings_->log_severity != LOGSEVERITY_DEFAULT) { + std::string log_severity; + switch (settings_->log_severity) { + case LOGSEVERITY_VERBOSE: + log_severity = switches::kLogSeverity_Verbose; + break; + case LOGSEVERITY_INFO: + log_severity = switches::kLogSeverity_Info; + break; + case LOGSEVERITY_WARNING: + log_severity = switches::kLogSeverity_Warning; + break; + case LOGSEVERITY_ERROR: + log_severity = switches::kLogSeverity_Error; + break; + case LOGSEVERITY_FATAL: + log_severity = switches::kLogSeverity_Fatal; + break; + case LOGSEVERITY_DISABLE: + log_severity = switches::kLogSeverity_Disable; + break; + default: + break; + } + if (!log_severity.empty()) { + command_line->AppendSwitchASCII(switches::kLogSeverity, log_severity); + } + } + + if (!command_line->HasSwitch(switches::kLogItems) && + settings_->log_items != LOG_ITEMS_DEFAULT) { + std::string log_items_str; + if (settings_->log_items == LOG_ITEMS_NONE) { + log_items_str = std::string(switches::kLogItems_None); + } else { + std::vector added_items; + if (settings_->log_items & LOG_ITEMS_FLAG_PROCESS_ID) { + added_items.emplace_back(switches::kLogItems_PId); + } + if (settings_->log_items & LOG_ITEMS_FLAG_THREAD_ID) { + added_items.emplace_back(switches::kLogItems_TId); + } + if (settings_->log_items & LOG_ITEMS_FLAG_TIME_STAMP) { + added_items.emplace_back(switches::kLogItems_TimeStamp); + } + if (settings_->log_items & LOG_ITEMS_FLAG_TICK_COUNT) { + added_items.emplace_back(switches::kLogItems_TickCount); + } + if (!added_items.empty()) { + log_items_str = base::JoinString(added_items, ","); + } + } + if (!log_items_str.empty()) { + command_line->AppendSwitchASCII(switches::kLogItems, log_items_str); + } + } + if (settings_->javascript_flags.length > 0) { command_line->AppendSwitchASCII( blink::switches::kJavaScriptFlags, @@ -213,6 +347,9 @@ std::optional ChromeMainDelegateCef::BasicStartupComplete() { std::ignore = commandLinePtr->Detach(nullptr); } + // Call as early as possible. + InitLogging(command_line); + #if BUILDFLAG(IS_MAC) util_mac::BasicStartupComplete(); #endif @@ -265,6 +402,23 @@ void ChromeMainDelegateCef::PreSandboxStartup() { base::PathService::Override(ui::DIR_LOCALES, locales_dir); } } + +#if !BUILDFLAG(IS_WIN) + // Call after InitLogging() potentially changes values in + // chrome/app/chrome_main_delegate.cc. + InitLogging(command_line); +#endif +} + +void ChromeMainDelegateCef::SandboxInitialized( + const std::string& process_type) { + ChromeMainDelegate::SandboxInitialized(process_type); + +#if BUILDFLAG(IS_WIN) + // Call after InitLogging() potentially changes values in + // chrome/app/chrome_main_delegate.cc. + InitLogging(base::CommandLine::ForCurrentProcess()); +#endif } std::optional ChromeMainDelegateCef::PreBrowserMain() { diff --git a/libcef/common/chrome/chrome_main_delegate_cef.h b/libcef/common/chrome/chrome_main_delegate_cef.h index e97a42f54..323d6a8f5 100644 --- a/libcef/common/chrome/chrome_main_delegate_cef.h +++ b/libcef/common/chrome/chrome_main_delegate_cef.h @@ -38,6 +38,7 @@ class ChromeMainDelegateCef : public ChromeMainDelegate, // ChromeMainDelegate overrides. std::optional BasicStartupComplete() override; void PreSandboxStartup() override; + void SandboxInitialized(const std::string& process_type) override; std::optional PreBrowserMain() override; std::optional PostEarlyInitialization(InvokedIn invoked_in) override; absl::variant RunProcess( diff --git a/patch/patch.cfg b/patch/patch.cfg index 3cdd03226..31c111342 100644 --- a/patch/patch.cfg +++ b/patch/patch.cfg @@ -348,6 +348,11 @@ patches = [ # https://github.com/chromiumembedded/cef/issues/2969 'name': 'chrome_browser_profile_menu', }, + { + # chrome: Enable logging by default for Release builds. + # https://github.com/chromiumembedded/cef/issues/2969 + 'name': 'chrome_common_logging', + }, { # alloy: Don't require heap profiler for utility processes. # Avoids a DCHECK added in https://crrev.com/c21e9f71d1f2e diff --git a/patch/patches/chrome_common_logging.patch b/patch/patches/chrome_common_logging.patch new file mode 100644 index 000000000..a668a2aa8 --- /dev/null +++ b/patch/patches/chrome_common_logging.patch @@ -0,0 +1,35 @@ +diff --git chrome/common/features.gni chrome/common/features.gni +index 01367ff6efd6c..975ea8e446dce 100644 +--- chrome/common/features.gni ++++ chrome/common/features.gni +@@ -7,6 +7,7 @@ import("//build/config/chromeos/ui_mode.gni") + import("//build/config/compiler/compiler.gni") + import("//build/config/dcheck_always_on.gni") + import("//build/config/features.gni") ++import("//cef/libcef/features/features.gni") + import("//components/compose/features.gni") + import("//components/feed/features.gni") + import("//components/nacl/features.gni") +@@ -31,7 +32,7 @@ assert(use_blink, "Chromium without blink shouldn't use anything in //chrome") + declare_args() { + # Enables the build to have logging enabled by default. + # This is intended for use only in developer builds. +- chrome_enable_logging_by_default = is_debug ++ chrome_enable_logging_by_default = is_debug || enable_cef + + # Platforms where Chrome x509 server certificate enterprise policies are + # supported. This must must match the supported_on/future_on list of the +@@ -90,11 +91,13 @@ declare_args() { + # optimize_webui was moved to ui/base/ui_features.gni + } + ++if (!enable_cef) { + # Logging must be disabled by default in all official builds (including special + # DCHECK-enabled builds). Logging is enabled by default for debug builds, and + # may be selectively enabled by default for release builds. + assert(!chrome_enable_logging_by_default || !is_official_build, + "Logging must be disabled by default in Official builds") ++} + + # Use brlapi from brltty for braille display support. + use_brlapi = is_chromeos_ash