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