wxLog

From WxWiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Official Classes SmallBlocks.png Archive Containers Controls Data Structures Database Date & Time Debug Device Contexts Dialogs Document & Views Drag & Drop Events Filesystem Frames Graphics Grid Cell Help HTML Logging Miscellaneous Networking Printing Sizers Streams Threading Windows

wxLog class defines the interface for the log targets used by wxWidgets logging functions as explained in the Logging Overview.

The only situations when you need to directly use this class is when you want to derive your own log target because the existing ones don't satisfy your needs.

Otherwise, it is completely hidden behind the wxLogXXX() functions and you may not even know about its existence.

Note: For console-mode applications, the default target is wxLogStderr, so that all wxLogXXX() functions print on stderr when wxUSE_GUI = 0.

Pitfalls

Avoid long messages in the static wxLog functions.

(Don't know where the error comes from but wxLogFrame doesn't display long multiline error messages correctly. The reason for someone to use long messages might be the bad performance of wxTextControl::AppendText, which I think is GTK specific. -- Sascha Doerdelmann)

Example of sending messages to a log file for both release and debug versions

 if (m_pLogFile == NULL)
{
   m_pLogFile = fopen( "log.txt", "w+" );
   delete wxLog::SetActiveTarget(new wxLogStderr(m_pLogFile));
}
wxLogMessage("Test");

When you close your program you would call:

delete wxLog::SetActiveTarget(NULL);
if (m_pLogFile != NULL)
{
	fclose(m_pLogFile);
}

Note that you must put the new target on the heap when you call SetActiveTarget and you must delete the return value (which is the old target). Also, you must use wxLogMessage for release builds. Normally this sends the message to a messagbox, but since you changed the target it won't. You could also force messages to go to stderr by doing this:

delete wxLog::SetActiveTarget(new wxLogStderr(NULL));

See Also