1 \section{Log classes overview
}\label{wxlogoverview
}
3 Classes:
\helpref{wxLog
}{wxlog
}
4 %\helpref{wxLogStderr}{wxlogstderr},%
5 %\helpref{wxLogOstream}{wxlogostream}, \helpref{wxLogTextCtrl}{wxlogtextctrl},%
6 %\helpref{wxLogWindow}{wxlogwindow}, \helpref{wxLogGui}{wxloggui},%
7 %\helpref{wxLogNull}{wxlognull}%
9 This is a general overview of logging classes provided by wxWindows. The word
10 logging here has a broad sense, including all of the program output, not only
11 non interactive messages. The logging facilities included in wxWindows provide
12 the base
{\it wxLog
} class which defines the standard interface for a
{\it log
13 target
} as well as several standard implementations of it and a family of
14 functions to use with them.
16 First of all, no knowledge of
{\it wxLog
} classes is needed to use them. For
17 this, you should only know about
{\it wxLogXXX()
} functions. All of them have
18 the same syntax as
{\it printf()
}, i.e. they take the format string as the
19 first argument and a variable number of arguments. Here are all of them:
21 \begin{itemize
}\itemsep=
0pt
22 \item{\bf wxLogFatalError
} which is like
{\it wxLogError
}, but also
23 terminates the program with the exit code
3 (using
{\it abort()
} standard
24 function also terminates the program with this exit code).
25 \item{\bf wxLogError
} is the function to use for error messages, i.e. the
26 messages that must be shown to the user. The default processing is to pop up a
27 message box to inform the user about it.
28 \item{\bf wxLogWarning
} for warnings - they are also normally shown to the
29 user, but don't interrupt the program work.
30 \item{\bf wxLogMessage
} is for all normal, informational messages. They also
31 appear in a message box by default (but it can be changed, see below). Notice
32 that the standard behaviour is to not show informational messages if there are
33 any errors later - the logic being that the later error messages make the
34 informational messages preceding them meaningless.
35 \item{\bf wxLogVerbose
} is for verbose output. Normally, it's suppressed, but
36 might be activated if the user wishes to know more details about the program
37 progress (another, but possibly confusing name for the same function is
{\bf
39 \item{\bf wxLogStatus
} is for status messages - they will go into the status
40 bar of the active or specified (as the first argument)
\helpref{wxFrame
}{wxframe
} if it has one.
41 \item{\bf wxLogSysError
} is mostly used by wxWindows itself, but might be
42 handy for logging errors after system call (API function) failure. It logs the
43 specified message text as well as the last system error
44 code (
{\it errno
} or
{\it ::GetLastError()
} depending on the platform) and the corresponding error
45 message. The second form of this function takes the error code explitly as the
47 \item{\bf wxLogDebug
} is
{\bf the
} right function for debug output. It only
48 does anything at all in the debug mode (when the preprocessor symbol
49 \_\_WXDEBUG\_\_ is defined) and expands to nothing in release mode (otherwise).
50 \item{\bf wxLogTrace
} as
{\bf wxLogDebug
} only does something in debug
51 build. The reason for making it a separate function from it is that usually
52 there are a lot of trace messages, so it might make sense to separate them
53 from other debug messages which would be flooded in them. Moreover, the second
54 version of this function takes a trace mask as the first argument which allows
55 to further restrict the amount of messages generated.
58 The usage of these functions should be fairly straightforward, however it may
59 be asked why not use the other logging facilities, such as C standard stdio
60 functions or C++ streams. The short answer is that they're all very good
61 generic mechanisms, but are not really adapted for wxWindows, while the log
62 classes are. Some of advantages in using wxWindows log functions are:
64 \begin{itemize
}\itemsep=
0pt
65 \item{\bf Portability
} It's a common practice to use
{\it printf()
} statements or
66 cout/cerr C++ streams for writing out some (debug or otherwise) information.
67 Although it works just fine under Unix, these messages go strictly nowhere
68 under Windows where the stdout of GUI programs is not assigned to anything.
69 Thus, you might view
{\it wxLogMessage()
} as a simple substitute for
{\it
71 \item{\bf Flexibility
} The output of wxLog functions can be redirected or
72 suppressed entirely based on their importance, which is either impossible or
73 difficult to do with traditional methods. For example, only error messages, or
74 only error messages and warnings might be logged, filtering out all
75 informational messages.
76 \item{\bf Completeness
} Usually, an error message should be presented to the user
77 when some operation fails. Let's take a quite simple but common case of a file
78 error: suppose that you're writing your data file on disk and there is not
79 enough space. The actual error might have been detected inside wxWindows code
80 (say, in
{\it wxFile::Write
}), so the calling function doesn't really know the
81 exact reason of the failure, it only knows that the data file couldn't be
82 written to the disk. However, as wxWindows uses
{\it wxLogError()
} in this
83 situation, the exact error code (and the corresponding error message) will be
84 given to the user together with "high level" message about data file writing
88 After having enumerated all the functions which are normally used to log the
89 messages, and why would you want to use them we now describe how all this
92 wxWindows has the notion of a
{\it log target
}: it's just a class deriving
93 from
\helpref{wxLog
}{wxlog
}. As such, it implements the virtual functions of
94 the base class which are called when a message is logged. Only one log target
95 is
{\it active
} at any moment, this is the one used by
\it{wxLogXXX()
}
96 functions. The normal usage of a log object (i.e. object of a class derived
97 from wxLog) is to install it as the active target with a call to
{\it
98 SetActiveTarget()
} and it will be used automatically by all subsequent calls
99 to
{\it wxLogXXX()
} functions.
101 To create a new log target class you only need to derive it from wxLog and
102 implement one (or both) of
{\it DoLog()
} and
{\it DoLogString()
} in it. The
103 second one is enough if you're happy with the standard wxLog message
104 formatting (prepending "Error:" or "Warning:", timestamping \&c) but just want
105 to send the messages somewhere else. The first one may be overridden to do
106 whatever you want but you have to distinguish between the different message
109 There are some predefined classes deriving from wxLog and which might be
110 helpful to see how you can create a new log target class and, of course, may
111 also be used without any change. There are:
113 \begin{itemize
}\itemsep=
0pt
114 \item{\bf wxLogStderr
} This class logs messages to a
{\it FILE *
}, using
115 stderr by default as its name suggests.
116 \item{\bf wxLogStream
} This class has the same functionality as wxLogStderr,
117 but uses
{\it ostream
} and cerr instead of
{\it FILE *
} and stderr.
118 \item{\bf wxLogGui
} This is the standard log target for wxWindows
119 applications (it's used by default if you don't do anything) and provides the
120 most reasonable handling of all types of messages for given platform.
121 \item{\bf wxLogWindow
} This log target provides a "log console" which
122 collects all messages generated by the application and also passes them to the
123 previous active log target. The log window frame has a menu allowing user to
124 clear the log, close it completely or save all messages to file.
125 \item{\bf wxLogNull
} The last log class is quite particular: it doesn't do
126 anything. The objects of this class may be instantiated to (temporarily)
127 suppress output of
{\it wxLogXXX()
} functions. As an example, trying to open a
128 non-existing file will usually provoke an error message, but if you for some
129 reason it's unwanted, just use this construction:
135 // wxFile.Open() normally complains if file can't be opened, we don't want it
138 if ( !file.Open("bar") )
139 ... process error ourselves ...
140 } // ~wxLogNull called, old log sink restored
142 wxLogMessage("..."); // ok