Version: 3.3.0
wxLogCollector Class Reference

#include <wx/log.h>

Detailed Description

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.

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());
}
}
bool Mkdir(int perm=wxS_DIR_DEFAULT, int flags=0) const
Creates a directory.
Allows to collect all log messages into a string instead of showing them.
Definition: log.h:995
const wxString & GetMessages() const
Get all the collected messages.
@ wxS_DIR_DEFAULT
Default mode for the new directories (see wxFileName::Mkdir): allow reading/writing/executing them to...
Definition: filefn.h:399

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

Public Member Functions

 wxLogCollector ()
 Constructor overrides active log target to collect messages. More...
 
const wxStringGetMessages () const
 Get all the collected messages. More...
 
 ~wxLogCollector ()
 Destructor restores the previously active log target. More...
 

Constructor & Destructor Documentation

◆ wxLogCollector()

wxLogCollector::wxLogCollector ( )

Constructor overrides active log target to collect messages.

◆ ~wxLogCollector()

wxLogCollector::~wxLogCollector ( )

Destructor restores the previously active log target.

Member Function Documentation

◆ GetMessages()

const wxString& wxLogCollector::GetMessages ( ) const

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.