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