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.
This commit is contained in:
Vadim Zeitlin 2024-01-17 01:16:06 +01:00
parent c40cf9c81d
commit fae1f35e08
3 changed files with 125 additions and 5 deletions

View file

@ -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;

View file

@ -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<wxWebViewConfigurationChromium*>(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<wxWebViewConfigurationChromium*>(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.

View file

@ -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<wxWebViewConfigurationChromium*>(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<wxWebViewConfigurationChromium*>(config.GetNativeConfiguration());
wxCHECK_MSG( configChrome, false, "Should have Chromium-specific config" );
settings.log_severity = static_cast<cef_log_severity_t>(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());