From fae1f35e08478ccfa434a246d6d46b6b29b2d970 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 17 Jan 2024 01:16:06 +0100 Subject: [PATCH] Allow customizing CEF log file and log level Add new wxWebViewConfigurationChromium class which corresponds to the "native" configuration used in the other backends and contains (some) fields of CefSettings in this one. --- include/wx/webview_chromium.h | 21 +++++++++ interface/wx/webview_chromium.h | 78 +++++++++++++++++++++++++++++++++ src/common/webview_chromium.cpp | 31 ++++++++++--- 3 files changed, 125 insertions(+), 5 deletions(-) diff --git a/include/wx/webview_chromium.h b/include/wx/webview_chromium.h index 6563ceac51..1a587f76e7 100644 --- a/include/wx/webview_chromium.h +++ b/include/wx/webview_chromium.h @@ -180,6 +180,27 @@ private: wxDECLARE_DYNAMIC_CLASS(wxWebViewChromium); }; + +// Chrome-specific configuration class: a pointer to this object can be +// retrieved using wxWebViewConfiguration::GetNativeConfiguration() and used to +// configure some CEF-specific initialization options. +// +// Note that, contrary to the name of the accessor, this class is not native in +// this backend, although it contains data found in native (i.e. defined in +// Chrome) structs. +class wxWebViewConfigurationChromium +{ +public: + wxWebViewConfigurationChromium() = default; + + // Log file location: by default debug.log under data path is used. + wxString m_logFile; + + // Logging level must be one of cef_log_severity_t values (0 means default). + int m_logLevel = 0; +}; + + class CefFrame; class CefProcessMessage; diff --git a/interface/wx/webview_chromium.h b/interface/wx/webview_chromium.h index da46fc7beb..09a2bdab14 100644 --- a/interface/wx/webview_chromium.h +++ b/interface/wx/webview_chromium.h @@ -273,6 +273,37 @@ class wxWebViewChromium : public wxWebView { public: + /** + Default constructor. + + Use Create() to actually create the web view object later. + */ + wxWebViewChromium(); + + /** + Constructor allowing to specify extra configuration parameters. + + You must use Create() to really initialize the object created with this + constructor. + + Chromium-specific configuration parameters can be specified by setting + wxWebViewConfigurationChromium fields before passing @a config to this + function, e.g. + + @code + wxWebViewConfiguration config = + wxWebView::NewConfiguration(wxWebViewBackendChromium); + + auto configChrome = + static_cast(config.GetNativeConfiguration()); + configChrome->m_logFile = "/my/custom/CEF/log/file/path.txt"; + + auto webview = new wxWebViewChromium(config); + webview->Create(this, wxID_ANY, url); + @endcode + */ + explicit wxWebViewChromium(const wxWebViewConfiguration& config); + /** wxWebViewChromium constructor, arguments as per wxWebView::New. */ @@ -305,6 +336,53 @@ public: }; +/** + Chromium-specific configuration parameters. + + This simple class contains parameters that can be passed to the function + initializing CEF when wxWebView is used for the first time. + + To use this struct you need to cast the value returned from + wxWebViewConfiguration::GetNativeConfiguration() to this type: + @code + wxWebViewConfiguration config = + wxWebView::NewConfiguration(wxWebViewBackendChromium); + + auto configChrome = + static_cast(config.GetNativeConfiguration()); + configChrome->m_logLevel = 99; // Disable all logging. + @endcode + + @since 3.3.0 + */ +class wxWebViewConfigurationChromium +{ +public: + /** + Default constructor initializes the object to default parameters. + */ + wxWebViewConfigurationChromium(); + + /** + CEF log file location. + + Should be an absolute path if specified. + + If this variable is empty, `debug.log` under the data directory is + used. + */ + wxString m_logFile; + + /** + Logging level to use for CEF. + + This must be one of `cef_log_severity_t` values. + + Default value 0 means to use default "INFO" log level. + */ + int m_logLevel = 0; +}; + /** Event class for events generated exclusively by wxWebViewChromium. diff --git a/src/common/webview_chromium.cpp b/src/common/webview_chromium.cpp index a7e52d23b3..5bb4b11721 100644 --- a/src/common/webview_chromium.cpp +++ b/src/common/webview_chromium.cpp @@ -253,7 +253,8 @@ void AppImplData::DoMessageLoopWork() // Chromium-specific configuration data // ---------------------------------------------------------------------------- -class wxWebViewConfigurationImplChromium : public wxWebViewConfigurationImpl +class wxWebViewConfigurationImplChromium : public wxWebViewConfigurationImpl, + public wxWebViewConfigurationChromium { public: static wxWebViewConfiguration MakeConfig() @@ -262,6 +263,14 @@ public: new wxWebViewConfigurationImplChromium); } + virtual void* GetNativeConfiguration() const + { + // Our "native" configuration is our own Chromium-specific class from + // which we inherit. + const wxWebViewConfigurationChromium* self = this; + return const_cast(self); + } + virtual void SetDataPath(const wxString& path) override { m_dataPath = path; @@ -1005,7 +1014,9 @@ bool wxWebViewChromium::InitCEF(const wxWebViewConfiguration& config) } #endif // !__WXOSX__ - wxFileName userDataPath(cefDataFolder.GetFullPath(), "UserData"); + const wxString cefPath = cefDataFolder.GetFullPath(); + + wxFileName userDataPath(cefPath, "UserData"); CefString(&settings.root_cache_path).FromWString(userDataPath.GetFullPath().ToStdWstring()); // Set up CEF for use inside another application, as is the case for us. @@ -1014,9 +1025,19 @@ bool wxWebViewChromium::InitCEF(const wxWebViewConfiguration& config) settings.no_sandbox = true; - wxFileName logFileName(cefDataFolder.GetFullPath(), "debug.log"); - settings.log_severity = LOGSEVERITY_INFO; - CefString(&settings.log_file).FromWString(logFileName.GetFullPath().ToStdWstring()); + // Configure logging. + const auto configChrome = + static_cast(config.GetNativeConfiguration()); + wxCHECK_MSG( configChrome, false, "Should have Chromium-specific config" ); + + settings.log_severity = static_cast(configChrome->m_logLevel); + wxString logFile = configChrome->m_logFile; + if ( logFile.empty() ) + { + logFile = wxFileName(cefPath, "debug.log").GetFullPath(); + } + + CefString(&settings.log_file).FromWString(logFile.ToStdWstring()); #ifdef __WXMSW__ CefMainArgs args(wxGetInstance());