Using wxLog
In my current study of wxWindows I found it extremely useful to use the built in wxLog classes. However, I was a little confused as to how they were to be implemented. After a few trials, I came up with the basic boilerplate steps below:
- This first set assumes use of a wxLogWindow as a basic console window to show messages from the application.
- In the wxApp::OnInit() implementation of the program create an instance of a wxLog class (i.e. wxLogWindow, wxLogStream, etc.)
- For the parent of the wxLogWindow set it to NULL for no parent or set it to your main wxFrame or wxDialogue window as the parent. For other loggers, such as wxLogStream, set the appropriate stream or other parameters.
- For wxLogWindow, I set the caption to a suitable value and set the window to automatically show. I also set the “passToOld” parameter to FALSE so that the main log window shows all messages and it doesn’t pass the messages through the normal process.
- If desired, and you are using trace masks, use SetVerbose(TRUE);
- It is important to set the active log as the log window (or stream) that you just created. Otherwise, the application continues to send messages to the normal GUI (which is a message box). By using wxLog::SetActiveTarget(logWin) I was able to pass the messages to my console window and stop the annoying message boxes from showing.
- Of course, there are a variety of other choices for the loggin mechanism, but this allowed me to get started with the logging functions quickly and allowed me to immediately begin showing important messages during the debugging/learning process.
Example:
MyFrame *theFrame=new MyFrame;
SetTopWindow(theFrame);
wxLogWindow* logWin = new wxLogWindow(theFrame, "Log Console",TRUE,FALSE);
logWin->SetVerbose(TRUE);
wxLog::SetActiveTarget(logWin);
theFrame->Show();