Add LogFilterByMessage GTK log filter

This will allow suppressing GTK log messages with the specified
fixed contents.
This commit is contained in:
Vadim Zeitlin 2022-05-12 00:27:39 +02:00
parent f137f19bb7
commit 4602caf49d
2 changed files with 68 additions and 0 deletions

View File

@ -45,6 +45,9 @@ protected:
const GLogField* fields,
gsize n_fields) const = 0;
// Typically called from the derived class dtor to stop using this filter.
void Uninstall();
private:
// The function used as glib log writer.
static GLogWriterOutput
@ -92,6 +95,32 @@ private:
wxDECLARE_NO_COPY_CLASS(LogFilterByLevel);
};
// LogFilterByMessage filters out all the messages with the specified content.
class LogFilterByMessage : public LogFilter
{
public:
// Objects of this class are supposed to be created with literal strings as
// argument, so don't bother copying the string but just use the pointer.
explicit LogFilterByMessage(const char* message)
: m_message(message)
{
Install();
}
// Remove this filter when the object goes out of scope.
~LogFilterByMessage();
protected:
bool Filter(GLogLevelFlags WXUNUSED(log_level),
const GLogField* fields,
gsize n_fields) const wxOVERRIDE;
private:
const char* const m_message;
wxDECLARE_NO_COPY_CLASS(LogFilterByMessage);
};
#endif // wxHAS_GLIB_LOG_WRITER
} // namespace wxGTKImpl

View File

@ -222,6 +222,45 @@ void LogFilter::Install()
ms_first = this;
}
void LogFilter::Uninstall()
{
if ( !ms_installed )
{
// We don't do anything at all in this case.
return;
}
// We should be uninstalling only the currently installed filter.
wxASSERT( ms_first == this );
ms_first = m_next;
}
bool LogFilterByMessage::Filter(GLogLevelFlags WXUNUSED(log_level),
const GLogField* fields,
gsize n_fields) const
{
for ( gsize n = 0; n < n_fields; ++n )
{
const GLogField& f = fields[n];
if ( strcmp(f.key, "MESSAGE") == 0 )
{
if ( strcmp(static_cast<const char*>(f.value), m_message) == 0 )
{
// This is the message we want to filter.
return true;
}
}
}
return false;
}
LogFilterByMessage::~LogFilterByMessage()
{
Uninstall();
}
} // namespace wxGTKImpl
/* static */