]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/log.h
The tree only sets the focus in response to a TVN_SELCHANGINGW event if the tree...
[wxWidgets.git] / docs / doxygen / overviews / log.h
CommitLineData
15b6757b 1/////////////////////////////////////////////////////////////////////////////
9715cf42 2// Name: log.h
15b6757b
FM
3// Purpose: topic overview
4// Author: wxWidgets team
5// RCS-ID: $Id$
526954c5 6// Licence: wxWindows licence
15b6757b
FM
7/////////////////////////////////////////////////////////////////////////////
8
880efa2a 9/**
36c9828f 10
9715cf42
BP
11@page overview_log wxLog Classes Overview
12
54e280d8 13Classes: wxLog, wxLogStderr, wxLogStream, wxLogTextCtrl, wxLogWindow, wxLogGui, wxLogNull, wxLogBuffer,
4ffdb640 14 wxLogChain, wxLogInterposer, wxLogInterposerTemp, wxStreamToTextRedirector, wxLogFormatter
9715cf42 15
54e280d8 16Table of contents:
9ad2fe62 17@li @ref overview_log_introduction
54e280d8 18@li @ref overview_log_enable
9ad2fe62 19@li @ref overview_log_targets
54e280d8 20@li @ref overview_log_mt
9ad2fe62 21@li @ref overview_log_customize
54e280d8 22@li @ref overview_log_tracemasks
335da902
FM
23<hr>
24
9ad2fe62
VZ
25
26@section overview_log_introduction Introduction
27
9715cf42
BP
28This is a general overview of logging classes provided by wxWidgets. The word
29logging here has a broad sense, including all of the program output, not only
30non-interactive messages. The logging facilities included in wxWidgets provide
31the base wxLog class which defines the standard interface for a @e log target
32as well as several standard implementations of it and a family of functions to
33use with them.
34
35First of all, no knowledge of wxLog classes is needed to use them. For this,
54e280d8
FM
36you should only know about @ref group_funcmacro_log "wxLogXXX() functions".
37All of them have the same syntax as @e printf() or @e vprintf() , i.e. they
38take the format string as the first argument and respectively a variable number
39of arguments or a variable argument list pointer. Here are all of them:
9715cf42 40
54e280d8 41@li wxLogFatalError() which is like wxLogError(), but also terminates the program
9715cf42
BP
42 with the exit code 3 (using @e abort() standard function). Unlike for all
43 the other logging functions, this function can't be overridden by a log
44 target.
54e280d8 45@li wxLogError() is the function to use for error messages, i.e. the messages
9715cf42
BP
46 that must be shown to the user. The default processing is to pop up a
47 message box to inform the user about it.
54e280d8 48@li wxLogWarning() for warnings. They are also normally shown to the user, but
9715cf42 49 don't interrupt the program work.
54e280d8 50@li wxLogMessage() is for all normal, informational messages. They also appear in
9715cf42 51 a message box by default (but it can be changed, see below).
54e280d8 52@li wxLogVerbose() is for verbose output. Normally, it is suppressed, but might
9715cf42
BP
53 be activated if the user wishes to know more details about the program
54 progress (another, but possibly confusing name for the same function is
55 wxLogInfo).
54e280d8 56@li wxLogStatus() is for status messages. They will go into the status bar of the
9715cf42 57 active or specified (as the first argument) wxFrame if it has one.
54e280d8 58@li wxLogSysError() is mostly used by wxWidgets itself, but might be handy for
9715cf42
BP
59 logging errors after system call (API function) failure. It logs the
60 specified message text as well as the last system error code (@e errno or
54e280d8 61 Windows' @e GetLastError() depending on the platform) and the corresponding error
9715cf42
BP
62 message. The second form of this function takes the error code explicitly
63 as the first argument.
54e280d8
FM
64@li wxLogDebug() is @b the right function for debug output. It only does anything
65 at all in the debug mode (when the preprocessor symbol @c __WXDEBUG__ is
af588446 66 defined) and expands to nothing in release mode (otherwise).
54e280d8 67 Note that under Windows, you must either run the program under debugger or
af588446
VZ
68 use a 3rd party program such as DebugView
69 (http://www.microsoft.com/technet/sysinternals/Miscellaneous/DebugView.mspx)
70 to actually see the debug output.
54e280d8 71@li wxLogTrace() as wxLogDebug() only does something in debug build. The reason for
9715cf42
BP
72 making it a separate function from it is that usually there are a lot of
73 trace messages, so it might make sense to separate them from other debug
74 messages which would be flooded in them. Moreover, the second version of
75 this function takes a trace mask as the first argument which allows to
76 further restrict the amount of messages generated.
77
78The usage of these functions should be fairly straightforward, however it may
79be asked why not use the other logging facilities, such as C standard stdio
80functions or C++ streams. The short answer is that they're all very good
81generic mechanisms, but are not really adapted for wxWidgets, while the log
82classes are. Some of advantages in using wxWidgets log functions are:
83
84@li @b Portability: It is a common practice to use @e printf() statements or
85 cout/cerr C++ streams for writing out some (debug or otherwise)
86 information. Although it works just fine under Unix, these messages go
87 strictly nowhere under Windows where the stdout of GUI programs is not
88 assigned to anything. Thus, you might view wxLogMessage() as a simple
89 substitute for @e printf().
90 You can also redirect the @e wxLogXXX calls to @e cout by just writing:
91 @code
92 wxLog* logger = new wxLogStream(&cout);
93 wxLog::SetActiveTarget(logger);
94 @endcode
95 Finally, there is also a possibility to redirect the output sent to @e cout
96 to a wxTextCtrl by using the wxStreamToTextRedirector class.
97@li @b Flexibility: The output of wxLog functions can be redirected or
98 suppressed entirely based on their importance, which is either impossible
99 or difficult to do with traditional methods. For example, only error
100 messages, or only error messages and warnings might be logged, filtering
101 out all informational messages.
102@li @b Completeness: Usually, an error message should be presented to the user
103 when some operation fails. Let's take a quite simple but common case of a
104 file error: suppose that you're writing your data file on disk and there is
105 not enough space. The actual error might have been detected inside
106 wxWidgets code (say, in wxFile::Write), so the calling function doesn't
107 really know the exact reason of the failure, it only knows that the data
108 file couldn't be written to the disk. However, as wxWidgets uses
109 wxLogError() in this situation, the exact error code (and the corresponding
110 error message) will be given to the user together with "high level" message
111 about data file writing error.
112
9ad2fe62 113
c602c59b
VZ
114@section overview_log_enable Log Messages Selection
115
116By default, most log messages are enabled. In particular, this means that
117errors logged by wxWidgets code itself (e.g. when it fails to perform some
118operation, for instance wxFile::Open() logs an error when it fails to open a
119file) will be processed and shown to the user. To disable the logging entirely
120you can use wxLog::EnableLogging() method or, more usually, wxLogNull class
121which temporarily disables logging and restores it back to the original setting
122when it is destroyed.
123
124To limit logging to important messages only, you may use wxLog::SetLogLevel()
125with e.g. wxLOG_Warning value -- this will completely disable all logging
126messages with the severity less than warnings, so wxLogMessage() output won't
127be shown to the user any more.
128
129Moreover, the log level can be set separately for different log components.
130Before showing how this can be useful, let us explain what log components are:
131they are simply arbitrary strings identifying the component, or module, which
132generated the message. They are hierarchical in the sense that "foo/bar/baz"
133component is supposed to be a child of "foo". And all components are children
134of the unnamed root component.
135
136By default, all messages logged by wxWidgets originate from "wx" component or
137one of its subcomponents such as "wx/net/ftp", while the messages logged by
138your own code are assigned empty log component. To change this, you need to
139define @c wxLOG_COMPONENT to a string uniquely identifying each component, e.g.
140you could give it the value "MyProgram" by default and re-define it as
141"MyProgram/DB" in the module working with the database and "MyProgram/DB/Trans"
142in its part managing the transactions. Then you could use
143wxLog::SetComponentLevel() in the following ways:
144 @code
145 // disable all database error messages, everybody knows databases never
146 // fail anyhow
147 wxLog::SetComponentLevel("MyProgram/DB", wxLOG_FatalError);
148
149 // but enable tracing for the transactions as somehow our changes don't
150 // get committed sometimes
151 wxLog::SetComponentLevel("MyProgram/DB/Trans", wxLOG_Trace);
152
153 // also enable tracing messages from wxWidgets dynamic module loading
154 // mechanism
155 wxLog::SetComponentLevel("wx/base/module", wxLOG_Trace);
156 @endcode
157Notice that the log level set explicitly for the transactions code overrides
158the log level of the parent component but that all other database code
159subcomponents inherit its setting by default and so won't generate any log
160messages at all.
161
9ad2fe62
VZ
162@section overview_log_targets Log Targets
163
9715cf42 164After having enumerated all the functions which are normally used to log the
c602c59b 165messages, and why would you want to use them, we now describe how all this
9715cf42
BP
166works.
167
168wxWidgets has the notion of a <em>log target</em>: it is just a class deriving
169from wxLog. As such, it implements the virtual functions of the base class
170which are called when a message is logged. Only one log target is @e active at
54e280d8
FM
171any moment, this is the one used by @ref group_funcmacro_log "wxLogXXX() functions".
172The normal usage of a log object (i.e. object of a class derived from wxLog) is
173to install it as the active target with a call to @e SetActiveTarget() and it will be used
174automatically by all subsequent calls to @ref group_funcmacro_log "wxLogXXX() functions".
9715cf42
BP
175
176To create a new log target class you only need to derive it from wxLog and
af588446
VZ
177override one or several of wxLog::DoLogRecord(), wxLog::DoLogTextAtLevel() and
178wxLog::DoLogText() in it. The first one is the most flexible and allows you to
179change the formatting of the messages, dynamically filter and redirect them and
180so on -- all log messages, except for those generated by wxLogFatalError(),
181pass by this function. wxLog::DoLogTextAtLevel() should be overridden if you
182simply want to redirect the log messages somewhere else, without changing their
183formatting. Finally, it is enough to override wxLog::DoLogText() if you only
184want to redirect the log messages and the destination doesn't depend on the
185message log level.
186
9715cf42
BP
187
188There are some predefined classes deriving from wxLog and which might be
189helpful to see how you can create a new log target class and, of course, may
190also be used without any change. There are:
191
192@li wxLogStderr: This class logs messages to a <tt>FILE *</tt>, using stderr by
193 default as its name suggests.
194@li wxLogStream: This class has the same functionality as wxLogStderr, but uses
195 @e ostream and cerr instead of <tt>FILE *</tt> and stderr.
196@li wxLogGui: This is the standard log target for wxWidgets applications (it is
197 used by default if you don't do anything) and provides the most reasonable
198 handling of all types of messages for given platform.
199@li wxLogWindow: This log target provides a "log console" which collects all
200 messages generated by the application and also passes them to the previous
201 active log target. The log window frame has a menu allowing user to clear
202 the log, close it completely or save all messages to file.
203@li wxLogBuffer: This target collects all the logged messages in an internal
204 buffer allowing to show them later to the user all at once.
205@li wxLogNull: The last log class is quite particular: it doesn't do anything.
206 The objects of this class may be instantiated to (temporarily) suppress
207 output of @e wxLogXXX() functions. As an example, trying to open a
208 non-existing file will usually provoke an error message, but if for some
209 reasons it is unwanted, just use this construction:
210 @code
211 wxFile file;
212
213 // wxFile.Open() normally complains if file can't be opened, we don't want it
214 {
215 wxLogNull logNo;
216 if ( !file.Open("bar") )
217 {
218 // ... process error ourselves ...
219 }
220 } // ~wxLogNull called, old log sink restored
221
222 wxLogMessage("..."); // ok
223 @endcode
224
225The log targets can also be combined: for example you may wish to redirect the
226messages somewhere else (for example, to a log file) but also process them as
227normally. For this the wxLogChain, wxLogInterposer, and wxLogInterposerTemp can
228be used.
229
9ad2fe62 230
54e280d8
FM
231@section overview_log_mt Logging in Multi-Threaded Applications
232
233Starting with wxWidgets 2.9.1, logging functions can be safely called from any
234thread. Messages logged from threads other than the main one will be buffered
235until wxLog::Flush() is called in the main thread (which usually happens during
236idle time, i.e. after processing all pending events) and will be really output
237only then. Notice that the default GUI logger already only output the messages
238when it is flushed, so by default messages from the other threads will be shown
239more or less at the same moment as usual. However if you define a custom log
240target, messages may be logged out of order, e.g. messages from the main thread
241with later timestamp may appear before messages with earlier timestamp logged
242from other threads. wxLog does however guarantee that messages logged by each
243thread will appear in order in which they were logged.
244
245Also notice that wxLog::EnableLogging() and wxLogNull class which uses it only
246affect the current thread, i.e. logging messages may still be generated by the
247other threads after a call to @c EnableLogging(false).
248
249
9ad2fe62
VZ
250@section overview_log_customize Logging Customization
251
252To completely change the logging behaviour you may define a custom log target.
253For example, you could define a class inheriting from wxLog which shows all the
254log messages in some part of your main application window reserved for the
255message output without interrupting the user work flow with modal message
256boxes.
257
258To use your custom log target you may either call wxLog::SetActiveTarget() with
259your custom log object or create a wxAppTraits-derived class and override
54e280d8 260wxAppTraits::CreateLogTarget() virtual method in it and also override wxApp::CreateTraits()
9ad2fe62
VZ
261to return an instance of your custom traits object. Notice that in the latter
262case you should be prepared for logging messages early during the program
263startup and also during program shutdown so you shouldn't rely on existence of
264the main application window, for example. You can however safely assume that
265GUI is (already/still) available when your log target as used as wxWidgets
266automatically switches to using wxLogStderr if it isn't.
267
54e280d8
FM
268There are several methods which may be overridden in the derived class to
269customize log messages handling: wxLog::DoLogRecord(), wxLog::DoLogTextAtLevel()
270and wxLog::DoLogText().
271
272The last method is the simplest one: you should override it if you simply
273want to redirect the log output elsewhere, without taking into account the
274level of the message. If you do want to handle messages of different levels
275differently, then you should override wxLog::DoLogTextAtLevel().
276
4ffdb640
VZ
277Additionally, you can customize the way full log messages are constructed from
278the components (such as time stamp, source file information, logging thread ID
279and so on). This task is performed by wxLogFormatter class so you need to
280derive a custom class from it and override its Format() method to build the log
281messages in desired way. Notice that if you just need to modify (or suppress)
282the time stamp display, overriding FormatTime() is enough.
283
284Finally, if even more control over the output format is needed, then
285DoLogRecord() can be overridden as it allows to construct custom messages
54e280d8
FM
286depending on the log level or even do completely different things depending
287on the message severity (for example, throw away all messages except
288warnings and errors, show warnings on the screen and forward the error
289messages to the user's (or programmer's) cell phone -- maybe depending on
290whether the timestamp tells us if it is day or night in the current time
291zone).
292
293The @e dialog sample illustrates this approach by defining a custom log target
9ad2fe62
VZ
294customizing the dialog used by wxLogGui for the single messages.
295
232addd1 296
54e280d8 297@section overview_log_tracemasks Using trace masks
232addd1 298
54e280d8
FM
299Notice that the use of log trace masks is hardly necessary any longer in
300current wxWidgets version as the same effect can be achieved by using
301different log components for different log statements of any level. Please
302see @ref overview_log_enable for more information about the log components.
232addd1 303
54e280d8
FM
304The functions below allow some limited customization of wxLog behaviour
305without writing a new log target class (which, aside from being a matter of
306several minutes, allows you to do anything you want).
307The verbose messages are the trace messages which are not disabled in the
308release mode and are generated by wxLogVerbose().
309They are not normally shown to the user because they present little interest,
310but may be activated, for example, in order to help the user find some program
311problem.
312
313As for the (real) trace messages, their handling depends on the currently
314enabled trace masks: if wxLog::AddTraceMask() was called for the mask of the given
315message, it will be logged, otherwise nothing happens.
316
317For example,
318@code
319wxLogTrace( wxTRACE_OleCalls, "IFoo::Bar() called" );
320@endcode
321
322will log the message if it was preceded by:
323
324@code
695d5232 325wxLog::AddTraceMask( wxTRACE_OleCalls );
54e280d8
FM
326@endcode
327
328The standard trace masks are given in wxLogTrace() documentation.
329
9715cf42 330*/
36c9828f 331