Add wxApp::GTKSuppressDiagnostics()

This allows to avoid GTK messages that are often more annoying and
confusing than useful.

Closes https://github.com/wxWidgets/wxWidgets/pull/2609

Closes #19347.
This commit is contained in:
Marco DeFreitas
2021-12-08 16:33:09 +01:00
committed by Vadim Zeitlin
parent a0eb355e12
commit a2389fc512
3 changed files with 80 additions and 0 deletions

View File

@@ -58,6 +58,12 @@ public:
// various bugs arising due to this.
static bool GTKIsUsingGlobalMenu();
// Provide the ability to suppress GTK output. By default, all output
// will be suppressed, but the user can pass in a mask specifiyng the
// types of messages to suppress. Flags are defined by glib with the
// GLogLevelFlags enum.
static void GTKSuppressDiagnostics(int flags = -1);
// implementation only from now on
// -------------------------------

View File

@@ -1002,6 +1002,37 @@ public:
*/
void SetUseBestVisual(bool flag, bool forceTrueColour = false);
/**
@name GTK-specific functions
*/
//@{
/**
Disables the printing of various GTK messages.
This function can be called to suppress GTK diagnostic messages that
are output on the standard error stream by default.
The default value of the argument disables all messages, but you
can pass in a mask flag to specifically disable only particular
categories of messages.
Note that this function only works when using glib 2.50 (released in
September 2016) or later and does nothing with the older versions of
the library.
@param flags
The mask for the types of messages to suppress. Refer to the
glib documentation for the @c GLogLevelFlags enum, which defines
the various message types.
@onlyfor{wxgtk}
@since 3.1.6
*/
static void GTKSuppressDiagnostics(int flags = -1);
//@}
/**
@name Mac-specific functions

View File

@@ -178,6 +178,49 @@ bool wxApp::DoIdle()
return keepSource;
}
// Custom Glib log writer: setting it is only possible with glib 2.50 or later.
#if GLIB_CHECK_VERSION(2, 50, 0)
extern "C" {
static GLogWriterOutput
wx_log_writer(GLogLevelFlags log_level,
const GLogField *fields,
gsize n_fields,
gpointer user_data)
{
const wxUIntPtr log_mask = reinterpret_cast<wxUIntPtr>(user_data);
GLogWriterOutput result;
if (log_level & log_mask)
{
result = G_LOG_WRITER_HANDLED;
}
else
{
result = g_log_writer_default(log_level, fields, n_fields, NULL);
}
return result;
}
}
/* static */
void wxApp::GTKSuppressDiagnostics(int flags)
{
if (glib_check_version(2, 50, 0) == 0)
{
g_log_set_writer_func(
wx_log_writer,
(wxUIntToPtr)(flags == -1 ? G_LOG_LEVEL_MASK : flags),
NULL);
}
}
#else // glib < 2.50
/* static */
void wxApp::GTKSuppressDiagnostics(int WXUNUSED(flags))
{
// We can't do anything here.
}
#endif // glib >=/< 2.50
//-----------------------------------------------------------------------------
// wxApp
//-----------------------------------------------------------------------------