From a2389fc512b72ca2fb6261dfbde024a7819500de Mon Sep 17 00:00:00 2001 From: Marco DeFreitas Date: Wed, 8 Dec 2021 16:33:09 +0100 Subject: [PATCH] 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. --- include/wx/gtk/app.h | 6 ++++++ interface/wx/app.h | 31 +++++++++++++++++++++++++++++++ src/gtk/app.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/include/wx/gtk/app.h b/include/wx/gtk/app.h index 2682515592..b89c70bcb0 100644 --- a/include/wx/gtk/app.h +++ b/include/wx/gtk/app.h @@ -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 // ------------------------------- diff --git a/interface/wx/app.h b/interface/wx/app.h index 0cad9adb6b..e9b869588a 100644 --- a/interface/wx/app.h +++ b/interface/wx/app.h @@ -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 diff --git a/src/gtk/app.cpp b/src/gtk/app.cpp index afae00e006..ab392b150e 100644 --- a/src/gtk/app.cpp +++ b/src/gtk/app.cpp @@ -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(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 //-----------------------------------------------------------------------------