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