]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
8ca28fb7 | 2 | // Name: wx/log.h |
c801d85f KB |
3 | // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs) |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
886dd7d2 VZ |
12 | #ifndef _WX_LOG_H_ |
13 | #define _WX_LOG_H_ | |
c801d85f | 14 | |
df5168c4 | 15 | #include "wx/defs.h" |
38830220 | 16 | |
0b4f47a3 DS |
17 | // ---------------------------------------------------------------------------- |
18 | // common constants for use in wxUSE_LOG/!wxUSE_LOG | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | // the trace masks have been superceded by symbolic trace constants, they're | |
22 | // for compatibility only andwill be removed soon - do NOT use them | |
23 | ||
24 | // meaning of different bits of the trace mask (which allows selectively | |
25 | // enable/disable some trace messages) | |
26 | #define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete) | |
27 | #define wxTraceMessages 0x0002 // trace window messages/X callbacks | |
28 | #define wxTraceResAlloc 0x0004 // trace GDI resource allocation | |
29 | #define wxTraceRefCount 0x0008 // trace various ref counting operations | |
30 | ||
31 | #ifdef __WXMSW__ | |
32 | #define wxTraceOleCalls 0x0100 // OLE interface calls | |
33 | #endif | |
34 | ||
546db2a8 VZ |
35 | // ---------------------------------------------------------------------------- |
36 | // types | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
1782be31 | 39 | // NB: these types are needed even if wxUSE_LOG == 0 |
546db2a8 VZ |
40 | typedef unsigned long wxTraceMask; |
41 | typedef unsigned long wxLogLevel; | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
44 | // headers | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
b6e4e44a | 47 | #include "wx/string.h" |
c9f78968 | 48 | #include "wx/strvararg.h" |
b6e4e44a | 49 | |
1782be31 VZ |
50 | #if wxUSE_LOG |
51 | ||
1782be31 VZ |
52 | #include "wx/arrstr.h" |
53 | ||
0e0126c2 | 54 | #ifndef __WXWINCE__ |
1782be31 | 55 | #include <time.h> // for time_t |
0e0126c2 | 56 | #endif |
c30aaf75 VZ |
57 | |
58 | #include "wx/dynarray.h" | |
d6b9496a | 59 | |
edc73852 RD |
60 | #ifndef wxUSE_LOG_DEBUG |
61 | # ifdef __WXDEBUG__ | |
62 | # define wxUSE_LOG_DEBUG 1 | |
63 | # else // !__WXDEBUG__ | |
64 | # define wxUSE_LOG_DEBUG 0 | |
65 | # endif | |
66 | #endif | |
67 | ||
1782be31 VZ |
68 | // ---------------------------------------------------------------------------- |
69 | // forward declarations | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | #if wxUSE_GUI | |
73 | class WXDLLIMPEXP_CORE wxTextCtrl; | |
74 | class WXDLLIMPEXP_CORE wxLogFrame; | |
75 | class WXDLLIMPEXP_CORE wxFrame; | |
55c9a186 | 76 | class WXDLLIMPEXP_CORE wxWindow; |
1782be31 VZ |
77 | #endif // wxUSE_GUI |
78 | ||
9ef3052c VZ |
79 | // ---------------------------------------------------------------------------- |
80 | // constants | |
81 | // ---------------------------------------------------------------------------- | |
82 | ||
83 | // different standard log levels (you may also define your own) | |
90adbcca | 84 | enum wxLogLevelValues |
9ef3052c | 85 | { |
d6b9496a VZ |
86 | wxLOG_FatalError, // program can't continue, abort immediately |
87 | wxLOG_Error, // a serious error, user must be informed about it | |
88 | wxLOG_Warning, // user is normally informed about it but may be ignored | |
89 | wxLOG_Message, // normal message (i.e. normal output of a non GUI app) | |
d6b9496a | 90 | wxLOG_Status, // informational: might go to the status line of GUI app |
edc73852 | 91 | wxLOG_Info, // informational message (a.k.a. 'Verbose') |
d6b9496a VZ |
92 | wxLOG_Debug, // never shown to the user, disabled in release mode |
93 | wxLOG_Trace, // trace messages are also only enabled in debug mode | |
94 | wxLOG_Progress, // used for progress indicator (not yet) | |
edc73852 | 95 | wxLOG_User = 100, // user defined levels start here |
65ca8c0b | 96 | wxLOG_Max = 10000 |
9ef3052c VZ |
97 | }; |
98 | ||
d6b9496a VZ |
99 | // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be |
100 | // discarded unless the string "foo" has been added to the list of allowed | |
101 | // ones with AddTraceMask() | |
102 | ||
08298395 OK |
103 | #define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete) |
104 | #define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks | |
105 | #define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation | |
106 | #define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations | |
d6b9496a VZ |
107 | |
108 | #ifdef __WXMSW__ | |
08298395 | 109 | #define wxTRACE_OleCalls wxT("ole") // OLE interface calls |
d6b9496a VZ |
110 | #endif |
111 | ||
65f19af1 | 112 | #include "wx/iosfwrap.h" |
470b7da3 | 113 | |
c801d85f KB |
114 | // ---------------------------------------------------------------------------- |
115 | // derive from this class to redirect (or suppress, or ...) log messages | |
116 | // normally, only a single instance of this class exists but it's not enforced | |
c801d85f | 117 | // ---------------------------------------------------------------------------- |
d6b9496a | 118 | |
bddd7a8d | 119 | class WXDLLIMPEXP_BASE wxLog |
c801d85f KB |
120 | { |
121 | public: | |
d6b9496a | 122 | // ctor |
6fb99eb3 | 123 | wxLog(){} |
d6b9496a VZ |
124 | |
125 | // these functions allow to completely disable all log messages | |
d1b20379 DS |
126 | |
127 | // is logging disabled now? | |
d6b9496a | 128 | static bool IsEnabled() { return ms_doLog; } |
d1b20379 DS |
129 | |
130 | // change the flag state, return the previous one | |
131 | static bool EnableLogging(bool doIt = true) | |
2e7f3845 | 132 | { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; } |
d6b9496a VZ |
133 | |
134 | // static sink function - see DoLog() for function to overload in the | |
135 | // derived classes | |
5a20d2ce | 136 | static void OnLog(wxLogLevel level, const wxString& szString, time_t t); |
d6b9496a VZ |
137 | |
138 | // message buffering | |
d1b20379 DS |
139 | |
140 | // flush shows all messages if they're not logged immediately (FILE | |
141 | // and iostream logs don't need it, but wxGuiLog does to avoid showing | |
142 | // 17 modal dialogs one after another) | |
d6b9496a | 143 | virtual void Flush(); |
d6b9496a | 144 | |
d1b20379 | 145 | // flush the active target if any |
bbfa0322 VZ |
146 | static void FlushActive() |
147 | { | |
2ed3265e VZ |
148 | if ( !ms_suspendCount ) |
149 | { | |
150 | wxLog *log = GetActiveTarget(); | |
1ec5cbf3 | 151 | if ( log ) |
2ed3265e VZ |
152 | log->Flush(); |
153 | } | |
bbfa0322 | 154 | } |
1ec5cbf3 VZ |
155 | |
156 | // only one sink is active at each moment | |
d1b20379 DS |
157 | // get current log target, will call wxApp::CreateLogTarget() to |
158 | // create one if none exists | |
d6b9496a | 159 | static wxLog *GetActiveTarget(); |
d1b20379 DS |
160 | |
161 | // change log target, pLogger may be NULL | |
d6b9496a VZ |
162 | static wxLog *SetActiveTarget(wxLog *pLogger); |
163 | ||
d1b20379 DS |
164 | // suspend the message flushing of the main target until the next call |
165 | // to Resume() - this is mainly for internal use (to prevent wxYield() | |
166 | // from flashing the messages) | |
2ed3265e | 167 | static void Suspend() { ms_suspendCount++; } |
d1b20379 DS |
168 | |
169 | // must be called for each Suspend()! | |
2ed3265e VZ |
170 | static void Resume() { ms_suspendCount--; } |
171 | ||
d6b9496a | 172 | // functions controlling the default wxLog behaviour |
d1b20379 DS |
173 | // verbose mode is activated by standard command-line '-verbose' |
174 | // option | |
175 | static void SetVerbose(bool bVerbose = true) { ms_bVerbose = bVerbose; } | |
edc73852 | 176 | |
d1b20379 | 177 | // Set log level. Log messages with level > logLevel will not be logged. |
edc73852 RD |
178 | static void SetLogLevel(wxLogLevel logLevel) { ms_logLevel = logLevel; } |
179 | ||
d1b20379 DS |
180 | // should GetActiveTarget() try to create a new log object if the |
181 | // current is NULL? | |
36bd6902 | 182 | static void DontCreateOnDemand(); |
d6b9496a | 183 | |
e94cd97d DE |
184 | // Make GetActiveTarget() create a new log object again. |
185 | static void DoCreateOnDemand(); | |
186 | ||
f9837791 VZ |
187 | // log the count of repeating messages instead of logging the messages |
188 | // multiple times | |
189 | static void SetRepetitionCounting(bool bRepetCounting = true) | |
2e7f3845 | 190 | { ms_bRepetCounting = bRepetCounting; } |
f9837791 VZ |
191 | |
192 | // gets duplicate counting status | |
193 | static bool GetRepetitionCounting() { return ms_bRepetCounting; } | |
194 | ||
d1b20379 | 195 | // trace mask (see wxTraceXXX constants for details) |
d6b9496a | 196 | static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; } |
d1b20379 DS |
197 | |
198 | // add string trace mask | |
df5168c4 | 199 | static void AddTraceMask(const wxString& str) |
2e7f3845 | 200 | { ms_aTraceMasks.push_back(str); } |
d1b20379 DS |
201 | |
202 | // add string trace mask | |
d6b9496a | 203 | static void RemoveTraceMask(const wxString& str); |
d1b20379 DS |
204 | |
205 | // remove all string trace masks | |
36bd6902 | 206 | static void ClearTraceMasks(); |
d1b20379 DS |
207 | |
208 | // get string trace masks | |
0e080be6 | 209 | static const wxArrayString &GetTraceMasks() { return ms_aTraceMasks; } |
d6b9496a | 210 | |
d1b20379 DS |
211 | // sets the timestamp string: this is used as strftime() format string |
212 | // for the log targets which add time stamps to the messages - set it | |
213 | // to NULL to disable time stamping completely. | |
d993e05b | 214 | static void SetTimestamp(const wxString& ts) { ms_timestamp = ts; } |
d2e1ef19 | 215 | |
edc73852 | 216 | |
d6b9496a | 217 | // accessors |
d1b20379 DS |
218 | |
219 | // gets the verbose status | |
fd7718b2 | 220 | static bool GetVerbose() { return ms_bVerbose; } |
d1b20379 DS |
221 | |
222 | // get trace mask | |
d6b9496a | 223 | static wxTraceMask GetTraceMask() { return ms_ulTraceMask; } |
d1b20379 DS |
224 | |
225 | // is this trace mask in the list? | |
5a20d2ce | 226 | static bool IsAllowedTraceMask(const wxString& mask); |
d1b20379 DS |
227 | |
228 | // return the current loglevel limit | |
edc73852 | 229 | static wxLogLevel GetLogLevel() { return ms_logLevel; } |
d6b9496a | 230 | |
d1b20379 | 231 | // get the current timestamp format string (may be NULL) |
d993e05b | 232 | static const wxString& GetTimestamp() { return ms_timestamp; } |
d2e1ef19 | 233 | |
edc73852 | 234 | |
d2e1ef19 | 235 | // helpers |
d1b20379 DS |
236 | |
237 | // put the time stamp into the string if ms_timestamp != NULL (don't | |
238 | // change it otherwise) | |
d2e1ef19 VZ |
239 | static void TimeStamp(wxString *str); |
240 | ||
d6b9496a | 241 | // make dtor virtual for all derived classes |
f9837791 | 242 | virtual ~wxLog(); |
c801d85f | 243 | |
c801d85f | 244 | |
1ec5cbf3 | 245 | // this method exists for backwards compatibility only, don't use |
d1b20379 | 246 | bool HasPendingMessages() const { return true; } |
1ec5cbf3 | 247 | |
2e7f3845 VZ |
248 | #if WXWIN_COMPATIBILITY_2_6 |
249 | // this function doesn't do anything any more, don't call it | |
250 | wxDEPRECATED( static wxChar *SetLogBuffer(wxChar *buf, size_t size = 0) ); | |
251 | #endif | |
252 | ||
1ec5cbf3 | 253 | protected: |
d6b9496a | 254 | // the logging functions that can be overriden |
d1b20379 DS |
255 | |
256 | // default DoLog() prepends the time stamp and a prefix corresponding | |
257 | // to the message to szString and then passes it to DoLogString() | |
5a20d2ce VS |
258 | virtual void DoLog(wxLogLevel level, const wxString& szString, time_t t); |
259 | #if WXWIN_COMPATIBILITY_2_8 | |
260 | // these shouldn't be used by new code | |
5d88a6b5 VZ |
261 | wxDEPRECATED_BUT_USED_INTERNALLY( |
262 | virtual void DoLog(wxLogLevel level, const char *szString, time_t t) | |
263 | ); | |
264 | ||
265 | wxDEPRECATED_BUT_USED_INTERNALLY( | |
266 | virtual void DoLog(wxLogLevel level, const wchar_t *wzString, time_t t) | |
267 | ); | |
268 | #endif // WXWIN_COMPATIBILITY_2_8 | |
5a20d2ce VS |
269 | |
270 | void LogString(const wxString& szString, time_t t) | |
271 | { DoLogString(szString, t); } | |
d1b20379 DS |
272 | |
273 | // default DoLogString does nothing but is not pure virtual because if | |
274 | // you override DoLog() you might not need it at all | |
5a20d2ce VS |
275 | virtual void DoLogString(const wxString& szString, time_t t); |
276 | #if WXWIN_COMPATIBILITY_2_8 | |
277 | // these shouldn't be used by new code | |
278 | virtual void DoLogString(const char *WXUNUSED(szString), | |
279 | time_t WXUNUSED(t)) {} | |
280 | virtual void DoLogString(const wchar_t *WXUNUSED(szString), | |
281 | time_t WXUNUSED(t)) {} | |
b99891b0 VZ |
282 | #endif // WXWIN_COMPATIBILITY_2_8 |
283 | ||
284 | // this macro should be used in the derived classes to avoid warnings about | |
285 | // hiding the other DoLog() overloads when overriding DoLog(wxString) -- | |
286 | // but don't use it with MSVC which doesn't give this warning but does give | |
287 | // warning when a deprecated function is overridden | |
288 | #if WXWIN_COMPATIBILITY_2_8 && !defined(__VISUALC__) | |
289 | #define wxSUPPRESS_DOLOG_HIDE_WARNING() \ | |
290 | virtual void DoLog(wxLogLevel, const char *, time_t) { } \ | |
291 | virtual void DoLog(wxLogLevel, const wchar_t *, time_t) { } | |
292 | ||
293 | #define wxSUPPRESS_DOLOGSTRING_HIDE_WARNING() \ | |
294 | virtual void DoLogString(const char *, time_t) { } \ | |
295 | virtual void DoLogString(const wchar_t *, time_t) { } | |
296 | #else | |
297 | #define wxSUPPRESS_DOLOG_HIDE_WARNING() | |
298 | #define wxSUPPRESS_DOLOGSTRING_HIDE_WARNING() | |
5a20d2ce | 299 | #endif |
c801d85f | 300 | |
f9837791 VZ |
301 | // log a line containing the number of times the previous message was |
302 | // repeated | |
303 | // returns: the number | |
304 | static unsigned DoLogNumberOfRepeats(); | |
305 | ||
d6b9496a VZ |
306 | private: |
307 | // static variables | |
308 | // ---------------- | |
06db8ebd | 309 | |
f9837791 VZ |
310 | // traditional behaviour or counting repetitions |
311 | static bool ms_bRepetCounting; | |
312 | static wxString ms_prevString; // previous message that was logged | |
313 | // how many times the previous message was logged | |
314 | static unsigned ms_prevCounter; | |
315 | static time_t ms_prevTimeStamp;// timestamp of the previous message | |
316 | static wxLogLevel ms_prevLevel; // level of the previous message | |
317 | ||
d6b9496a | 318 | static wxLog *ms_pLogger; // currently active log sink |
d1b20379 | 319 | static bool ms_doLog; // false => all logging disabled |
d6b9496a | 320 | static bool ms_bAutoCreate; // create new log targets on demand? |
d1b20379 | 321 | static bool ms_bVerbose; // false => ignore LogInfo messages |
fe7b1156 | 322 | |
edc73852 RD |
323 | static wxLogLevel ms_logLevel; // limit logging to levels <= ms_logLevel |
324 | ||
2ed3265e VZ |
325 | static size_t ms_suspendCount; // if positive, logs are not flushed |
326 | ||
d2e1ef19 VZ |
327 | // format string for strftime(), if NULL, time stamping log messages is |
328 | // disabled | |
d993e05b | 329 | static wxString ms_timestamp; |
d2e1ef19 | 330 | |
d6b9496a VZ |
331 | static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour |
332 | static wxArrayString ms_aTraceMasks; // more powerful filter for wxLogTrace | |
c801d85f KB |
333 | }; |
334 | ||
335 | // ---------------------------------------------------------------------------- | |
336 | // "trivial" derivations of wxLog | |
337 | // ---------------------------------------------------------------------------- | |
338 | ||
d3fc1755 VZ |
339 | // log everything to a buffer |
340 | class WXDLLIMPEXP_BASE wxLogBuffer : public wxLog | |
341 | { | |
342 | public: | |
343 | wxLogBuffer() { } | |
344 | ||
345 | // get the string contents with all messages logged | |
346 | const wxString& GetBuffer() const { return m_str; } | |
347 | ||
348 | // show the buffer contents to the user in the best possible way (this uses | |
349 | // wxMessageOutputMessageBox) and clear it | |
350 | virtual void Flush(); | |
351 | ||
352 | protected: | |
5a20d2ce VS |
353 | virtual void DoLog(wxLogLevel level, const wxString& szString, time_t t); |
354 | virtual void DoLogString(const wxString& szString, time_t t); | |
d3fc1755 | 355 | |
b99891b0 VZ |
356 | wxSUPPRESS_DOLOG_HIDE_WARNING() |
357 | wxSUPPRESS_DOLOGSTRING_HIDE_WARNING() | |
358 | ||
d3fc1755 VZ |
359 | private: |
360 | wxString m_str; | |
361 | ||
362 | DECLARE_NO_COPY_CLASS(wxLogBuffer) | |
363 | }; | |
364 | ||
0f8218d7 | 365 | |
c801d85f | 366 | // log everything to a "FILE *", stderr by default |
bddd7a8d | 367 | class WXDLLIMPEXP_BASE wxLogStderr : public wxLog |
c801d85f KB |
368 | { |
369 | public: | |
d6b9496a VZ |
370 | // redirect log output to a FILE |
371 | wxLogStderr(FILE *fp = (FILE *) NULL); | |
c801d85f | 372 | |
03147cd0 | 373 | protected: |
d6b9496a | 374 | // implement sink function |
5a20d2ce | 375 | virtual void DoLogString(const wxString& szString, time_t t); |
c801d85f | 376 | |
b99891b0 VZ |
377 | wxSUPPRESS_DOLOGSTRING_HIDE_WARNING() |
378 | ||
d6b9496a | 379 | FILE *m_fp; |
d3fc1755 VZ |
380 | |
381 | DECLARE_NO_COPY_CLASS(wxLogStderr) | |
c801d85f KB |
382 | }; |
383 | ||
4bf78aae | 384 | #if wxUSE_STD_IOSTREAM |
03147cd0 | 385 | |
c801d85f | 386 | // log everything to an "ostream", cerr by default |
bddd7a8d | 387 | class WXDLLIMPEXP_BASE wxLogStream : public wxLog |
c801d85f KB |
388 | { |
389 | public: | |
d6b9496a | 390 | // redirect log output to an ostream |
dd107c50 | 391 | wxLogStream(wxSTD ostream *ostr = (wxSTD ostream *) NULL); |
c801d85f KB |
392 | |
393 | protected: | |
d6b9496a | 394 | // implement sink function |
5a20d2ce | 395 | virtual void DoLogString(const wxString& szString, time_t t); |
c801d85f | 396 | |
b99891b0 VZ |
397 | wxSUPPRESS_DOLOGSTRING_HIDE_WARNING() |
398 | ||
d6b9496a | 399 | // using ptr here to avoid including <iostream.h> from this file |
dd107c50 | 400 | wxSTD ostream *m_ostr; |
c801d85f | 401 | }; |
03147cd0 VZ |
402 | |
403 | #endif // wxUSE_STD_IOSTREAM | |
c801d85f | 404 | |
03147cd0 VZ |
405 | // ---------------------------------------------------------------------------- |
406 | // /dev/null log target: suppress logging until this object goes out of scope | |
407 | // ---------------------------------------------------------------------------- | |
408 | ||
409 | // example of usage: | |
410 | /* | |
411 | void Foo() | |
412 | { | |
413 | wxFile file; | |
414 | ||
415 | // wxFile.Open() normally complains if file can't be opened, we don't | |
416 | // want it | |
417 | wxLogNull logNo; | |
418 | ||
419 | if ( !file.Open("bar") ) | |
420 | ... process error ourselves ... | |
421 | ||
422 | // ~wxLogNull called, old log sink restored | |
423 | } | |
424 | */ | |
bddd7a8d | 425 | class WXDLLIMPEXP_BASE wxLogNull |
03147cd0 VZ |
426 | { |
427 | public: | |
d1b20379 | 428 | wxLogNull() : m_flagOld(wxLog::EnableLogging(false)) { } |
be52b341 | 429 | ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); } |
03147cd0 VZ |
430 | |
431 | private: | |
432 | bool m_flagOld; // the previous value of the wxLog::ms_doLog | |
433 | }; | |
434 | ||
435 | // ---------------------------------------------------------------------------- | |
436 | // chaining log target: installs itself as a log target and passes all | |
437 | // messages to the real log target given to it in the ctor but also forwards | |
438 | // them to the previously active one | |
439 | // | |
440 | // note that you don't have to call SetActiveTarget() with this class, it | |
441 | // does it itself in its ctor | |
442 | // ---------------------------------------------------------------------------- | |
443 | ||
bddd7a8d | 444 | class WXDLLIMPEXP_BASE wxLogChain : public wxLog |
03147cd0 VZ |
445 | { |
446 | public: | |
447 | wxLogChain(wxLog *logger); | |
8b30a4e4 | 448 | virtual ~wxLogChain(); |
03147cd0 VZ |
449 | |
450 | // change the new log target | |
451 | void SetLog(wxLog *logger); | |
452 | ||
453 | // this can be used to temporarily disable (and then reenable) passing | |
454 | // messages to the old logger (by default we do pass them) | |
455 | void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; } | |
456 | ||
457 | // are we passing the messages to the previous log target? | |
458 | bool IsPassingMessages() const { return m_bPassMessages; } | |
459 | ||
460 | // return the previous log target (may be NULL) | |
461 | wxLog *GetOldLog() const { return m_logOld; } | |
462 | ||
463 | // override base class version to flush the old logger as well | |
464 | virtual void Flush(); | |
465 | ||
47fe7ff3 JS |
466 | // call to avoid destroying the old log target |
467 | void DetachOldLog() { m_logOld = NULL; } | |
468 | ||
03147cd0 VZ |
469 | protected: |
470 | // pass the chain to the old logger if needed | |
5a20d2ce | 471 | virtual void DoLog(wxLogLevel level, const wxString& szString, time_t t); |
03147cd0 | 472 | |
b99891b0 VZ |
473 | wxSUPPRESS_DOLOG_HIDE_WARNING() |
474 | ||
03147cd0 VZ |
475 | private: |
476 | // the current log target | |
477 | wxLog *m_logNew; | |
478 | ||
479 | // the previous log target | |
480 | wxLog *m_logOld; | |
481 | ||
482 | // do we pass the messages to the old logger? | |
483 | bool m_bPassMessages; | |
22f3361e VZ |
484 | |
485 | DECLARE_NO_COPY_CLASS(wxLogChain) | |
03147cd0 VZ |
486 | }; |
487 | ||
488 | // a chain log target which uses itself as the new logger | |
47fe7ff3 JS |
489 | |
490 | #define wxLogPassThrough wxLogInterposer | |
491 | ||
492 | class WXDLLIMPEXP_BASE wxLogInterposer : public wxLogChain | |
493 | { | |
494 | public: | |
495 | wxLogInterposer(); | |
496 | ||
497 | private: | |
498 | DECLARE_NO_COPY_CLASS(wxLogInterposer) | |
499 | }; | |
500 | ||
501 | // a temporary interposer which doesn't destroy the old log target | |
502 | // (calls DetachOldLog) | |
503 | ||
504 | class WXDLLIMPEXP_BASE wxLogInterposerTemp : public wxLogChain | |
03147cd0 VZ |
505 | { |
506 | public: | |
47fe7ff3 | 507 | wxLogInterposerTemp(); |
fc7a2a60 VZ |
508 | |
509 | private: | |
47fe7ff3 | 510 | DECLARE_NO_COPY_CLASS(wxLogInterposerTemp) |
03147cd0 VZ |
511 | }; |
512 | ||
8ca28fb7 | 513 | #if wxUSE_GUI |
7e8c564c VS |
514 | // include GUI log targets: |
515 | #include "wx/generic/logg.h" | |
e90c1d2a | 516 | #endif // wxUSE_GUI |
03f38c58 | 517 | |
c801d85f KB |
518 | // ============================================================================ |
519 | // global functions | |
520 | // ============================================================================ | |
521 | ||
522 | // ---------------------------------------------------------------------------- | |
523 | // Log functions should be used by application instead of stdio, iostream &c | |
524 | // for log messages for easy redirection | |
525 | // ---------------------------------------------------------------------------- | |
526 | ||
88ac883a VZ |
527 | // ---------------------------------------------------------------------------- |
528 | // get error code/error message from system in a portable way | |
529 | // ---------------------------------------------------------------------------- | |
530 | ||
531 | // return the last system error code | |
bddd7a8d | 532 | WXDLLIMPEXP_BASE unsigned long wxSysErrorCode(); |
c11d62a6 | 533 | |
88ac883a | 534 | // return the error message for given (or last if 0) error code |
bddd7a8d | 535 | WXDLLIMPEXP_BASE const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0); |
88ac883a | 536 | |
c11d62a6 | 537 | // ---------------------------------------------------------------------------- |
c801d85f | 538 | // define wxLog<level> |
c11d62a6 | 539 | // ---------------------------------------------------------------------------- |
c801d85f | 540 | |
2523e9b7 | 541 | #define DECLARE_LOG_FUNCTION(level) \ |
81727065 | 542 | extern void WXDLLIMPEXP_BASE \ |
d1f6e2cf VS |
543 | wxDoLog##level##Wchar(const wxChar *format, ...); \ |
544 | extern void WXDLLIMPEXP_BASE \ | |
545 | wxDoLog##level##Utf8(const char *format, ...); \ | |
2523e9b7 | 546 | WX_DEFINE_VARARG_FUNC_VOID(wxLog##level, \ |
1528e0b8 | 547 | 1, (const wxFormatString&), \ |
d1f6e2cf | 548 | wxDoLog##level##Wchar, wxDoLog##level##Utf8) \ |
2523e9b7 VS |
549 | DECLARE_LOG_FUNCTION_WATCOM(level) \ |
550 | extern void WXDLLIMPEXP_BASE wxVLog##level(const wxString& format, \ | |
551 | va_list argptr) | |
552 | ||
553 | #ifdef __WATCOMC__ | |
554 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351; | |
555 | // can't use WX_WATCOM_ONLY_CODE here because the macro would expand to | |
556 | // something too big for Borland C++ to handle | |
557 | #define DECLARE_LOG_FUNCTION_WATCOM(level) \ | |
59a14f69 VS |
558 | WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \ |
559 | 1, (const wxString&), \ | |
560 | (wxFormatString(f1))) \ | |
561 | WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \ | |
562 | 1, (const wxCStrData&), \ | |
563 | (wxFormatString(f1))) \ | |
564 | WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \ | |
d1f6e2cf | 565 | 1, (const char*), \ |
59a14f69 VS |
566 | (wxFormatString(f1))) \ |
567 | WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \ | |
d1f6e2cf | 568 | 1, (const wchar_t*), \ |
59a14f69 | 569 | (wxFormatString(f1))) |
2523e9b7 VS |
570 | #else |
571 | #define DECLARE_LOG_FUNCTION_WATCOM(level) | |
572 | #endif | |
573 | ||
c9f78968 | 574 | |
2523e9b7 | 575 | #define DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, expdecl) \ |
d1f6e2cf VS |
576 | extern void expdecl wxDoLog##level##Wchar(argclass arg, \ |
577 | const wxChar *format, ...); \ | |
578 | extern void expdecl wxDoLog##level##Utf8(argclass arg, \ | |
579 | const char *format, ...); \ | |
2523e9b7 | 580 | WX_DEFINE_VARARG_FUNC_VOID(wxLog##level, \ |
1528e0b8 | 581 | 2, (argclass, const wxFormatString&), \ |
d1f6e2cf | 582 | wxDoLog##level##Wchar, wxDoLog##level##Utf8) \ |
2523e9b7 | 583 | DECLARE_LOG_FUNCTION2_EXP_WATCOM(level, argclass, arg, expdecl) \ |
c9f78968 | 584 | extern void expdecl wxVLog##level(argclass arg, \ |
81727065 | 585 | const wxString& format, \ |
2523e9b7 VS |
586 | va_list argptr) |
587 | ||
588 | #ifdef __WATCOMC__ | |
589 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351; | |
590 | // can't use WX_WATCOM_ONLY_CODE here because the macro would expand to | |
591 | // something too big for Borland C++ to handle | |
592 | #define DECLARE_LOG_FUNCTION2_EXP_WATCOM(level, argclass, arg, expdecl) \ | |
59a14f69 VS |
593 | WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \ |
594 | 2, (argclass, const wxString&), \ | |
595 | (f1, wxFormatString(f2))) \ | |
596 | WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \ | |
597 | 2, (argclass, const wxCStrData&), \ | |
598 | (f1, wxFormatString(f2))) \ | |
599 | WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \ | |
2523e9b7 | 600 | 2, (argclass, const char*), \ |
59a14f69 VS |
601 | (f1, wxFormatString(f2))) \ |
602 | WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \ | |
2523e9b7 | 603 | 2, (argclass, const wchar_t*), \ |
59a14f69 | 604 | (f1, wxFormatString(f2))) |
2523e9b7 VS |
605 | #else |
606 | #define DECLARE_LOG_FUNCTION2_EXP_WATCOM(level, argclass, arg, expdecl) | |
607 | #endif | |
608 | ||
c9f78968 | 609 | |
88ac883a VZ |
610 | #else // !wxUSE_LOG |
611 | ||
82e77a80 VS |
612 | #ifdef __WATCOMC__ |
613 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
614 | #define WX_WATCOM_ONLY_CODE( x ) x | |
615 | #else | |
616 | #define WX_WATCOM_ONLY_CODE( x ) | |
617 | #endif | |
618 | ||
88ac883a | 619 | // log functions do nothing at all |
2523e9b7 VS |
620 | #define DECLARE_LOG_FUNCTION(level) \ |
621 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxString&)) \ | |
622 | WX_WATCOM_ONLY_CODE( \ | |
623 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const char*)) \ | |
624 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wchar_t*)) \ | |
625 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxCStrData&)) \ | |
626 | ) \ | |
81727065 | 627 | inline void wxVLog##level(const wxString& WXUNUSED(format), \ |
59a14f69 | 628 | va_list WXUNUSED(argptr)) { } \ |
c9f78968 | 629 | |
2523e9b7 VS |
630 | #define DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, expdecl) \ |
631 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxString&)) \ | |
632 | WX_WATCOM_ONLY_CODE( \ | |
633 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const char*)) \ | |
634 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wchar_t*)) \ | |
635 | WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxCStrData&)) \ | |
636 | ) \ | |
c9f78968 | 637 | inline void wxVLog##level(argclass WXUNUSED(arg), \ |
59a14f69 VS |
638 | const wxString& WXUNUSED(format), \ |
639 | va_list WXUNUSED(argptr)) {} | |
2523e9b7 | 640 | |
e30285ab | 641 | // Empty Class to fake wxLogNull |
bddd7a8d | 642 | class WXDLLIMPEXP_BASE wxLogNull |
e30285ab VZ |
643 | { |
644 | public: | |
886dd7d2 | 645 | wxLogNull() { } |
e30285ab VZ |
646 | }; |
647 | ||
648 | // Dummy macros to replace some functions. | |
649 | #define wxSysErrorCode() (unsigned long)0 | |
650 | #define wxSysErrorMsg( X ) (const wxChar*)NULL | |
651 | ||
652 | // Fake symbolic trace masks... for those that are used frequently | |
fda7962d | 653 | #define wxTRACE_OleCalls wxEmptyString // OLE interface calls |
e30285ab | 654 | |
88ac883a | 655 | #endif // wxUSE_LOG/!wxUSE_LOG |
bdeb1f0d | 656 | |
33936026 WS |
657 | #define DECLARE_LOG_FUNCTION2(level, argclass, arg) \ |
658 | DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, WXDLLIMPEXP_BASE) | |
0b4f47a3 | 659 | |
463c2e5b | 660 | // VC6 produces a warning if we a macro expanding to nothing to |
2523e9b7 | 661 | // DECLARE_LOG_FUNCTION2: |
463c2e5b | 662 | #if defined(__VISUALC__) && __VISUALC__ < 1300 |
2523e9b7 | 663 | // "not enough actual parameters for macro 'DECLARE_LOG_FUNCTION2_EXP'" |
463c2e5b VS |
664 | #pragma warning(disable:4003) |
665 | #endif | |
c801d85f | 666 | |
88ac883a | 667 | // a generic function for all levels (level is passes as parameter) |
33936026 | 668 | DECLARE_LOG_FUNCTION2(Generic, wxLogLevel, level); |
c801d85f | 669 | |
88ac883a VZ |
670 | // one function per each level |
671 | DECLARE_LOG_FUNCTION(FatalError); | |
672 | DECLARE_LOG_FUNCTION(Error); | |
673 | DECLARE_LOG_FUNCTION(Warning); | |
674 | DECLARE_LOG_FUNCTION(Message); | |
675 | DECLARE_LOG_FUNCTION(Info); | |
676 | DECLARE_LOG_FUNCTION(Verbose); | |
470b7da3 | 677 | |
88ac883a VZ |
678 | // this function sends the log message to the status line of the top level |
679 | // application frame, if any | |
2523e9b7 | 680 | DECLARE_LOG_FUNCTION(Status); |
470b7da3 | 681 | |
886dd7d2 VZ |
682 | #if wxUSE_GUI |
683 | // this one is the same as previous except that it allows to explicitly | |
b76069e2 | 684 | class WXDLLEXPORT wxFrame; |
886dd7d2 | 685 | // specify the frame to which the output should go |
2523e9b7 | 686 | DECLARE_LOG_FUNCTION2_EXP(Status, wxFrame *, pFrame, WXDLLIMPEXP_CORE); |
886dd7d2 | 687 | #endif // wxUSE_GUI |
c801d85f | 688 | |
88ac883a VZ |
689 | // additional one: as wxLogError, but also logs last system call error code |
690 | // and the corresponding error message if available | |
2523e9b7 | 691 | DECLARE_LOG_FUNCTION(SysError); |
c801d85f | 692 | |
88ac883a VZ |
693 | // and another one which also takes the error code (for those broken APIs |
694 | // that don't set the errno (like registry APIs in Win32)) | |
2523e9b7 VS |
695 | DECLARE_LOG_FUNCTION2(SysError, long, lErrCode); |
696 | #ifdef __WATCOMC__ | |
697 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
698 | DECLARE_LOG_FUNCTION2(SysError, unsigned long, lErrCode); | |
699 | #endif | |
88ac883a VZ |
700 | |
701 | // debug functions do nothing in release mode | |
bdeb1f0d | 702 | #if wxUSE_LOG && wxUSE_LOG_DEBUG |
d6b9496a VZ |
703 | DECLARE_LOG_FUNCTION(Debug); |
704 | ||
b103e4f3 VZ |
705 | // there is no more unconditional LogTrace: it is not different from |
706 | // LogDebug and it creates overload ambiguities | |
707 | //DECLARE_LOG_FUNCTION(Trace); | |
d6b9496a | 708 | |
b103e4f3 VZ |
709 | // this version only logs the message if the mask had been added to the |
710 | // list of masks with AddTraceMask() | |
2523e9b7 VS |
711 | DECLARE_LOG_FUNCTION2(Trace, const wxString&, mask); |
712 | #ifdef __WATCOMC__ | |
713 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
714 | DECLARE_LOG_FUNCTION2(Trace, const char*, mask); | |
715 | DECLARE_LOG_FUNCTION2(Trace, const wchar_t*, mask); | |
716 | #endif | |
717 | ||
b103e4f3 VZ |
718 | // and this one does nothing if all of level bits are not set in |
719 | // wxLog::GetActive()->GetTraceMask() -- it's deprecated in favour of | |
d6b9496a | 720 | // string identifiers |
2523e9b7 VS |
721 | DECLARE_LOG_FUNCTION2(Trace, wxTraceMask, mask); |
722 | #ifdef __WATCOMC__ | |
723 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
724 | DECLARE_LOG_FUNCTION2(Trace, int, mask); | |
725 | #endif | |
bdeb1f0d | 726 | #else //!debug || !wxUSE_LOG |
388a1f66 VZ |
727 | // these functions do nothing in release builds, but don't define them as |
728 | // nothing as it could result in different code structure in debug and | |
729 | // release and this could result in trouble when these macros are used | |
730 | // inside if/else | |
731 | // | |
732 | // note that making wxVLogDebug/Trace() themselves (empty inline) functions | |
733 | // is a bad idea as some compilers are stupid enough to not inline even | |
734 | // empty functions if their parameters are complicated enough, but by | |
735 | // defining them as an empty inline function we ensure that even dumbest | |
736 | // compilers optimise them away | |
737 | inline void wxLogNop() { } | |
738 | ||
739 | #define wxVLogDebug(fmt, valist) wxLogNop() | |
740 | #define wxVLogTrace(mask, fmt, valist) wxLogNop() | |
ca766534 VS |
741 | |
742 | #ifdef HAVE_VARIADIC_MACROS | |
388a1f66 VZ |
743 | // unlike the inline functions below, this completely removes the |
744 | // wxLogXXX calls from the object file: | |
745 | #define wxLogDebug(fmt, ...) wxLogNop() | |
746 | #define wxLogTrace(mask, fmt, ...) wxLogNop() | |
ca766534 | 747 | #else // !HAVE_VARIADIC_MACROS |
c9f78968 | 748 | //inline void wxLogDebug(const wxString& fmt, ...) {} |
44be939a | 749 | WX_DEFINE_VARARG_FUNC_NOP(wxLogDebug, 1, (const wxString&)) |
c9f78968 VS |
750 | //inline void wxLogTrace(wxTraceMask, const wxString& fmt, ...) {} |
751 | //inline void wxLogTrace(const wxString&, const wxString& fmt, ...) {} | |
44be939a VS |
752 | WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (wxTraceMask, const wxString&)) |
753 | WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const wxString&, const wxString&)) | |
754 | #ifdef __WATCOMC__ | |
755 | // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351 | |
756 | WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const char*, const char*)) | |
757 | WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const wchar_t*, const wchar_t*)) | |
758 | #endif | |
ca766534 | 759 | #endif // HAVE_VARIADIC_MACROS/!HAVE_VARIADIC_MACROS |
88ac883a | 760 | #endif // debug/!debug |
c801d85f | 761 | |
463c2e5b VS |
762 | #if defined(__VISUALC__) && __VISUALC__ < 1300 |
763 | #pragma warning(default:4003) | |
764 | #endif | |
765 | ||
c11d62a6 VZ |
766 | // wxLogFatalError helper: show the (fatal) error to the user in a safe way, |
767 | // i.e. without using wxMessageBox() for example because it could crash | |
bddd7a8d | 768 | void WXDLLIMPEXP_BASE |
886dd7d2 | 769 | wxSafeShowMessage(const wxString& title, const wxString& text); |
c11d62a6 | 770 | |
88ac883a VZ |
771 | // ---------------------------------------------------------------------------- |
772 | // debug only logging functions: use them with API name and error code | |
773 | // ---------------------------------------------------------------------------- | |
c801d85f | 774 | |
e90babdf | 775 | #ifdef __WXDEBUG__ |
42e69d6b VZ |
776 | // make life easier for people using VC++ IDE: clicking on the message |
777 | // will take us immediately to the place of the failed API | |
778 | #ifdef __VISUALC__ | |
4b7f2165 VZ |
779 | #define wxLogApiError(api, rc) \ |
780 | wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \ | |
0accd1cf | 781 | __FILE__, __LINE__, api, \ |
8b94d999 | 782 | (long)rc, wxSysErrorMsg(rc)) |
42e69d6b | 783 | #else // !VC++ |
4b7f2165 | 784 | #define wxLogApiError(api, rc) \ |
18da7cf2 VZ |
785 | wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \ |
786 | wxT("error 0x%08lx (%s)."), \ | |
0accd1cf | 787 | __FILE__, __LINE__, api, \ |
8b94d999 | 788 | (long)rc, wxSysErrorMsg(rc)) |
42e69d6b VZ |
789 | #endif // VC++/!VC++ |
790 | ||
791 | #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode()) | |
792 | ||
c801d85f | 793 | #else //!debug |
388a1f66 VZ |
794 | #define wxLogApiError(api, err) wxLogNop() |
795 | #define wxLogLastError(api) wxLogNop() | |
c801d85f KB |
796 | #endif //debug/!debug |
797 | ||
a619fa3f DE |
798 | // wxCocoa has additiional trace masks |
799 | #if defined(__WXCOCOA__) | |
800 | #include "wx/cocoa/log.h" | |
801 | #endif | |
802 | ||
82e77a80 VS |
803 | #ifdef WX_WATCOM_ONLY_CODE |
804 | #undef WX_WATCOM_ONLY_CODE | |
805 | #endif | |
806 | ||
34138703 | 807 | #endif // _WX_LOG_H_ |
04662def | 808 |