]>
Commit | Line | Data |
---|---|---|
15b6757b | 1 | ///////////////////////////////////////////////////////////////////////////// |
9715cf42 | 2 | // Name: log.h |
15b6757b FM |
3 | // Purpose: topic overview |
4 | // Author: wxWidgets team | |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
880efa2a | 9 | /** |
36c9828f | 10 | |
9715cf42 BP |
11 | @page overview_log wxLog Classes Overview |
12 | ||
13 | Classes: | |
14 | @li wxLog | |
15 | @li wxLogStderr | |
16 | @li wxLogStream | |
17 | @li wxLogTextCtrl | |
18 | @li wxLogWindow | |
19 | @li wxLogGui | |
20 | @li wxLogNull | |
21 | @li wxLogBuffer | |
22 | @li wxLogChain | |
23 | @li wxLogInterposer | |
24 | @li wxLogInterposerTemp | |
25 | @li wxStreamToTextRedirector | |
26 | ||
9ad2fe62 VZ |
27 | @li @ref overview_log_introduction |
28 | @li @ref overview_log_targets | |
29 | @li @ref overview_log_customize | |
30 | ||
335da902 FM |
31 | <hr> |
32 | ||
9ad2fe62 VZ |
33 | |
34 | @section overview_log_introduction Introduction | |
35 | ||
9715cf42 BP |
36 | This is a general overview of logging classes provided by wxWidgets. The word |
37 | logging here has a broad sense, including all of the program output, not only | |
38 | non-interactive messages. The logging facilities included in wxWidgets provide | |
39 | the base wxLog class which defines the standard interface for a @e log target | |
40 | as well as several standard implementations of it and a family of functions to | |
41 | use with them. | |
42 | ||
43 | First of all, no knowledge of wxLog classes is needed to use them. For this, | |
44 | you should only know about @e wxLogXXX() functions. All of them have the same | |
45 | syntax as @e printf() or @e vprintf() , i.e. they take the format string as the | |
46 | first argument and respectively a variable number of arguments or a variable | |
47 | argument list pointer. Here are all of them: | |
48 | ||
49 | @li wxLogFatalError which is like wxLogError, but also terminates the program | |
50 | with the exit code 3 (using @e abort() standard function). Unlike for all | |
51 | the other logging functions, this function can't be overridden by a log | |
52 | target. | |
53 | @li wxLogError is the function to use for error messages, i.e. the messages | |
54 | that must be shown to the user. The default processing is to pop up a | |
55 | message box to inform the user about it. | |
56 | @li wxLogWarning for warnings. They are also normally shown to the user, but | |
57 | don't interrupt the program work. | |
58 | @li wxLogMessage is for all normal, informational messages. They also appear in | |
59 | a message box by default (but it can be changed, see below). | |
60 | @li wxLogVerbose is for verbose output. Normally, it is suppressed, but might | |
61 | be activated if the user wishes to know more details about the program | |
62 | progress (another, but possibly confusing name for the same function is | |
63 | wxLogInfo). | |
64 | @li wxLogStatus is for status messages. They will go into the status bar of the | |
65 | active or specified (as the first argument) wxFrame if it has one. | |
66 | @li wxLogSysError is mostly used by wxWidgets itself, but might be handy for | |
67 | logging errors after system call (API function) failure. It logs the | |
68 | specified message text as well as the last system error code (@e errno or | |
69 | ::GetLastError() depending on the platform) and the corresponding error | |
70 | message. The second form of this function takes the error code explicitly | |
71 | as the first argument. | |
72 | @li wxLogDebug is @b the right function for debug output. It only does anything | |
73 | at all in the debug mode (when the preprocessor symbol __WXDEBUG__ is | |
74 | defined) and expands to nothing in release mode (otherwise). @b Tip: under | |
75 | Windows, you must either run the program under debugger or use a 3rd party | |
76 | program such as DebugView to actually see the debug output. | |
77 | - DebugView: http://www.microsoft.com/technet/sysinternals/Miscellaneous/DebugView.mspx | |
78 | @li wxLogTrace as wxLogDebug only does something in debug build. The reason for | |
79 | making it a separate function from it is that usually there are a lot of | |
80 | trace messages, so it might make sense to separate them from other debug | |
81 | messages which would be flooded in them. Moreover, the second version of | |
82 | this function takes a trace mask as the first argument which allows to | |
83 | further restrict the amount of messages generated. | |
84 | ||
85 | The usage of these functions should be fairly straightforward, however it may | |
86 | be asked why not use the other logging facilities, such as C standard stdio | |
87 | functions or C++ streams. The short answer is that they're all very good | |
88 | generic mechanisms, but are not really adapted for wxWidgets, while the log | |
89 | classes are. Some of advantages in using wxWidgets log functions are: | |
90 | ||
91 | @li @b Portability: It is a common practice to use @e printf() statements or | |
92 | cout/cerr C++ streams for writing out some (debug or otherwise) | |
93 | information. Although it works just fine under Unix, these messages go | |
94 | strictly nowhere under Windows where the stdout of GUI programs is not | |
95 | assigned to anything. Thus, you might view wxLogMessage() as a simple | |
96 | substitute for @e printf(). | |
97 | You can also redirect the @e wxLogXXX calls to @e cout by just writing: | |
98 | @code | |
99 | wxLog* logger = new wxLogStream(&cout); | |
100 | wxLog::SetActiveTarget(logger); | |
101 | @endcode | |
102 | Finally, there is also a possibility to redirect the output sent to @e cout | |
103 | to a wxTextCtrl by using the wxStreamToTextRedirector class. | |
104 | @li @b Flexibility: The output of wxLog functions can be redirected or | |
105 | suppressed entirely based on their importance, which is either impossible | |
106 | or difficult to do with traditional methods. For example, only error | |
107 | messages, or only error messages and warnings might be logged, filtering | |
108 | out all informational messages. | |
109 | @li @b Completeness: Usually, an error message should be presented to the user | |
110 | when some operation fails. Let's take a quite simple but common case of a | |
111 | file error: suppose that you're writing your data file on disk and there is | |
112 | not enough space. The actual error might have been detected inside | |
113 | wxWidgets code (say, in wxFile::Write), so the calling function doesn't | |
114 | really know the exact reason of the failure, it only knows that the data | |
115 | file couldn't be written to the disk. However, as wxWidgets uses | |
116 | wxLogError() in this situation, the exact error code (and the corresponding | |
117 | error message) will be given to the user together with "high level" message | |
118 | about data file writing error. | |
119 | ||
9ad2fe62 VZ |
120 | |
121 | @section overview_log_targets Log Targets | |
122 | ||
9715cf42 BP |
123 | After having enumerated all the functions which are normally used to log the |
124 | messages, and why would you want to use them we now describe how all this | |
125 | works. | |
126 | ||
127 | wxWidgets has the notion of a <em>log target</em>: it is just a class deriving | |
128 | from wxLog. As such, it implements the virtual functions of the base class | |
129 | which are called when a message is logged. Only one log target is @e active at | |
130 | any moment, this is the one used by @e wxLogXXX() functions. The normal usage | |
131 | of a log object (i.e. object of a class derived from wxLog) is to install it as | |
132 | the active target with a call to @e SetActiveTarget() and it will be used | |
133 | automatically by all subsequent calls to @e wxLogXXX() functions. | |
134 | ||
135 | To create a new log target class you only need to derive it from wxLog and | |
136 | implement one (or both) of @e DoLog() and @e DoLogString() in it. The second | |
137 | one is enough if you're happy with the standard wxLog message formatting | |
138 | (prepending "Error:" or "Warning:", timestamping @&c) but just want to send | |
139 | the messages somewhere else. The first one may be overridden to do whatever | |
140 | you want but you have to distinguish between the different message types | |
141 | yourself. | |
142 | ||
143 | There are some predefined classes deriving from wxLog and which might be | |
144 | helpful to see how you can create a new log target class and, of course, may | |
145 | also be used without any change. There are: | |
146 | ||
147 | @li wxLogStderr: This class logs messages to a <tt>FILE *</tt>, using stderr by | |
148 | default as its name suggests. | |
149 | @li wxLogStream: This class has the same functionality as wxLogStderr, but uses | |
150 | @e ostream and cerr instead of <tt>FILE *</tt> and stderr. | |
151 | @li wxLogGui: This is the standard log target for wxWidgets applications (it is | |
152 | used by default if you don't do anything) and provides the most reasonable | |
153 | handling of all types of messages for given platform. | |
154 | @li wxLogWindow: This log target provides a "log console" which collects all | |
155 | messages generated by the application and also passes them to the previous | |
156 | active log target. The log window frame has a menu allowing user to clear | |
157 | the log, close it completely or save all messages to file. | |
158 | @li wxLogBuffer: This target collects all the logged messages in an internal | |
159 | buffer allowing to show them later to the user all at once. | |
160 | @li wxLogNull: The last log class is quite particular: it doesn't do anything. | |
161 | The objects of this class may be instantiated to (temporarily) suppress | |
162 | output of @e wxLogXXX() functions. As an example, trying to open a | |
163 | non-existing file will usually provoke an error message, but if for some | |
164 | reasons it is unwanted, just use this construction: | |
165 | @code | |
166 | wxFile file; | |
167 | ||
168 | // wxFile.Open() normally complains if file can't be opened, we don't want it | |
169 | { | |
170 | wxLogNull logNo; | |
171 | if ( !file.Open("bar") ) | |
172 | { | |
173 | // ... process error ourselves ... | |
174 | } | |
175 | } // ~wxLogNull called, old log sink restored | |
176 | ||
177 | wxLogMessage("..."); // ok | |
178 | @endcode | |
179 | ||
180 | The log targets can also be combined: for example you may wish to redirect the | |
181 | messages somewhere else (for example, to a log file) but also process them as | |
182 | normally. For this the wxLogChain, wxLogInterposer, and wxLogInterposerTemp can | |
183 | be used. | |
184 | ||
9ad2fe62 VZ |
185 | |
186 | @section overview_log_customize Logging Customization | |
187 | ||
188 | To completely change the logging behaviour you may define a custom log target. | |
189 | For example, you could define a class inheriting from wxLog which shows all the | |
190 | log messages in some part of your main application window reserved for the | |
191 | message output without interrupting the user work flow with modal message | |
192 | boxes. | |
193 | ||
194 | To use your custom log target you may either call wxLog::SetActiveTarget() with | |
195 | your custom log object or create a wxAppTraits-derived class and override | |
196 | CreateLogTarget() virtual method in it and also override wxApp::CreateTraits() | |
197 | to return an instance of your custom traits object. Notice that in the latter | |
198 | case you should be prepared for logging messages early during the program | |
199 | startup and also during program shutdown so you shouldn't rely on existence of | |
200 | the main application window, for example. You can however safely assume that | |
201 | GUI is (already/still) available when your log target as used as wxWidgets | |
202 | automatically switches to using wxLogStderr if it isn't. | |
203 | ||
204 | The dialog sample illustrates this approach by defining a custom log target | |
205 | customizing the dialog used by wxLogGui for the single messages. | |
206 | ||
9715cf42 | 207 | */ |
36c9828f | 208 |