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