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