diff --git a/include/wx/log.h b/include/wx/log.h index 724d80aae5..83e08f7e10 100644 --- a/include/wx/log.h +++ b/include/wx/log.h @@ -765,6 +765,41 @@ private: bool m_flagOld; // the previous value of the wxLog::ms_doLog }; +// ---------------------------------------------------------------------------- +// Collect all logged messages into a (multiline) string. +// ---------------------------------------------------------------------------- + +// This class is supposed to be used as a local variable and collects, without +// showing them, all the messages logged during its lifetime. +class wxLogCollector +{ +public: + wxLogCollector() + : m_logOrig{wxLog::SetActiveTarget(&m_logBuf)} + { + delete m_logBuf.SetFormatter(new wxLogFormatterNone{}); + } + + ~wxLogCollector() + { + // Don't flush the messages in the buffer. + m_logBuf.Clear(); + + wxLog::SetActiveTarget(m_logOrig); + } + + const wxString& GetMessages() const + { + return m_logBuf.GetBuffer(); + } + +private: + wxLogBuffer m_logBuf; + wxLog* const m_logOrig; + + wxDECLARE_NO_COPY_CLASS(wxLogCollector); +}; + // ---------------------------------------------------------------------------- // chaining log target: installs itself as a log target and passes all // messages to the real log target given to it in the ctor but also forwards diff --git a/interface/wx/log.h b/interface/wx/log.h index e65691c3a3..a8fc5125f7 100644 --- a/interface/wx/log.h +++ b/interface/wx/log.h @@ -957,6 +957,65 @@ public: }; +/** + @class wxLogCollector + + Allows to collect all log messages into a string instead of showing them. + + This class is supposed to be used as a local variable and collects all the + messages logged during its lifetime instead of showing them as usual, e.g. + + @code + void Foo() + { + wxLogCollector collectLogs; + + // Call some function that can log error messages, e.g. try to create a + // new directory. Without wxLogCollector a failure here would show + // errors to the user. + if ( !wxFileName::Mkdir("/some/path", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL) ) + { + // Instead, we can report them here as we see fit, e.g. write them + // to a log file or process them in some other way. + wxFprintf(logFile, "Creating directory failed: %s", + collectLogs.GetMessages()); + } + } + @endcode + + Note that because this class uses wxLog::SetActiveTarget() to temporarily + switch the active log target to wxLogBuffer, you need to ensure that the + log target doesn't change while it is alive (in the simplest case by just + avoiding to change it at all). + + @since 3.3.0 +*/ +class wxLogCollector +{ +public: + /** + Constructor overrides active log target to collect messages. + */ + wxLogCollector(); + + /** + Get all the collected messages. + + The returned string may be empty but if it isn't, it contains the + trailing new line (and may also contain more new lines inside it if + multiple messages were logged). + + Note that the messages here contain just the messages, without any time + stamps or log level prefixes. + */ + const wxString& GetMessages() const; + + /** + Destructor restores the previously active log target. + */ + ~wxLogCollector(); +}; + /** @class wxLogNull @@ -1005,6 +1064,8 @@ public: This class is thread-safe and can be used from both the main and the backgrounds threads. + @see wxLogCollector + @library{wxbase} @category{logging} */