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