]> git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/log.h
added Id keyword
[wxWidgets.git] / docs / doxygen / overviews / log.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: log
3 // Purpose: topic overview
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /*!
10
11 @page log_overview wxLog classes overview
12
13 Classes: #wxLog,
14
15 #wxLogStderr,
16
17 #wxLogStream,
18
19 #wxLogTextCtrl,
20
21 #wxLogWindow,
22
23 #wxLogGui,
24
25 #wxLogNull,
26
27 #wxLogBuffer,
28
29 #wxLogChain,
30
31 #wxLogInterposer,
32
33 #wxLogInterposerTemp,
34
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:
47
48
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
75 use a 3rd party program such as #DbgView
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.
83
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
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:
99
100
101 @code
102 wxLog *logger=new wxLogStream();
103 wxLog::SetActiveTarget(logger);
104 @endcode
105
106
107 Finally, there is also a possibility to redirect the output sent to @e cout
108 to a #wxTextCtrl by using the
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.
125
126
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:
147
148
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.
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.
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:
167
168 @code
169 wxFile file;
170
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
177
178 wxLogMessage("..."); // ok
179 @endcode
180
181
182
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.
187
188 */
189
190