wxlog classes overview
classes: wxlog,
wxlogstderr,
wxlogstream,
wxlogtextctrl,
wxlogwindow,
wxloggui,
wxlognull,
wxlogchain,
wxlogpassthrough,
wxstreamtotextredirector
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:
- wxlogfatalerror which is like wxlogerror, but also
terminates the program with the exit code 3 (using abort() standard
function). unlike for all the other logging functions, this function can't be
overridden by a log target.
- wxlogerror is the function to use for error messages, i.e. the
messages that must be shown to the user. the default processing is to pop up a
message box to inform the user about it.
- wxlogwarning for warnings - they are also normally shown to the
user, but don't interrupt the program work.
- wxlogmessage is for all normal, informational messages. they also
appear in a message box by default (but it can be changed, see below).
- wxlogverbose is for verbose output. normally, it is suppressed, but
might be activated if the user wishes to know more details about the program
progress (another, but possibly confusing name for the same function is
wxloginfo).
- wxlogstatus is for status messages - they will go into the status
bar of the active or specified (as the first argument) wxframe if it has one.
- wxlogsyserror is mostly used by wxwidgets itself, but might be
handy for logging errors after system call (api function) failure. it logs the
specified message text as well as the last system error
code (errno or ::getlasterror() depending on the platform) and
the corresponding error message. the second form of this function takes the
error code explicitly as the first argument.
- wxlogdebug is the right function for debug output. it only
does anything at all in the debug mode (when the preprocessor symbol
__wxdebug__ is defined) and expands to nothing in release mode (otherwise).
tip: under windows, you must either run the program under debugger or
use a 3rd party program such as dbgview
to actually see the debug output.
- wxlogtrace as wxlogdebug only does something in debug
build. the reason for making it a separate function from it is that usually
there are a lot of trace messages, so it might make sense to separate them
from other debug messages which would be flooded in them. moreover, the second
version of this function takes a trace mask as the first argument which allows
to further restrict the amount of messages generated.
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:
- portability it is a common practice to use printf()
statements or cout/cerr c++ streams for writing out some (debug or otherwise)
information.
although it works just fine under unix, these messages go strictly nowhere
under windows where the stdout of gui programs is not assigned to anything.
thus, you might view wxlogmessage() as a simple substitute for
printf().
you can also redirect the wxlogxxx calls to cout by just writing:
wxlog *logger=new wxlogstream(&cout);
wxlog::setactivetarget(logger);
finally, there is also a possibility to redirect the output sent to cout
to a wxtextctrl by using the
wxstreamtotextredirector class.
- flexibility the output of wxlog functions can be redirected or
suppressed entirely based on their importance, which is either impossible or
difficult to do with traditional methods. for example, only error messages, or
only error messages and warnings might be logged, filtering out all
informational messages.
- completeness usually, an error message should be presented to the user
when some operation fails. let's take a quite simple but common case of a file
error: suppose that you're writing your data file on disk and there is not
enough space. the actual error might have been detected inside wxwidgets code
(say, in wxfile::write), so the calling function doesn't really know the
exact reason of the failure, it only knows that the data file couldn't be
written to the disk. however, as wxwidgets uses wxlogerror() in this
situation, the exact error code (and the corresponding error message) will be
given to the user together with "high level" message about data file writing
error.
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
implement one (or both) of dolog() and dologstring() in it. the
second one is enough if you're happy with the standard wxlog message
formatting (prepending "error:" or "warning:", timestamping &c) but just want
to send the messages somewhere else. the first one may be overridden to do
whatever you want but you have to distinguish between the different message
types yourself.
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:
- wxlogstderr this class logs messages to a file *, using
stderr by default as its name suggests.
- wxlogstream this class has the same functionality as wxlogstderr,
but uses ostream and cerr instead of file * and stderr.
- wxloggui this is the standard log target for wxwidgets
applications (it is used by default if you don't do anything) and provides the
most reasonable handling of all types of messages for given platform.
- wxlogwindow this log target provides a "log console" which
collects all messages generated by the application and also passes them to the
previous active log target. the log window frame has a menu allowing user to
clear the log, close it completely or save all messages to file.
- wxlognull the last log class is quite particular: it doesn't do
anything. the objects of this class may be instantiated to (temporarily)
suppress output of wxlogxxx() functions. as an example, trying to open a
non-existing file will usually provoke an error message, but if for some
reasons it is unwanted, just use this construction:
wxfile file;
// wxfile.open() normally complains if file can't be opened, we don't want it
{
wxlognull logno;
if ( !file.open("bar") )
... process error ourselves ...
} // ~wxlognull called, old log sink restored
wxlogmessage("..."); // ok
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 and
wxlogpassthrough can be used.
|