This is a general overview of logging classes provided by wxWidgets.
The word logging here has a broad sense, including all of the program output, not only non-interactive messages. The logging facilities included in wxWidgets provide the base wxLog class which defines the standard interface for a log target as well as several standard implementations of it and a family of functions to use with them.
First of all, no knowledge of wxLog classes is needed to use them. For this, you should only know about wxLogXXX() functions. All of them have the same syntax as printf() or vprintf() , i.e. they take the format string as the first argument and respectively a variable number of arguments or a variable argument list pointer. Here are all of them:
WXDEBUG
is defined) and expands to nothing in release mode (otherwise). Note that under Windows, you must either run the program under debugger or use a 3rd party program such as DebugView (http://www.microsoft.com/technet/sysinternals/Miscellaneous/DebugView.mspx) to actually see the debug output. The usage of these functions should be fairly straightforward, however it may be asked why not use the other logging facilities, such as C standard stdio functions or C++ streams. The short answer is that they're all very good generic mechanisms, but are not really adapted for wxWidgets, while the log classes are. Some of advantages in using wxWidgets log functions are:
By default, most log messages are enabled. In particular, this means that errors logged by wxWidgets code itself (e.g. when it fails to perform some operation, for instance wxFile::Open() logs an error when it fails to open a file) will be processed and shown to the user. To disable the logging entirely you can use wxLog::EnableLogging() method or, more usually, wxLogNull class which temporarily disables logging and restores it back to the original setting when it is destroyed.
To limit logging to important messages only, you may use wxLog::SetLogLevel() with e.g. wxLOG_Warning value – this will completely disable all logging messages with the severity less than warnings, so wxLogMessage() output won't be shown to the user any more.
Moreover, the log level can be set separately for different log components. Before showing how this can be useful, let us explain what log components are: they are simply arbitrary strings identifying the component, or module, which generated the message. They are hierarchical in the sense that "foo/bar/baz" component is supposed to be a child of "foo". And all components are children of the unnamed root component.
By default, all messages logged by wxWidgets originate from "wx" component or one of its subcomponents such as "wx/net/ftp", while the messages logged by your own code are assigned empty log component. To change this, you need to define wxLOG_COMPONENT
to a string uniquely identifying each component, e.g. you could give it the value "MyProgram" by default and re-define it as "MyProgram/DB" in the module working with the database and "MyProgram/DB/Trans" in its part managing the transactions. Then you could use wxLog::SetComponentLevel() in the following ways:
Notice that the log level set explicitly for the transactions code overrides the log level of the parent component but that all other database code subcomponents inherit its setting by default and so won't generate any log messages at all.
After having enumerated all the functions which are normally used to log the messages, and why would you want to use them, we now describe how all this works.
wxWidgets has the notion of a log target: it is just a class deriving from wxLog. As such, it implements the virtual functions of the base class which are called when a message is logged. Only one log target is active at any moment, this is the one used by wxLogXXX() functions. The normal usage of a log object (i.e. object of a class derived from wxLog) is to install it as the active target with a call to SetActiveTarget() and it will be used automatically by all subsequent calls to wxLogXXX() functions.
To create a new log target class you only need to derive it from wxLog and override one or several of wxLog::DoLogRecord(), wxLog::DoLogTextAtLevel() and wxLog::DoLogText() in it. The first one is the most flexible and allows you to change the formatting of the messages, dynamically filter and redirect them and so on – all log messages, except for those generated by wxLogFatalError(), pass by this function. wxLog::DoLogTextAtLevel() should be overridden if you simply want to redirect the log messages somewhere else, without changing their formatting. Finally, it is enough to override wxLog::DoLogText() if you only want to redirect the log messages and the destination doesn't depend on the message log level.
There are some predefined classes deriving from wxLog and which might be helpful to see how you can create a new log target class and, of course, may also be used without any change. There are:
FILE *
, using stderr by default as its name suggests. FILE *
and stderr. The log targets can also be combined: for example you may wish to redirect the messages somewhere else (for example, to a log file) but also process them as normally. For this the wxLogChain, wxLogInterposer, and wxLogInterposerTemp can be used.
Starting with wxWidgets 2.9.1, logging functions can be safely called from any thread. Messages logged from threads other than the main one will be buffered until wxLog::Flush() is called in the main thread (which usually happens during idle time, i.e. after processing all pending events) and will be really output only then. Notice that the default GUI logger already only output the messages when it is flushed, so by default messages from the other threads will be shown more or less at the same moment as usual. However if you define a custom log target, messages may be logged out of order, e.g. messages from the main thread with later timestamp may appear before messages with earlier timestamp logged from other threads. wxLog does however guarantee that messages logged by each thread will appear in order in which they were logged.
Also notice that wxLog::EnableLogging() and wxLogNull class which uses it only affect the current thread, i.e. logging messages may still be generated by the other threads after a call to EnableLogging(false)
.
To completely change the logging behaviour you may define a custom log target. For example, you could define a class inheriting from wxLog which shows all the log messages in some part of your main application window reserved for the message output without interrupting the user work flow with modal message boxes.
To use your custom log target you may either call wxLog::SetActiveTarget() with your custom log object or create a wxAppTraits-derived class and override wxAppTraits::CreateLogTarget() virtual method in it and also override wxApp::CreateTraits() to return an instance of your custom traits object. Notice that in the latter case you should be prepared for logging messages early during the program startup and also during program shutdown so you shouldn't rely on existence of the main application window, for example. You can however safely assume that GUI is (already/still) available when your log target as used as wxWidgets automatically switches to using wxLogStderr if it isn't.
There are several methods which may be overridden in the derived class to customize log messages handling: wxLog::DoLogRecord(), wxLog::DoLogTextAtLevel() and wxLog::DoLogText().
The last method is the simplest one: you should override it if you simply want to redirect the log output elsewhere, without taking into account the level of the message. If you do want to handle messages of different levels differently, then you should override wxLog::DoLogTextAtLevel().
Additionally, you can customize the way full log messages are constructed from the components (such as time stamp, source file information, logging thread ID and so on). This task is performed by wxLogFormatter class so you need to derive a custom class from it and override its Format() method to build the log messages in desired way. Notice that if you just need to modify (or suppress) the time stamp display, overriding FormatTime() is enough.
Finally, if even more control over the output format is needed, then DoLogRecord() can be overridden as it allows to construct custom messages depending on the log level or even do completely different things depending on the message severity (for example, throw away all messages except warnings and errors, show warnings on the screen and forward the error messages to the user's (or programmer's) cell phone – maybe depending on whether the timestamp tells us if it is day or night in the current time zone).
The dialog sample illustrates this approach by defining a custom log target customizing the dialog used by wxLogGui for the single messages.
Notice that the use of log trace masks is hardly necessary any longer in current wxWidgets version as the same effect can be achieved by using different log components for different log statements of any level. Please see Log Messages Selection for more information about the log components.
The functions below allow some limited customization of wxLog behaviour without writing a new log target class (which, aside from being a matter of several minutes, allows you to do anything you want). The verbose messages are the trace messages which are not disabled in the release mode and are generated by wxLogVerbose(). They are not normally shown to the user because they present little interest, but may be activated, for example, in order to help the user find some program problem.
As for the (real) trace messages, their handling depends on the currently enabled trace masks: if wxLog::AddTraceMask() was called for the mask of the given message, it will be logged, otherwise nothing happens.
For example,
will log the message if it was preceded by:
The standard trace masks are given in wxLogTrace() documentation.