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