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