Optionally detect not filtered GTK log messages

In the future it might be useful to run the code with WXTRACE=gtklog to
see if any GTK log messages we're filtering don't need to be filtered
any longer.
This commit is contained in:
Vadim Zeitlin
2022-05-12 00:34:54 +02:00
parent 8f49ecc3d0
commit bffcb88266
2 changed files with 20 additions and 5 deletions

View File

@@ -34,8 +34,8 @@ public:
} }
// Function to call to install this filter as the active one. // Function to call to install this filter as the active one.
// Does nothing if run-time glib version is too old. // Does nothing and just returns false if run-time glib version is too old.
void Install(); bool Install();
protected: protected:
// Function to override in the derived class to actually filter: return // Function to override in the derived class to actually filter: return
@@ -104,10 +104,15 @@ public:
explicit LogFilterByMessage(const char* message) explicit LogFilterByMessage(const char* message)
: m_message(message) : m_message(message)
{ {
Install(); // We shouldn't warn about anything if Install() failed.
m_warnNotFiltered = Install();
} }
// Remove this filter when the object goes out of scope. // Remove this filter when the object goes out of scope.
//
// The dtor also checks if we actually filtered the message and logs a
// trace message with the "gtklog" mask if we didn't: this allows checking
// if the filter is actually being used.
~LogFilterByMessage(); ~LogFilterByMessage();
protected: protected:
@@ -118,6 +123,8 @@ protected:
private: private:
const char* const m_message; const char* const m_message;
mutable bool m_warnNotFiltered;
wxDECLARE_NO_COPY_CLASS(LogFilterByMessage); wxDECLARE_NO_COPY_CLASS(LogFilterByMessage);
}; };

View File

@@ -203,14 +203,14 @@ LogFilter::wx_log_writer(GLogLevelFlags log_level,
return g_log_writer_default(log_level, fields, n_fields, NULL); return g_log_writer_default(log_level, fields, n_fields, NULL);
} }
void LogFilter::Install() bool LogFilter::Install()
{ {
if ( !ms_installed ) if ( !ms_installed )
{ {
if ( glib_check_version(2, 50, 0) != 0 ) if ( glib_check_version(2, 50, 0) != 0 )
{ {
// No runtime support for log callback, we can't do anything. // No runtime support for log callback, we can't do anything.
return; return false;
} }
g_log_set_writer_func(LogFilter::wx_log_writer, NULL, NULL); g_log_set_writer_func(LogFilter::wx_log_writer, NULL, NULL);
@@ -220,6 +220,8 @@ void LogFilter::Install()
// Put this object in front of the linked list. // Put this object in front of the linked list.
m_next = ms_first; m_next = ms_first;
ms_first = this; ms_first = this;
return true;
} }
void LogFilter::Uninstall() void LogFilter::Uninstall()
@@ -248,6 +250,7 @@ bool LogFilterByMessage::Filter(GLogLevelFlags WXUNUSED(log_level),
if ( strcmp(static_cast<const char*>(f.value), m_message) == 0 ) if ( strcmp(static_cast<const char*>(f.value), m_message) == 0 )
{ {
// This is the message we want to filter. // This is the message we want to filter.
m_warnNotFiltered = false;
return true; return true;
} }
} }
@@ -259,6 +262,11 @@ bool LogFilterByMessage::Filter(GLogLevelFlags WXUNUSED(log_level),
LogFilterByMessage::~LogFilterByMessage() LogFilterByMessage::~LogFilterByMessage()
{ {
Uninstall(); Uninstall();
if ( m_warnNotFiltered )
{
wxLogTrace("gtklog", "Message \"%s\" wasn't logged.", m_message);
}
} }
} // namespace wxGTKImpl } // namespace wxGTKImpl