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