1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs)
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // NB: this is needed even if wxUSE_LOG == 0
22 typedef unsigned long wxLogLevel
;
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
33 #define wxTraceOleCalls 0x0100 // OLE interface calls
36 typedef unsigned long wxTraceMask
;
37 #endif // WXWIN_COMPATIBILITY_2_8
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #include "wx/string.h"
44 #include "wx/strvararg.h"
48 #include "wx/arrstr.h"
52 #include <time.h> // for time_t
54 #endif // ! __WXPALMOS5__
56 #include "wx/dynarray.h"
57 #include "wx/hashmap.h"
60 #include "wx/thread.h"
61 #endif // wxUSE_THREADS
63 // wxUSE_LOG_DEBUG enables the debug log messages
64 #ifndef wxUSE_LOG_DEBUG
66 #define wxUSE_LOG_DEBUG 1
67 #else // !wxDEBUG_LEVEL
68 #define wxUSE_LOG_DEBUG 0
72 // wxUSE_LOG_TRACE enables the trace messages, they are disabled by default
73 #ifndef wxUSE_LOG_TRACE
75 #define wxUSE_LOG_TRACE 1
76 #else // !wxDEBUG_LEVEL
77 #define wxUSE_LOG_TRACE 0
79 #endif // wxUSE_LOG_TRACE
81 // ----------------------------------------------------------------------------
82 // forward declarations
83 // ----------------------------------------------------------------------------
85 class WXDLLIMPEXP_FWD_BASE wxObject
;
88 class WXDLLIMPEXP_FWD_CORE wxFrame
;
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 // different standard log levels (you may also define your own)
98 wxLOG_FatalError
, // program can't continue, abort immediately
99 wxLOG_Error
, // a serious error, user must be informed about it
100 wxLOG_Warning
, // user is normally informed about it but may be ignored
101 wxLOG_Message
, // normal message (i.e. normal output of a non GUI app)
102 wxLOG_Status
, // informational: might go to the status line of GUI app
103 wxLOG_Info
, // informational message (a.k.a. 'Verbose')
104 wxLOG_Debug
, // never shown to the user, disabled in release mode
105 wxLOG_Trace
, // trace messages are also only enabled in debug mode
106 wxLOG_Progress
, // used for progress indicator (not yet)
107 wxLOG_User
= 100, // user defined levels start here
111 // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
112 // discarded unless the string "foo" has been added to the list of allowed
113 // ones with AddTraceMask()
115 #define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete)
116 #define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks
117 #define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation
118 #define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations
121 #define wxTRACE_OleCalls wxT("ole") // OLE interface calls
124 #include "wx/iosfwrap.h"
126 // ----------------------------------------------------------------------------
127 // information about a log record, i.e. unit of log output
128 // ----------------------------------------------------------------------------
130 class wxLogRecordInfo
133 // default ctor creates an uninitialized object
136 memset(this, 0, sizeof(this));
139 // normal ctor, used by wxLogger specifies the location of the log
140 // statement; its time stamp and thread id are set up here
141 wxLogRecordInfo(const char *filename_
,
145 filename
= filename_
;
149 timestamp
= time(NULL
);
152 threadId
= wxThread::GetCurrentId();
153 #endif // wxUSE_THREADS
158 // we need to define copy ctor and assignment operator because of m_data
159 wxLogRecordInfo(const wxLogRecordInfo
& other
)
164 wxLogRecordInfo
& operator=(const wxLogRecordInfo
& other
)
166 if ( &other
!= this )
175 // dtor is non-virtual, this class is not meant to be derived from
182 // the file name and line number of the file where the log record was
183 // generated, if available or NULL and 0 otherwise
184 const char *filename
;
187 // the name of the function where the log record was generated (may be NULL
188 // if the compiler doesn't support __FUNCTION__)
191 // time of record generation
195 // id of the thread which logged this record
196 wxThreadIdType threadId
;
197 #endif // wxUSE_THREADS
200 // store an arbitrary value in this record context
202 // wxWidgets always uses keys starting with "wx.", e.g. "wx.sys_error"
203 void StoreValue(const wxString
& key
, wxUIntPtr val
)
206 m_data
= new ExtraData
;
208 m_data
->numValues
[key
] = val
;
211 void StoreValue(const wxString
& key
, const wxString
& val
)
214 m_data
= new ExtraData
;
216 m_data
->strValues
[key
] = val
;
220 // these functions retrieve the value of either numeric or string key,
221 // return false if not found
222 bool GetNumValue(const wxString
& key
, wxUIntPtr
*val
) const
227 wxStringToNumHashMap::const_iterator it
= m_data
->numValues
.find(key
);
228 if ( it
== m_data
->numValues
.end() )
236 bool GetStrValue(const wxString
& key
, wxString
*val
) const
241 wxStringToStringHashMap::const_iterator it
= m_data
->strValues
.find(key
);
242 if ( it
== m_data
->strValues
.end() )
251 void Copy(const wxLogRecordInfo
& other
)
253 memcpy(this, &other
, sizeof(wxLogRecordInfo
));
255 m_data
= new ExtraData(*other
.m_data
);
258 // extra data associated with the log record: this is completely optional
259 // and can be used to pass information from the log function to the log
260 // sink (e.g. wxLogSysError() uses this to pass the error code)
263 wxStringToNumHashMap numValues
;
264 wxStringToStringHashMap strValues
;
271 #define wxLOG_KEY_TRACE_MASK "wx.trace_mask"
273 // ----------------------------------------------------------------------------
274 // derive from this class to redirect (or suppress, or ...) log messages
275 // normally, only a single instance of this class exists but it's not enforced
276 // ----------------------------------------------------------------------------
278 class WXDLLIMPEXP_BASE wxLog
284 // make dtor virtual for all derived classes
288 // these functions allow to completely disable all log messages
290 // is logging enabled at all now?
291 static bool IsEnabled() { return ms_doLog
; }
293 // is logging at this level enabled?
294 static bool IsLevelEnabled(wxLogLevel level
)
295 { return IsEnabled() && level
<= ms_logLevel
; }
297 // change the flag state, return the previous one
298 static bool EnableLogging(bool doIt
= true)
299 { bool doLogOld
= ms_doLog
; ms_doLog
= doIt
; return doLogOld
; }
303 // flush shows all messages if they're not logged immediately (FILE
304 // and iostream logs don't need it, but wxGuiLog does to avoid showing
305 // 17 modal dialogs one after another)
306 virtual void Flush();
308 // flush the active target if any
309 static void FlushActive()
311 if ( !ms_suspendCount
)
313 wxLog
*log
= GetActiveTarget();
319 // only one sink is active at each moment
320 // get current log target, will call wxApp::CreateLogTarget() to
321 // create one if none exists
322 static wxLog
*GetActiveTarget();
324 // change log target, pLogger may be NULL
325 static wxLog
*SetActiveTarget(wxLog
*pLogger
);
327 // suspend the message flushing of the main target until the next call
328 // to Resume() - this is mainly for internal use (to prevent wxYield()
329 // from flashing the messages)
330 static void Suspend() { ms_suspendCount
++; }
332 // must be called for each Suspend()!
333 static void Resume() { ms_suspendCount
--; }
335 // functions controlling the default wxLog behaviour
336 // verbose mode is activated by standard command-line '--verbose'
338 static void SetVerbose(bool bVerbose
= true) { ms_bVerbose
= bVerbose
; }
340 // Set log level. Log messages with level > logLevel will not be logged.
341 static void SetLogLevel(wxLogLevel logLevel
) { ms_logLevel
= logLevel
; }
343 // should GetActiveTarget() try to create a new log object if the
345 static void DontCreateOnDemand();
347 // Make GetActiveTarget() create a new log object again.
348 static void DoCreateOnDemand();
350 // log the count of repeating messages instead of logging the messages
352 static void SetRepetitionCounting(bool bRepetCounting
= true)
353 { ms_bRepetCounting
= bRepetCounting
; }
355 // gets duplicate counting status
356 static bool GetRepetitionCounting() { return ms_bRepetCounting
; }
358 // add string trace mask
359 static void AddTraceMask(const wxString
& str
);
361 // add string trace mask
362 static void RemoveTraceMask(const wxString
& str
);
364 // remove all string trace masks
365 static void ClearTraceMasks();
367 // get string trace masks: note that this is MT-unsafe if other threads can
368 // call AddTraceMask() concurrently
369 static const wxArrayString
& GetTraceMasks() { return ms_aTraceMasks
; }
371 // sets the time stamp string format: this is used as strftime() format
372 // string for the log targets which add time stamps to the messages; set
373 // it to empty string to disable time stamping completely.
374 static void SetTimestamp(const wxString
& ts
) { ms_timestamp
= ts
; }
376 // disable time stamping of log messages
377 static void DisableTimestamp() { SetTimestamp(wxEmptyString
); }
382 // gets the verbose status
383 static bool GetVerbose() { return ms_bVerbose
; }
385 // is this trace mask in the list?
386 static bool IsAllowedTraceMask(const wxString
& mask
);
388 // return the current loglevel limit
389 static wxLogLevel
GetLogLevel() { return ms_logLevel
; }
391 // get the current timestamp format string (maybe empty)
392 static const wxString
& GetTimestamp() { return ms_timestamp
; }
396 // helpers: all functions in this section are mostly for internal use only,
397 // don't call them from your code even if they are not formally deprecated
399 // put the time stamp into the string if ms_timestamp != NULL (don't
400 // change it otherwise)
401 static void TimeStamp(wxString
*str
);
403 // these methods should only be called from derived classes DoLogRecord(),
404 // DoLogTextAtLevel() and DoLogText() implementations respectively and
405 // shouldn't be called directly, use logging functions instead
406 void LogRecord(wxLogLevel level
,
408 const wxLogRecordInfo
& info
)
410 DoLogRecord(level
, msg
, info
);
413 void LogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
415 DoLogTextAtLevel(level
, msg
);
418 void LogText(const wxString
& msg
)
423 // this is a helper used by wxLogXXX() functions, don't call it directly
424 // and see DoLog() for function to overload in the derived classes
425 static void OnLog(wxLogLevel level
,
427 const wxLogRecordInfo
& info
);
429 // version called when no information about the location of the log record
430 // generation is available (but the time stamp is), it mainly exists for
431 // backwards compatibility, don't use it in new code
432 static void OnLog(wxLogLevel level
, const wxString
& msg
, time_t t
);
434 // a helper calling the above overload with current time
435 static void OnLog(wxLogLevel level
, const wxString
& msg
)
437 OnLog(level
, msg
, time(NULL
));
441 // this method exists for backwards compatibility only, don't use
442 bool HasPendingMessages() const { return true; }
444 #if WXWIN_COMPATIBILITY_2_6
445 // this function doesn't do anything any more, don't call it
447 static wxChar
*SetLogBuffer(wxChar
*, size_t = 0), return NULL
;
449 #endif // WXWIN_COMPATIBILITY_2_6
451 // don't use integer masks any more, use string trace masks instead
452 #if WXWIN_COMPATIBILITY_2_8
453 wxDEPRECATED_INLINE( static void SetTraceMask(wxTraceMask ulMask
),
454 ms_ulTraceMask
= ulMask
; )
455 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE( static wxTraceMask
GetTraceMask(),
456 return ms_ulTraceMask
; )
457 #endif // WXWIN_COMPATIBILITY_2_8
460 // the logging functions that can be overridden: DoLogRecord() is called
461 // for every "record", i.e. a unit of log output, to be logged and by
462 // default formats the message and passes it to DoLogTextAtLevel() which in
463 // turn passes it to DoLogText() by default
465 // override this method if you want to change message formatting or do
467 virtual void DoLogRecord(wxLogLevel level
,
469 const wxLogRecordInfo
& info
);
471 // override this method to redirect output to different channels depending
472 // on its level only; if even the level doesn't matter, override
473 // DoLogText() instead
474 virtual void DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
);
476 // this function is not pure virtual as it might not be needed if you do
477 // the logging in overridden DoLogRecord() or DoLogTextAtLevel() directly
478 // but if you do not override them in your derived class you must override
479 // this one as the default implementation of it simply asserts
480 virtual void DoLogText(const wxString
& msg
);
483 // the rest of the functions are for backwards compatibility only, don't
484 // use them in new code; if you're updating your existing code you need to
485 // switch to overriding DoLogRecord/Text() above (although as long as these
486 // functions exist, log classes using them will continue to work)
487 #if WXWIN_COMPATIBILITY_2_8
488 wxDEPRECATED_BUT_USED_INTERNALLY(
489 virtual void DoLog(wxLogLevel level
, const char *szString
, time_t t
)
492 wxDEPRECATED_BUT_USED_INTERNALLY(
493 virtual void DoLog(wxLogLevel level
, const wchar_t *wzString
, time_t t
)
496 // these shouldn't be used by new code
497 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
498 virtual void DoLogString(const char *WXUNUSED(szString
),
500 wxEMPTY_PARAMETER_VALUE
503 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
504 virtual void DoLogString(const wchar_t *WXUNUSED(wzString
),
506 wxEMPTY_PARAMETER_VALUE
508 #endif // WXWIN_COMPATIBILITY_2_8
511 // log a message indicating the number of times the previous message was
512 // repeated if previous repetition counter is strictly positive, does
513 // nothing otherwise; return the old value of repetition counter
514 unsigned LogLastRepeatIfNeeded();
517 // implement of LogLastRepeatIfNeeded(): it assumes that the
518 // caller had already locked GetPreviousLogCS()
519 unsigned LogLastRepeatIfNeededUnlocked();
524 // if true, don't log the same message multiple times, only log it once
525 // with the number of times it was repeated
526 static bool ms_bRepetCounting
;
528 static wxLog
*ms_pLogger
; // currently active log sink
529 static bool ms_doLog
; // false => all logging disabled
530 static bool ms_bAutoCreate
; // create new log targets on demand?
531 static bool ms_bVerbose
; // false => ignore LogInfo messages
533 static wxLogLevel ms_logLevel
; // limit logging to levels <= ms_logLevel
535 static size_t ms_suspendCount
; // if positive, logs are not flushed
537 // format string for strftime(), if empty, time stamping log messages is
539 static wxString ms_timestamp
;
541 #if WXWIN_COMPATIBILITY_2_8
542 static wxTraceMask ms_ulTraceMask
; // controls wxLogTrace behaviour
543 #endif // WXWIN_COMPATIBILITY_2_8
545 // currently enabled trace masks
546 static wxArrayString ms_aTraceMasks
;
549 // ----------------------------------------------------------------------------
550 // "trivial" derivations of wxLog
551 // ----------------------------------------------------------------------------
553 // log everything except for the debug/trace messages (which are passed to
554 // wxMessageOutputDebug) to a buffer
555 class WXDLLIMPEXP_BASE wxLogBuffer
: public wxLog
560 // get the string contents with all messages logged
561 const wxString
& GetBuffer() const { return m_str
; }
563 // show the buffer contents to the user in the best possible way (this uses
564 // wxMessageOutputMessageBox) and clear it
565 virtual void Flush();
568 virtual void DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
);
573 wxDECLARE_NO_COPY_CLASS(wxLogBuffer
);
577 // log everything to a "FILE *", stderr by default
578 class WXDLLIMPEXP_BASE wxLogStderr
: public wxLog
581 // redirect log output to a FILE
582 wxLogStderr(FILE *fp
= NULL
);
585 // implement sink function
586 virtual void DoLogText(const wxString
& msg
);
590 wxDECLARE_NO_COPY_CLASS(wxLogStderr
);
593 #if wxUSE_STD_IOSTREAM
595 // log everything to an "ostream", cerr by default
596 class WXDLLIMPEXP_BASE wxLogStream
: public wxLog
599 // redirect log output to an ostream
600 wxLogStream(wxSTD ostream
*ostr
= (wxSTD ostream
*) NULL
);
603 // implement sink function
604 virtual void DoLogText(const wxString
& msg
);
606 // using ptr here to avoid including <iostream.h> from this file
607 wxSTD ostream
*m_ostr
;
610 #endif // wxUSE_STD_IOSTREAM
612 // ----------------------------------------------------------------------------
613 // /dev/null log target: suppress logging until this object goes out of scope
614 // ----------------------------------------------------------------------------
622 // wxFile.Open() normally complains if file can't be opened, we don't
626 if ( !file.Open("bar") )
627 ... process error ourselves ...
629 // ~wxLogNull called, old log sink restored
632 class WXDLLIMPEXP_BASE wxLogNull
635 wxLogNull() : m_flagOld(wxLog::EnableLogging(false)) { }
636 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld
); }
639 bool m_flagOld
; // the previous value of the wxLog::ms_doLog
642 // ----------------------------------------------------------------------------
643 // chaining log target: installs itself as a log target and passes all
644 // messages to the real log target given to it in the ctor but also forwards
645 // them to the previously active one
647 // note that you don't have to call SetActiveTarget() with this class, it
648 // does it itself in its ctor
649 // ----------------------------------------------------------------------------
651 class WXDLLIMPEXP_BASE wxLogChain
: public wxLog
654 wxLogChain(wxLog
*logger
);
655 virtual ~wxLogChain();
657 // change the new log target
658 void SetLog(wxLog
*logger
);
660 // this can be used to temporarily disable (and then reenable) passing
661 // messages to the old logger (by default we do pass them)
662 void PassMessages(bool bDoPass
) { m_bPassMessages
= bDoPass
; }
664 // are we passing the messages to the previous log target?
665 bool IsPassingMessages() const { return m_bPassMessages
; }
667 // return the previous log target (may be NULL)
668 wxLog
*GetOldLog() const { return m_logOld
; }
670 // override base class version to flush the old logger as well
671 virtual void Flush();
673 // call to avoid destroying the old log target
674 void DetachOldLog() { m_logOld
= NULL
; }
677 // pass the record to the old logger if needed
678 virtual void DoLogRecord(wxLogLevel level
,
680 const wxLogRecordInfo
& info
);
683 // the current log target
686 // the previous log target
689 // do we pass the messages to the old logger?
690 bool m_bPassMessages
;
692 wxDECLARE_NO_COPY_CLASS(wxLogChain
);
695 // a chain log target which uses itself as the new logger
697 #define wxLogPassThrough wxLogInterposer
699 class WXDLLIMPEXP_BASE wxLogInterposer
: public wxLogChain
705 wxDECLARE_NO_COPY_CLASS(wxLogInterposer
);
708 // a temporary interposer which doesn't destroy the old log target
709 // (calls DetachOldLog)
711 class WXDLLIMPEXP_BASE wxLogInterposerTemp
: public wxLogChain
714 wxLogInterposerTemp();
717 wxDECLARE_NO_COPY_CLASS(wxLogInterposerTemp
);
721 // include GUI log targets:
722 #include "wx/generic/logg.h"
725 // ----------------------------------------------------------------------------
727 // ----------------------------------------------------------------------------
729 // wxLogger is a helper class used by wxLogXXX() functions implementation,
730 // don't use it directly as it's experimental and subject to change (OTOH it
731 // might become public in the future if it's deemed to be useful enough)
733 // contains information about the context from which a log message originates
734 // and provides Log() vararg method which forwards to wxLog::OnLog() and passes
735 // this context to it
739 // ctor takes the basic information about the log record
740 wxLogger(wxLogLevel level
,
741 const char *filename
,
745 m_info(filename
, line
, func
)
749 // store extra data in our log record and return this object itself (so
750 // that further calls to its functions could be chained)
751 template <typename T
>
752 wxLogger
& Store(const wxString
& key
, T val
)
754 m_info
.StoreValue(key
, val
);
758 // hack for "overloaded" wxLogXXX() functions: calling this method
759 // indicates that we may have an extra first argument preceding the format
760 // string and that if we do have it, we should store it in m_info using the
761 // given key (while by default 0 value will be used)
762 wxLogger
& MaybeStore(const wxString
& key
)
764 wxASSERT_MSG( m_optKey
.empty(), "can only have one optional value" );
767 m_info
.StoreValue(key
, 0);
772 // non-vararg function used by wxVLogXXX():
774 // log the message at the level specified in the ctor if this log message
776 void LogV(const wxString
& format
, va_list argptr
)
778 // remember that fatal errors can't be disabled
779 if ( m_level
== wxLOG_FatalError
|| wxLog::IsLevelEnabled(m_level
) )
780 DoCallOnLog(format
, argptr
);
783 // overloads used by functions with optional leading arguments (whose
784 // values are stored in the key passed to MaybeStore())
785 void LogV(long num
, const wxString
& format
, va_list argptr
)
787 Store(m_optKey
, num
);
789 LogV(format
, argptr
);
792 void LogV(void *ptr
, const wxString
& format
, va_list argptr
)
794 Store(m_optKey
, wxPtrToUInt(ptr
));
796 LogV(format
, argptr
);
800 // vararg functions used by wxLogXXX():
802 // will log the message at the level specified in the ctor
804 // notice that this function supposes that the caller already checked that
805 // the level was enabled and does no checks itself
806 WX_DEFINE_VARARG_FUNC_VOID
809 1, (const wxFormatString
&),
813 // same as Log() but with an extra numeric or pointer parameters: this is
814 // used to pass an optional value by storing it in m_info under the name
815 // passed to MaybeStore() and is required to support "overloaded" versions
816 // of wxLogStatus() and wxLogSysError()
817 WX_DEFINE_VARARG_FUNC_VOID
820 2, (long, const wxFormatString
&),
821 DoLogWithNum
, DoLogWithNumUtf8
824 // unfortunately we can't use "void *" here as we get overload ambiguities
825 // with Log(wxFormatString, ...) when the first argument is a "char *" or
826 // "wchar_t *" then -- so we only allow passing wxObject here, which is
827 // ugly but fine in practice as this overload is only used by wxLogStatus()
828 // whose first argument is a wxFrame
829 WX_DEFINE_VARARG_FUNC_VOID
832 2, (wxObject
*, const wxFormatString
&),
833 DoLogWithPtr
, DoLogWithPtrUtf8
836 // log the message at the level specified as its first argument
838 // as the macros don't have access to the level argument in this case, this
839 // function does check that the level is enabled itself
840 WX_DEFINE_VARARG_FUNC_VOID
843 2, (wxLogLevel
, const wxFormatString
&),
844 DoLogAtLevel
, DoLogAtLevelUtf8
847 // special versions for wxLogTrace() which is passed either string or
848 // integer mask as first argument determining whether the message should be
850 WX_DEFINE_VARARG_FUNC_VOID
853 2, (const wxString
&, const wxFormatString
&),
854 DoLogTrace
, DoLogTraceUtf8
857 #if WXWIN_COMPATIBILITY_2_8
858 WX_DEFINE_VARARG_FUNC_VOID
861 2, (wxTraceMask
, const wxFormatString
&),
862 DoLogTraceMask
, DoLogTraceMaskUtf8
864 #endif // WXWIN_COMPATIBILITY_2_8
867 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
868 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
869 1, (const wxString
&),
870 (wxFormatString(f1
)))
871 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
872 1, (const wxCStrData
&),
873 (wxFormatString(f1
)))
874 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
876 (wxFormatString(f1
)))
877 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
879 (wxFormatString(f1
)))
881 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
882 2, (long, const wxString
&),
883 (f1
, wxFormatString(f2
)))
884 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
885 2, (long, const wxCStrData
&),
886 (f1
, wxFormatString(f2
)))
887 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
888 2, (long, const char *),
889 (f1
, wxFormatString(f2
)))
890 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
891 2, (long, const wchar_t *),
892 (f1
, wxFormatString(f2
)))
894 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
895 2, (wxObject
*, const wxString
&),
896 (f1
, wxFormatString(f2
)))
897 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
898 2, (wxObject
*, const wxCStrData
&),
899 (f1
, wxFormatString(f2
)))
900 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
901 2, (wxObject
*, const char *),
902 (f1
, wxFormatString(f2
)))
903 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
904 2, (wxObject
*, const wchar_t *),
905 (f1
, wxFormatString(f2
)))
907 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
908 2, (wxLogLevel
, const wxString
&),
909 (f1
, wxFormatString(f2
)))
910 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
911 2, (wxLogLevel
, const wxCStrData
&),
912 (f1
, wxFormatString(f2
)))
913 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
914 2, (wxLogLevel
, const char *),
915 (f1
, wxFormatString(f2
)))
916 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
917 2, (wxLogLevel
, const wchar_t *),
918 (f1
, wxFormatString(f2
)))
920 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
921 2, (const wxString
&, const wxString
&),
922 (f1
, wxFormatString(f2
)))
923 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
924 2, (const wxString
&, const wxCStrData
&),
925 (f1
, wxFormatString(f2
)))
926 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
927 2, (const wxString
&, const char *),
928 (f1
, wxFormatString(f2
)))
929 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
930 2, (const wxString
&, const wchar_t *),
931 (f1
, wxFormatString(f2
)))
933 #if WXWIN_COMPATIBILITY_2_8
934 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
935 2, (wxTraceMask
, wxTraceMask
),
936 (f1
, wxFormatString(f2
)))
937 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
938 2, (wxTraceMask
, const wxCStrData
&),
939 (f1
, wxFormatString(f2
)))
940 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
941 2, (wxTraceMask
, const char *),
942 (f1
, wxFormatString(f2
)))
943 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
944 2, (wxTraceMask
, const wchar_t *),
945 (f1
, wxFormatString(f2
)))
946 #endif // WXWIN_COMPATIBILITY_2_8
947 #endif // __WATCOMC__
950 #if !wxUSE_UTF8_LOCALE_ONLY
951 void DoLog(const wxChar
*format
, ...)
954 va_start(argptr
, format
);
955 DoCallOnLog(format
, argptr
);
959 void DoLogWithNum(long num
, const wxChar
*format
, ...)
961 Store(m_optKey
, num
);
964 va_start(argptr
, format
);
965 DoCallOnLog(format
, argptr
);
969 void DoLogWithPtr(void *ptr
, const wxChar
*format
, ...)
971 Store(m_optKey
, wxPtrToUInt(ptr
));
974 va_start(argptr
, format
);
975 DoCallOnLog(format
, argptr
);
979 void DoLogAtLevel(wxLogLevel level
, const wxChar
*format
, ...)
981 if ( !wxLog::IsLevelEnabled(level
) )
985 va_start(argptr
, format
);
986 DoCallOnLog(level
, format
, argptr
);
990 void DoLogTrace(const wxString
& mask
, const wxChar
*format
, ...)
992 if ( !wxLog::IsAllowedTraceMask(mask
) )
995 Store(wxLOG_KEY_TRACE_MASK
, mask
);
998 va_start(argptr
, format
);
999 DoCallOnLog(format
, argptr
);
1003 #if WXWIN_COMPATIBILITY_2_8
1004 void DoLogTraceMask(wxTraceMask mask
, const wxChar
*format
, ...)
1006 if ( (wxLog::GetTraceMask() & mask
) != mask
)
1009 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1012 va_start(argptr
, format
);
1013 DoCallOnLog(format
, argptr
);
1016 #endif // WXWIN_COMPATIBILITY_2_8
1017 #endif // !wxUSE_UTF8_LOCALE_ONLY
1019 #if wxUSE_UNICODE_UTF8
1020 void DoLogUtf8(const char *format
, ...)
1023 va_start(argptr
, format
);
1024 DoCallOnLog(format
, argptr
);
1028 void DoLogWithNumUtf8(long num
, const char *format
, ...)
1030 Store(m_optKey
, num
);
1033 va_start(argptr
, format
);
1034 DoCallOnLog(format
, argptr
);
1038 void DoLogWithPtrUtf8(void *ptr
, const char *format
, ...)
1040 Store(m_optKey
, wxPtrToUInt(ptr
));
1043 va_start(argptr
, format
);
1044 DoCallOnLog(format
, argptr
);
1048 void DoLogAtLevelUtf8(wxLogLevel level
, const char *format
, ...)
1050 if ( !wxLog::IsLevelEnabled(level
) )
1054 va_start(argptr
, format
);
1055 DoCallOnLog(level
, format
, argptr
);
1059 void DoLogTraceUtf8(const wxString
& mask
, const char *format
, ...)
1061 if ( !wxLog::IsAllowedTraceMask(mask
) )
1064 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1067 va_start(argptr
, format
);
1068 DoCallOnLog(format
, argptr
);
1072 #if WXWIN_COMPATIBILITY_2_8
1073 void DoLogTraceMaskUtf8(wxTraceMask mask
, const char *format
, ...)
1075 if ( (wxLog::GetTraceMask() & mask
) != mask
)
1078 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1081 va_start(argptr
, format
);
1082 DoCallOnLog(format
, argptr
);
1085 #endif // WXWIN_COMPATIBILITY_2_8
1086 #endif // wxUSE_UNICODE_UTF8
1088 void DoCallOnLog(wxLogLevel level
, const wxString
& format
, va_list argptr
)
1090 wxLog::OnLog(level
, wxString::FormatV(format
, argptr
), m_info
);
1093 void DoCallOnLog(const wxString
& format
, va_list argptr
)
1095 wxLog::OnLog(m_level
, wxString::FormatV(format
, argptr
), m_info
);
1099 const wxLogLevel m_level
;
1100 wxLogRecordInfo m_info
;
1104 wxDECLARE_NO_COPY_CLASS(wxLogger
);
1107 // ============================================================================
1109 // ============================================================================
1111 // ----------------------------------------------------------------------------
1112 // get error code/error message from system in a portable way
1113 // ----------------------------------------------------------------------------
1115 // return the last system error code
1116 WXDLLIMPEXP_BASE
unsigned long wxSysErrorCode();
1118 // return the error message for given (or last if 0) error code
1119 WXDLLIMPEXP_BASE
const wxChar
* wxSysErrorMsg(unsigned long nErrCode
= 0);
1121 // ----------------------------------------------------------------------------
1122 // define wxLog<level>() functions which can be used by application instead of
1123 // stdio, iostream &c for log messages for easy redirection
1124 // ----------------------------------------------------------------------------
1127 The code below is unreadable because it (unfortunately unavoidably)
1128 contains a lot of macro magic but all it does is to define wxLogXXX() such
1129 that you can call them as vararg functions to log a message at the
1130 corresponding level.
1132 More precisely, it defines:
1134 - wxLog{FatalError,Error,Warning,Message,Verbose,Debug}() functions
1135 taking the format string and additional vararg arguments if needed.
1136 - wxLogGeneric(wxLogLevel level, const wxString& format, ...) which
1137 takes the log level explicitly.
1138 - wxLogSysError(const wxString& format, ...) and wxLogSysError(long
1139 err, const wxString& format, ...) which log a wxLOG_Error severity
1140 message with the error message corresponding to the system error code
1141 err or the last error.
1142 - wxLogStatus(const wxString& format, ...) which logs the message into
1143 the status bar of the main application window and its overload
1144 wxLogStatus(wxFrame *frame, const wxString& format, ...) which logs it
1145 into the status bar of the specified frame.
1146 - wxLogTrace(Mask mask, const wxString& format, ...) which only logs
1147 the message is the specified mask is enabled. This comes in two kinds:
1148 Mask can be a wxString or a long. Both are deprecated.
1150 In addition, wxVLogXXX() versions of all the functions above are also
1151 defined. They take a va_list argument instead of "...".
1154 // creates wxLogger object for the current location
1155 #define wxMAKE_LOGGER(level) \
1156 wxLogger(wxLOG_##level, __FILE__, __LINE__, __WXFUNCTION__)
1158 // this macro generates the expression which logs whatever follows it in
1159 // parentheses at the level specified as argument
1160 #define wxDO_LOG(level) wxMAKE_LOGGER(level).Log
1162 // this is the non-vararg equivalent
1163 #define wxDO_LOGV(level, format, argptr) \
1164 wxMAKE_LOGGER(level).LogV(format, argptr)
1166 // this macro declares wxLog<level>() macro which logs whatever follows it if
1167 // logging at specified level is enabled (notice that if it is false, the
1168 // following arguments are not even evaluated which is good as it avoids
1169 // unnecessary overhead)
1171 // Note: the strange if/else construct is needed to make the following code
1174 // wxLogError("!!!");
1178 // work as expected, without it the second "else" would match the "if"
1179 // inside wxLogError(). Unfortunately code like
1182 // wxLogError("!!!");
1184 // now provokes "suggest explicit braces to avoid ambiguous 'else'"
1185 // warnings from g++ 4.3 and later with -Wparentheses on but they can be
1186 // easily fixed by adding curly braces around wxLogError() and at least
1187 // the code still does do the right thing.
1188 #define wxDO_LOG_IF_ENABLED(level) \
1189 if ( !wxLog::IsLevelEnabled(wxLOG_##level) ) \
1194 // wxLogFatalError() is special as it can't be disabled
1195 #define wxLogFatalError wxDO_LOG(FatalError)
1196 #define wxVLogFatalError(format, argptr) wxDO_LOGV(FatalError, format, argptr)
1198 #define wxLogError wxDO_LOG_IF_ENABLED(Error)
1199 #define wxVLogError(format, argptr) wxDO_LOGV(Error, format, argptr)
1201 #define wxLogWarning wxDO_LOG_IF_ENABLED(Warning)
1202 #define wxVLogWarning(format, argptr) wxDO_LOGV(Warning, format, argptr)
1204 #define wxLogMessage wxDO_LOG_IF_ENABLED(Message)
1205 #define wxVLogMessage(format, argptr) wxDO_LOGV(Message, format, argptr)
1207 // this one is special as it only logs if we're in verbose mode
1208 #define wxLogVerbose \
1209 if ( !(wxLog::IsLevelEnabled(wxLOG_Info) && wxLog::GetVerbose()) ) \
1213 #define wxVLogVerbose(format, argptr) \
1214 if ( !(wxLog::IsLevelEnabled(wxLOG_Info) && wxLog::GetVerbose()) ) \
1217 wxDO_LOGV(Info, format, argptr)
1219 // deprecated synonyms for wxLogVerbose() and wxVLogVerbose()
1220 #define wxLogInfo wxLogVerbose
1221 #define wxVLogInfo wxVLogVerbose
1224 // another special case: the level is passed as first argument of the function
1225 // and so is not available to the macro
1227 // notice that because of this, arguments of wxLogGeneric() are currently
1228 // always evaluated, unlike for the other log functions
1229 #define wxLogGeneric wxMAKE_LOGGER(Max).LogAtLevel
1230 #define wxVLogGeneric(level, format, argptr) \
1231 if ( !wxLog::IsLevelEnabled(wxLOG_##level) ) \
1234 wxDO_LOGV(level, format, argptr)
1237 // wxLogSysError() needs to stash the error code value in the log record info
1238 // so it needs special handling too; additional complications arise because the
1239 // error code may or not be present as the first argument
1240 #define wxLOG_KEY_SYS_ERROR_CODE "wx.sys_error"
1242 #define wxLogSysError \
1243 if ( !wxLog::IsLevelEnabled(wxLOG_Error) ) \
1246 wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE).Log
1248 // unfortunately we can't have overloaded macros so we can't define versions
1249 // both with and without error code argument and have to rely on LogV()
1250 // overloads in wxLogger to select between them
1251 #define wxVLogSysError \
1252 wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE).LogV
1255 // wxLogStatus() is similar to wxLogSysError() as it allows to optionally
1256 // specify the frame to which the message should go
1257 #define wxLOG_KEY_FRAME "wx.frame"
1259 #define wxLogStatus \
1260 if ( !wxLog::IsLevelEnabled(wxLOG_Status) ) \
1263 wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).Log
1265 #define wxVLogStatus(format, argptr) \
1266 wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).LogV
1272 #undef wxUSE_LOG_DEBUG
1273 #define wxUSE_LOG_DEBUG 0
1275 #undef wxUSE_LOG_TRACE
1276 #define wxUSE_LOG_TRACE 0
1278 #if defined(__WATCOMC__) || defined(__MINGW32__)
1279 // Mingw has similar problem with wxLogSysError:
1280 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x ) x
1282 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x )
1285 // define macros for defining log functions which do nothing at all
1287 // WX_WATCOM_ONLY_CODE is needed to work around
1288 // http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1289 #define wxDEFINE_EMPTY_LOG_FUNCTION(level) \
1290 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxString&)) \
1291 WX_WATCOM_ONLY_CODE( \
1292 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const char*)) \
1293 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wchar_t*)) \
1294 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxCStrData&)) \
1296 inline void wxVLog##level(const wxString& WXUNUSED(format), \
1297 va_list WXUNUSED(argptr)) { } \
1299 #define wxDEFINE_EMPTY_LOG_FUNCTION2(level, argclass) \
1300 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxString&)) \
1301 WX_WATCOM_OR_MINGW_ONLY_CODE( \
1302 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const char*)) \
1303 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wchar_t*)) \
1304 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxCStrData&)) \
1306 inline void wxVLog##level(argclass WXUNUSED(arg), \
1307 const wxString& WXUNUSED(format), \
1308 va_list WXUNUSED(argptr)) {}
1310 wxDEFINE_EMPTY_LOG_FUNCTION(FatalError
);
1311 wxDEFINE_EMPTY_LOG_FUNCTION(Error
);
1312 wxDEFINE_EMPTY_LOG_FUNCTION(SysError
);
1313 wxDEFINE_EMPTY_LOG_FUNCTION2(SysError
, long);
1314 wxDEFINE_EMPTY_LOG_FUNCTION(Warning
);
1315 wxDEFINE_EMPTY_LOG_FUNCTION(Message
);
1316 wxDEFINE_EMPTY_LOG_FUNCTION(Info
);
1317 wxDEFINE_EMPTY_LOG_FUNCTION(Verbose
);
1319 wxDEFINE_EMPTY_LOG_FUNCTION2(Generic
, wxLogLevel
);
1322 wxDEFINE_EMPTY_LOG_FUNCTION(Status
);
1323 wxDEFINE_EMPTY_LOG_FUNCTION2(Status
, wxFrame
*);
1326 // Empty Class to fake wxLogNull
1327 class WXDLLIMPEXP_BASE wxLogNull
1333 // Dummy macros to replace some functions.
1334 #define wxSysErrorCode() (unsigned long)0
1335 #define wxSysErrorMsg( X ) (const wxChar*)NULL
1337 // Fake symbolic trace masks... for those that are used frequently
1338 #define wxTRACE_OleCalls wxEmptyString // OLE interface calls
1340 #endif // wxUSE_LOG/!wxUSE_LOG
1343 // debug functions can be completely disabled in optimized builds
1345 // if these log functions are disabled, we prefer to define them as (empty)
1346 // variadic macros as this completely removes them and their argument
1347 // evaluation from the object code but if this is not supported by compiler we
1348 // use empty inline functions instead (defining them as nothing would result in
1349 // compiler warnings)
1351 // note that making wxVLogDebug/Trace() themselves (empty inline) functions is
1352 // a bad idea as some compilers are stupid enough to not inline even empty
1353 // functions if their parameters are complicated enough, but by defining them
1354 // as an empty inline function we ensure that even dumbest compilers optimise
1357 // but Borland gives "W8019: Code has no effect" for wxLogNop() so we need
1358 // to define it differently for it to avoid these warnings (same problem as
1359 // with wxUnusedVar())
1360 #define wxLogNop() { }
1362 inline void wxLogNop() { }
1366 #define wxLogDebug wxDO_LOG_IF_ENABLED(Debug)
1367 #define wxVLogDebug(format, argptr) wxDO_LOGV(Debug, format, argptr)
1368 #else // !wxUSE_LOG_DEBUG
1369 #define wxVLogDebug(fmt, valist) wxLogNop()
1371 #ifdef HAVE_VARIADIC_MACROS
1372 #define wxLogDebug(fmt, ...) wxLogNop()
1373 #else // !HAVE_VARIADIC_MACROS
1374 WX_DEFINE_VARARG_FUNC_NOP(wxLogDebug
, 1, (const wxString
&))
1376 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
1379 #define wxLogTrace \
1380 if ( !wxLog::IsLevelEnabled(wxLOG_Trace) ) \
1383 wxMAKE_LOGGER(Trace).LogTrace
1384 #else // !wxUSE_LOG_TRACE
1385 #define wxVLogTrace(mask, fmt, valist) wxLogNop()
1387 #ifdef HAVE_VARIADIC_MACROS
1388 #define wxLogTrace(mask, fmt, ...) wxLogNop()
1389 #else // !HAVE_VARIADIC_MACROS
1390 #if WXWIN_COMPATIBILITY_2_8
1391 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (wxTraceMask
, const wxString
&))
1393 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const wxString
&, const wxString
&))
1395 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1396 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const char*, const char*))
1397 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const wchar_t*, const wchar_t*))
1399 #endif // HAVE_VARIADIC_MACROS/!HAVE_VARIADIC_MACROS
1400 #endif // wxUSE_LOG_TRACE/!wxUSE_LOG_TRACE
1402 // wxLogFatalError helper: show the (fatal) error to the user in a safe way,
1403 // i.e. without using wxMessageBox() for example because it could crash
1404 void WXDLLIMPEXP_BASE
1405 wxSafeShowMessage(const wxString
& title
, const wxString
& text
);
1407 // ----------------------------------------------------------------------------
1408 // debug only logging functions: use them with API name and error code
1409 // ----------------------------------------------------------------------------
1412 // make life easier for people using VC++ IDE: clicking on the message
1413 // will take us immediately to the place of the failed API
1415 #define wxLogApiError(api, rc) \
1416 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
1417 __FILE__, __LINE__, api, \
1418 (long)rc, wxSysErrorMsg(rc))
1420 #define wxLogApiError(api, rc) \
1421 wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \
1422 wxT("error 0x%08lx (%s)."), \
1423 __FILE__, __LINE__, api, \
1424 (long)rc, wxSysErrorMsg(rc))
1425 #endif // VC++/!VC++
1427 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
1429 #else // !wxUSE_LOG_DEBUG
1430 #define wxLogApiError(api, err) wxLogNop()
1431 #define wxLogLastError(api) wxLogNop()
1432 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
1434 // wxCocoa has additiional trace masks
1435 #if defined(__WXCOCOA__)
1436 #include "wx/cocoa/log.h"
1439 #ifdef WX_WATCOM_ONLY_CODE
1440 #undef WX_WATCOM_ONLY_CODE
1443 #endif // _WX_LOG_H_