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