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 // ----------------------------------------------------------------------------
86 class WXDLLIMPEXP_FWD_CORE wxFrame
;
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // different standard log levels (you may also define your own)
96 wxLOG_FatalError
, // program can't continue, abort immediately
97 wxLOG_Error
, // a serious error, user must be informed about it
98 wxLOG_Warning
, // user is normally informed about it but may be ignored
99 wxLOG_Message
, // normal message (i.e. normal output of a non GUI app)
100 wxLOG_Status
, // informational: might go to the status line of GUI app
101 wxLOG_Info
, // informational message (a.k.a. 'Verbose')
102 wxLOG_Debug
, // never shown to the user, disabled in release mode
103 wxLOG_Trace
, // trace messages are also only enabled in debug mode
104 wxLOG_Progress
, // used for progress indicator (not yet)
105 wxLOG_User
= 100, // user defined levels start here
109 // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
110 // discarded unless the string "foo" has been added to the list of allowed
111 // ones with AddTraceMask()
113 #define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete)
114 #define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks
115 #define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation
116 #define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations
119 #define wxTRACE_OleCalls wxT("ole") // OLE interface calls
122 #include "wx/iosfwrap.h"
124 // ----------------------------------------------------------------------------
125 // information about a log record, i.e. unit of log output
126 // ----------------------------------------------------------------------------
128 class wxLogRecordInfo
131 // default ctor creates an uninitialized object
134 memset(this, 0, sizeof(this));
137 // normal ctor, used by wxLogger specifies the location of the log
138 // statement; its time stamp and thread id are set up here
139 wxLogRecordInfo(const char *filename_
,
143 filename
= filename_
;
147 timestamp
= time(NULL
);
150 threadId
= wxThread::GetCurrentId();
151 #endif // wxUSE_THREADS
156 // we need to define copy ctor and assignment operator because of m_data
157 wxLogRecordInfo(const wxLogRecordInfo
& other
)
162 wxLogRecordInfo
& operator=(const wxLogRecordInfo
& other
)
164 if ( &other
!= this )
173 // dtor is non-virtual, this class is not meant to be derived from
180 // the file name and line number of the file where the log record was
181 // generated, if available or NULL and 0 otherwise
182 const char *filename
;
185 // the name of the function where the log record was generated (may be NULL
186 // if the compiler doesn't support __FUNCTION__)
189 // time of record generation
193 // id of the thread which logged this record
194 wxThreadIdType threadId
;
195 #endif // wxUSE_THREADS
198 // store an arbitrary value in this record context
200 // wxWidgets always uses keys starting with "wx.", e.g. "wx.sys_error"
201 void StoreValue(const wxString
& key
, wxUIntPtr val
)
204 m_data
= new ExtraData
;
206 m_data
->numValues
[key
] = val
;
209 void StoreValue(const wxString
& key
, const wxString
& val
)
212 m_data
= new ExtraData
;
214 m_data
->strValues
[key
] = val
;
218 // these functions retrieve the value of either numeric or string key,
219 // return false if not found
220 bool GetNumValue(const wxString
& key
, wxUIntPtr
*val
) const
225 wxStringToNumHashMap::const_iterator it
= m_data
->numValues
.find(key
);
226 if ( it
== m_data
->numValues
.end() )
234 bool GetStrValue(const wxString
& key
, wxString
*val
) const
239 wxStringToStringHashMap::const_iterator it
= m_data
->strValues
.find(key
);
240 if ( it
== m_data
->strValues
.end() )
249 void Copy(const wxLogRecordInfo
& other
)
251 memcpy(this, &other
, sizeof(wxLogRecordInfo
));
253 m_data
= new ExtraData(*other
.m_data
);
256 // extra data associated with the log record: this is completely optional
257 // and can be used to pass information from the log function to the log
258 // sink (e.g. wxLogSysError() uses this to pass the error code)
261 wxStringToNumHashMap numValues
;
262 wxStringToStringHashMap strValues
;
269 #define wxLOG_KEY_TRACE_MASK "wx.trace_mask"
271 // ----------------------------------------------------------------------------
272 // derive from this class to redirect (or suppress, or ...) log messages
273 // normally, only a single instance of this class exists but it's not enforced
274 // ----------------------------------------------------------------------------
276 class WXDLLIMPEXP_BASE wxLog
282 // make dtor virtual for all derived classes
286 // these functions allow to completely disable all log messages
288 // is logging enabled at all now?
289 static bool IsEnabled() { return ms_doLog
; }
291 // is logging at this level enabled?
292 static bool IsLevelEnabled(wxLogLevel level
)
293 { return IsEnabled() && level
<= ms_logLevel
; }
295 // change the flag state, return the previous one
296 static bool EnableLogging(bool doIt
= true)
297 { bool doLogOld
= ms_doLog
; ms_doLog
= doIt
; return doLogOld
; }
301 // flush shows all messages if they're not logged immediately (FILE
302 // and iostream logs don't need it, but wxGuiLog does to avoid showing
303 // 17 modal dialogs one after another)
304 virtual void Flush();
306 // flush the active target if any
307 static void FlushActive()
309 if ( !ms_suspendCount
)
311 wxLog
*log
= GetActiveTarget();
317 // only one sink is active at each moment
318 // get current log target, will call wxApp::CreateLogTarget() to
319 // create one if none exists
320 static wxLog
*GetActiveTarget();
322 // change log target, pLogger may be NULL
323 static wxLog
*SetActiveTarget(wxLog
*pLogger
);
325 // suspend the message flushing of the main target until the next call
326 // to Resume() - this is mainly for internal use (to prevent wxYield()
327 // from flashing the messages)
328 static void Suspend() { ms_suspendCount
++; }
330 // must be called for each Suspend()!
331 static void Resume() { ms_suspendCount
--; }
333 // functions controlling the default wxLog behaviour
334 // verbose mode is activated by standard command-line '--verbose'
336 static void SetVerbose(bool bVerbose
= true) { ms_bVerbose
= bVerbose
; }
338 // Set log level. Log messages with level > logLevel will not be logged.
339 static void SetLogLevel(wxLogLevel logLevel
) { ms_logLevel
= logLevel
; }
341 // should GetActiveTarget() try to create a new log object if the
343 static void DontCreateOnDemand();
345 // Make GetActiveTarget() create a new log object again.
346 static void DoCreateOnDemand();
348 // log the count of repeating messages instead of logging the messages
350 static void SetRepetitionCounting(bool bRepetCounting
= true)
351 { ms_bRepetCounting
= bRepetCounting
; }
353 // gets duplicate counting status
354 static bool GetRepetitionCounting() { return ms_bRepetCounting
; }
356 // add string trace mask
357 static void AddTraceMask(const wxString
& str
);
359 // add string trace mask
360 static void RemoveTraceMask(const wxString
& str
);
362 // remove all string trace masks
363 static void ClearTraceMasks();
365 // get string trace masks: note that this is MT-unsafe if other threads can
366 // call AddTraceMask() concurrently
367 static const wxArrayString
& GetTraceMasks() { return ms_aTraceMasks
; }
369 // sets the time stamp string format: this is used as strftime() format
370 // string for the log targets which add time stamps to the messages; set
371 // it to empty string to disable time stamping completely.
372 static void SetTimestamp(const wxString
& ts
) { ms_timestamp
= ts
; }
374 // disable time stamping of log messages
375 static void DisableTimestamp() { SetTimestamp(wxEmptyString
); }
380 // gets the verbose status
381 static bool GetVerbose() { return ms_bVerbose
; }
383 // is this trace mask in the list?
384 static bool IsAllowedTraceMask(const wxString
& mask
);
386 // return the current loglevel limit
387 static wxLogLevel
GetLogLevel() { return ms_logLevel
; }
389 // get the current timestamp format string (maybe empty)
390 static const wxString
& GetTimestamp() { return ms_timestamp
; }
394 // helpers: all functions in this section are mostly for internal use only,
395 // don't call them from your code even if they are not formally deprecated
397 // put the time stamp into the string if ms_timestamp != NULL (don't
398 // change it otherwise)
399 static void TimeStamp(wxString
*str
);
401 // these methods should only be called from derived classes DoLogRecord(),
402 // DoLogTextAtLevel() and DoLogText() implementations respectively and
403 // shouldn't be called directly, use logging functions instead
404 void LogRecord(wxLogLevel level
,
406 const wxLogRecordInfo
& info
)
408 DoLogRecord(level
, msg
, info
);
411 void LogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
413 DoLogTextAtLevel(level
, msg
);
416 void LogText(const wxString
& msg
)
421 // this is a helper used by wxLogXXX() functions, don't call it directly
422 // and see DoLog() for function to overload in the derived classes
423 static void OnLog(wxLogLevel level
,
425 const wxLogRecordInfo
& info
);
427 // version called when no information about the location of the log record
428 // generation is available (but the time stamp is), it mainly exists for
429 // backwards compatibility, don't use it in new code
430 static void OnLog(wxLogLevel level
, const wxString
& msg
, time_t t
);
432 // a helper calling the above overload with current time
433 static void OnLog(wxLogLevel level
, const wxString
& msg
)
435 OnLog(level
, msg
, time(NULL
));
439 // this method exists for backwards compatibility only, don't use
440 bool HasPendingMessages() const { return true; }
442 #if WXWIN_COMPATIBILITY_2_6
443 // this function doesn't do anything any more, don't call it
445 static wxChar
*SetLogBuffer(wxChar
*, size_t = 0), return NULL
;
447 #endif // WXWIN_COMPATIBILITY_2_6
449 // don't use integer masks any more, use string trace masks instead
450 #if WXWIN_COMPATIBILITY_2_8
451 wxDEPRECATED_INLINE( static void SetTraceMask(wxTraceMask ulMask
),
452 ms_ulTraceMask
= ulMask
; )
453 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE( static wxTraceMask
GetTraceMask(),
454 return ms_ulTraceMask
; )
455 #endif // WXWIN_COMPATIBILITY_2_8
458 // the logging functions that can be overridden: DoLogRecord() is called
459 // for every "record", i.e. a unit of log output, to be logged and by
460 // default formats the message and passes it to DoLogTextAtLevel() which in
461 // turn passes it to DoLogText() by default
463 // override this method if you want to change message formatting or do
465 virtual void DoLogRecord(wxLogLevel level
,
467 const wxLogRecordInfo
& info
);
469 // override this method to redirect output to different channels depending
470 // on its level only; if even the level doesn't matter, override
471 // DoLogText() instead
472 virtual void DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
);
474 // this function is not pure virtual as it might not be needed if you do
475 // the logging in overridden DoLogRecord() or DoLogTextAtLevel() directly
476 // but if you do not override them in your derived class you must override
477 // this one as the default implementation of it simply asserts
478 virtual void DoLogText(const wxString
& msg
);
481 // the rest of the functions are for backwards compatibility only, don't
482 // use them in new code; if you're updating your existing code you need to
483 // switch to overriding DoLogRecord/Text() above (although as long as these
484 // functions exist, log classes using them will continue to work)
485 #if WXWIN_COMPATIBILITY_2_8
486 wxDEPRECATED_BUT_USED_INTERNALLY(
487 virtual void DoLog(wxLogLevel level
, const char *szString
, time_t t
)
490 wxDEPRECATED_BUT_USED_INTERNALLY(
491 virtual void DoLog(wxLogLevel level
, const wchar_t *wzString
, time_t t
)
494 // these shouldn't be used by new code
495 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
496 virtual void DoLogString(const char *WXUNUSED(szString
),
498 wxEMPTY_PARAMETER_VALUE
501 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
502 virtual void DoLogString(const wchar_t *WXUNUSED(wzString
),
504 wxEMPTY_PARAMETER_VALUE
506 #endif // WXWIN_COMPATIBILITY_2_8
509 // log a message indicating the number of times the previous message was
510 // repeated if previous repetition counter is strictly positive, does
511 // nothing otherwise; return the old value of repetition counter
512 unsigned LogLastRepeatIfNeeded();
515 // implement of LogLastRepeatIfNeeded(): it assumes that the
516 // caller had already locked GetPreviousLogCS()
517 unsigned LogLastRepeatIfNeededUnlocked();
522 // if true, don't log the same message multiple times, only log it once
523 // with the number of times it was repeated
524 static bool ms_bRepetCounting
;
526 static wxLog
*ms_pLogger
; // currently active log sink
527 static bool ms_doLog
; // false => all logging disabled
528 static bool ms_bAutoCreate
; // create new log targets on demand?
529 static bool ms_bVerbose
; // false => ignore LogInfo messages
531 static wxLogLevel ms_logLevel
; // limit logging to levels <= ms_logLevel
533 static size_t ms_suspendCount
; // if positive, logs are not flushed
535 // format string for strftime(), if empty, time stamping log messages is
537 static wxString ms_timestamp
;
539 #if WXWIN_COMPATIBILITY_2_8
540 static wxTraceMask ms_ulTraceMask
; // controls wxLogTrace behaviour
541 #endif // WXWIN_COMPATIBILITY_2_8
543 // currently enabled trace masks
544 static wxArrayString ms_aTraceMasks
;
547 // ----------------------------------------------------------------------------
548 // "trivial" derivations of wxLog
549 // ----------------------------------------------------------------------------
551 // log everything except for the debug/trace messages (which are passed to
552 // wxMessageOutputDebug) to a buffer
553 class WXDLLIMPEXP_BASE wxLogBuffer
: public wxLog
558 // get the string contents with all messages logged
559 const wxString
& GetBuffer() const { return m_str
; }
561 // show the buffer contents to the user in the best possible way (this uses
562 // wxMessageOutputMessageBox) and clear it
563 virtual void Flush();
566 virtual void DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
);
571 wxDECLARE_NO_COPY_CLASS(wxLogBuffer
);
575 // log everything to a "FILE *", stderr by default
576 class WXDLLIMPEXP_BASE wxLogStderr
: public wxLog
579 // redirect log output to a FILE
580 wxLogStderr(FILE *fp
= NULL
);
583 // implement sink function
584 virtual void DoLogText(const wxString
& msg
);
588 wxDECLARE_NO_COPY_CLASS(wxLogStderr
);
591 #if wxUSE_STD_IOSTREAM
593 // log everything to an "ostream", cerr by default
594 class WXDLLIMPEXP_BASE wxLogStream
: public wxLog
597 // redirect log output to an ostream
598 wxLogStream(wxSTD ostream
*ostr
= (wxSTD ostream
*) NULL
);
601 // implement sink function
602 virtual void DoLogText(const wxString
& msg
);
604 // using ptr here to avoid including <iostream.h> from this file
605 wxSTD ostream
*m_ostr
;
608 #endif // wxUSE_STD_IOSTREAM
610 // ----------------------------------------------------------------------------
611 // /dev/null log target: suppress logging until this object goes out of scope
612 // ----------------------------------------------------------------------------
620 // wxFile.Open() normally complains if file can't be opened, we don't
624 if ( !file.Open("bar") )
625 ... process error ourselves ...
627 // ~wxLogNull called, old log sink restored
630 class WXDLLIMPEXP_BASE wxLogNull
633 wxLogNull() : m_flagOld(wxLog::EnableLogging(false)) { }
634 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld
); }
637 bool m_flagOld
; // the previous value of the wxLog::ms_doLog
640 // ----------------------------------------------------------------------------
641 // chaining log target: installs itself as a log target and passes all
642 // messages to the real log target given to it in the ctor but also forwards
643 // them to the previously active one
645 // note that you don't have to call SetActiveTarget() with this class, it
646 // does it itself in its ctor
647 // ----------------------------------------------------------------------------
649 class WXDLLIMPEXP_BASE wxLogChain
: public wxLog
652 wxLogChain(wxLog
*logger
);
653 virtual ~wxLogChain();
655 // change the new log target
656 void SetLog(wxLog
*logger
);
658 // this can be used to temporarily disable (and then reenable) passing
659 // messages to the old logger (by default we do pass them)
660 void PassMessages(bool bDoPass
) { m_bPassMessages
= bDoPass
; }
662 // are we passing the messages to the previous log target?
663 bool IsPassingMessages() const { return m_bPassMessages
; }
665 // return the previous log target (may be NULL)
666 wxLog
*GetOldLog() const { return m_logOld
; }
668 // override base class version to flush the old logger as well
669 virtual void Flush();
671 // call to avoid destroying the old log target
672 void DetachOldLog() { m_logOld
= NULL
; }
675 // pass the record to the old logger if needed
676 virtual void DoLogRecord(wxLogLevel level
,
678 const wxLogRecordInfo
& info
);
681 // the current log target
684 // the previous log target
687 // do we pass the messages to the old logger?
688 bool m_bPassMessages
;
690 wxDECLARE_NO_COPY_CLASS(wxLogChain
);
693 // a chain log target which uses itself as the new logger
695 #define wxLogPassThrough wxLogInterposer
697 class WXDLLIMPEXP_BASE wxLogInterposer
: public wxLogChain
703 wxDECLARE_NO_COPY_CLASS(wxLogInterposer
);
706 // a temporary interposer which doesn't destroy the old log target
707 // (calls DetachOldLog)
709 class WXDLLIMPEXP_BASE wxLogInterposerTemp
: public wxLogChain
712 wxLogInterposerTemp();
715 wxDECLARE_NO_COPY_CLASS(wxLogInterposerTemp
);
719 // include GUI log targets:
720 #include "wx/generic/logg.h"
723 // ----------------------------------------------------------------------------
725 // ----------------------------------------------------------------------------
727 // wxLogger is a helper class used by wxLogXXX() functions implementation,
728 // don't use it directly as it's experimental and subject to change (OTOH it
729 // might become public in the future if it's deemed to be useful enough)
731 // contains information about the context from which a log message originates
732 // and provides Log() vararg method which forwards to wxLog::OnLog() and passes
733 // this context to it
737 // ctor takes the basic information about the log record
738 wxLogger(wxLogLevel level
,
739 const char *filename
,
743 m_info(filename
, line
, func
)
747 // store extra data in our log record and return this object itself (so
748 // that further calls to its functions could be chained)
749 template <typename T
>
750 wxLogger
& Store(const wxString
& key
, T val
)
752 m_info
.StoreValue(key
, val
);
756 // hack for "overloaded" wxLogXXX() functions: calling this method
757 // indicates that we may have an extra first argument preceding the format
758 // string and that if we do have it, we should store it in m_info using the
759 // given key (while by default 0 value will be used)
760 wxLogger
& MaybeStore(const wxString
& key
)
762 wxASSERT_MSG( m_optKey
.empty(), "can only have one optional value" );
765 m_info
.StoreValue(key
, 0);
770 // non-vararg function used by wxVLogXXX():
772 // log the message at the level specified in the ctor if this log message
774 void LogV(const wxString
& format
, va_list argptr
)
776 // remember that fatal errors can't be disabled
777 if ( m_level
== wxLOG_FatalError
|| wxLog::IsLevelEnabled(m_level
) )
778 DoCallOnLog(format
, argptr
);
781 // overloads used by functions with optional leading arguments (whose
782 // values are stored in the key passed to MaybeStore())
783 void LogV(long num
, const wxString
& format
, va_list argptr
)
785 Store(m_optKey
, num
);
787 LogV(format
, argptr
);
790 void LogV(void *ptr
, const wxString
& format
, va_list argptr
)
792 Store(m_optKey
, wxPtrToUInt(ptr
));
794 LogV(format
, argptr
);
798 // vararg functions used by wxLogXXX():
800 // will log the message at the level specified in the ctor
802 // notice that this function supposes that the caller already checked that
803 // the level was enabled and does no checks itself
804 WX_DEFINE_VARARG_FUNC_VOID
807 1, (const wxFormatString
&),
811 // same as Log() but with an extra numeric or pointer parameters: this is
812 // used to pass an optional value by storing it in m_info under the name
813 // passed to MaybeStore() and is required to support "overloaded" versions
814 // of wxLogStatus() and wxLogSysError()
815 WX_DEFINE_VARARG_FUNC_VOID
818 2, (long, const wxFormatString
&),
819 DoLogWithNum
, DoLogWithNumUtf8
822 WX_DEFINE_VARARG_FUNC_VOID
825 2, (void *, const wxFormatString
&),
826 DoLogWithPtr
, DoLogWithPtrUtf8
829 // log the message at the level specified as its first argument
831 // as the macros don't have access to the level argument in this case, this
832 // function does check that the level is enabled itself
833 WX_DEFINE_VARARG_FUNC_VOID
836 2, (wxLogLevel
, const wxFormatString
&),
837 DoLogAtLevel
, DoLogAtLevelUtf8
840 // special versions for wxLogTrace() which is passed either string or
841 // integer (TODO) mask as first argument determining whether the message
842 // should be logged or not
843 WX_DEFINE_VARARG_FUNC_VOID
846 2, (const wxString
&, const wxFormatString
&),
847 DoLogTrace
, DoLogTraceUtf8
851 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
852 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
853 1, (const wxString
&),
854 (wxFormatString(f1
)))
855 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
856 1, (const wxCStrData
&),
857 (wxFormatString(f1
)))
858 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
860 (wxFormatString(f1
)))
861 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
863 (wxFormatString(f1
)))
865 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
866 2, (long, const wxString
&),
867 (f1
, wxFormatString(f2
)))
868 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
869 2, (long, const wxCStrData
&),
870 (f1
, wxFormatString(f2
)))
871 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
872 2, (long, const char *),
873 (f1
, wxFormatString(f2
)))
874 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
875 2, (long, const wchar_t *),
876 (f1
, wxFormatString(f2
)))
878 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
879 2, (void *, const wxString
&),
880 (f1
, wxFormatString(f2
)))
881 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
882 2, (void *, const wxCStrData
&),
883 (f1
, wxFormatString(f2
)))
884 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
885 2, (void *, const char *),
886 (f1
, wxFormatString(f2
)))
887 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
888 2, (void *, const wchar_t *),
889 (f1
, wxFormatString(f2
)))
891 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
892 2, (wxLogLevel
, const wxString
&),
893 (f1
, wxFormatString(f2
)))
894 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
895 2, (wxLogLevel
, const wxCStrData
&),
896 (f1
, wxFormatString(f2
)))
897 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
898 2, (wxLogLevel
, const char *),
899 (f1
, wxFormatString(f2
)))
900 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
901 2, (wxLogLevel
, const wchar_t *),
902 (f1
, wxFormatString(f2
)))
904 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
905 2, (const wxString
&, const wxString
&),
906 (f1
, wxFormatString(f2
)))
907 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
908 2, (const wxString
&, const wxCStrData
&),
909 (f1
, wxFormatString(f2
)))
910 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
911 2, (const wxString
&, const char *),
912 (f1
, wxFormatString(f2
)))
913 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
914 2, (const wxString
&, const wchar_t *),
915 (f1
, wxFormatString(f2
)))
916 #endif // __WATCOMC__
919 #if !wxUSE_UTF8_LOCALE_ONLY
920 void DoLog(const wxChar
*format
, ...)
923 va_start(argptr
, format
);
924 DoCallOnLog(format
, argptr
);
928 void DoLogWithNum(long num
, const wxChar
*format
, ...)
930 Store(m_optKey
, num
);
933 va_start(argptr
, format
);
934 DoCallOnLog(format
, argptr
);
938 void DoLogWithPtr(void *ptr
, const wxChar
*format
, ...)
940 Store(m_optKey
, wxPtrToUInt(ptr
));
943 va_start(argptr
, format
);
944 DoCallOnLog(format
, argptr
);
948 void DoLogAtLevel(wxLogLevel level
, const wxChar
*format
, ...)
950 if ( !wxLog::IsLevelEnabled(level
) )
954 va_start(argptr
, format
);
955 DoCallOnLog(level
, format
, argptr
);
959 void DoLogTrace(const wxString
& mask
, const wxChar
*format
, ...)
961 if ( !wxLog::IsAllowedTraceMask(mask
) )
964 Store(wxLOG_KEY_TRACE_MASK
, mask
);
967 va_start(argptr
, format
);
968 DoCallOnLog(format
, argptr
);
971 #endif // !wxUSE_UTF8_LOCALE_ONLY
973 #if wxUSE_UNICODE_UTF8
974 void DoLogUtf8(const char *format
, ...)
977 va_start(argptr
, format
);
978 DoCallOnLog(format
, argptr
);
982 void DoLogWithNumUtf8(long num
, const char *format
, ...)
984 Store(m_optKey
, num
);
987 va_start(argptr
, format
);
988 DoCallOnLog(format
, argptr
);
992 void DoLogWithPtrUtf8(void *ptr
, const char *format
, ...)
994 Store(m_optKey
, wxPtrToUInt(ptr
));
997 va_start(argptr
, format
);
998 DoCallOnLog(format
, argptr
);
1002 void DoLogAtLevelUtf8(wxLogLevel level
, const char *format
, ...)
1004 if ( !wxLog::IsLevelEnabled(level
) )
1008 va_start(argptr
, format
);
1009 DoCallOnLog(level
, format
, argptr
);
1013 void DoLogTraceUtf8(const wxString
& mask
, const char *format
, ...)
1015 if ( !wxLog::IsAllowedTraceMask(mask
) )
1018 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1021 va_start(argptr
, format
);
1022 DoCallOnLog(format
, argptr
);
1025 #endif // wxUSE_UNICODE_UTF8
1027 void DoCallOnLog(wxLogLevel level
, const wxString
& format
, va_list argptr
)
1029 wxLog::OnLog(level
, wxString::FormatV(format
, argptr
), m_info
);
1032 void DoCallOnLog(const wxString
& format
, va_list argptr
)
1034 wxLog::OnLog(m_level
, wxString::FormatV(format
, argptr
), m_info
);
1038 const wxLogLevel m_level
;
1039 wxLogRecordInfo m_info
;
1043 wxDECLARE_NO_COPY_CLASS(wxLogger
);
1046 // ============================================================================
1048 // ============================================================================
1050 // ----------------------------------------------------------------------------
1051 // get error code/error message from system in a portable way
1052 // ----------------------------------------------------------------------------
1054 // return the last system error code
1055 WXDLLIMPEXP_BASE
unsigned long wxSysErrorCode();
1057 // return the error message for given (or last if 0) error code
1058 WXDLLIMPEXP_BASE
const wxChar
* wxSysErrorMsg(unsigned long nErrCode
= 0);
1060 // ----------------------------------------------------------------------------
1061 // define wxLog<level>() functions which can be used by application instead of
1062 // stdio, iostream &c for log messages for easy redirection
1063 // ----------------------------------------------------------------------------
1066 The code below is unreadable because it (unfortunately unavoidably)
1067 contains a lot of macro magic but all it does is to define wxLogXXX() such
1068 that you can call them as vararg functions to log a message at the
1069 corresponding level.
1071 More precisely, it defines:
1073 - wxLog{FatalError,Error,Warning,Message,Verbose,Debug}() functions
1074 taking the format string and additional vararg arguments if needed.
1075 - wxLogGeneric(wxLogLevel level, const wxString& format, ...) which
1076 takes the log level explicitly.
1077 - wxLogSysError(const wxString& format, ...) and wxLogSysError(long
1078 err, const wxString& format, ...) which log a wxLOG_Error severity
1079 message with the error message corresponding to the system error code
1080 err or the last error.
1081 - wxLogStatus(const wxString& format, ...) which logs the message into
1082 the status bar of the main application window and its overload
1083 wxLogStatus(wxFrame *frame, const wxString& format, ...) which logs it
1084 into the status bar of the specified frame.
1085 - wxLogTrace(Mask mask, const wxString& format, ...) which only logs
1086 the message is the specified mask is enabled. This comes in two kinds:
1087 Mask can be a wxString or a long. Both are deprecated.
1089 In addition, wxVLogXXX() versions of all the functions above are also
1090 defined. They take a va_list argument instead of "...".
1093 // creates wxLogger object for the current location
1094 #define wxMAKE_LOGGER(level) \
1095 wxLogger(wxLOG_##level, __FILE__, __LINE__, __WXFUNCTION__)
1097 // this macro generates the expression which logs whatever follows it in
1098 // parentheses at the level specified as argument
1099 #define wxDO_LOG(level) wxMAKE_LOGGER(level).Log
1101 // this is the non-vararg equivalent
1102 #define wxDO_LOGV(level, format, argptr) \
1103 wxMAKE_LOGGER(level).LogV(format, argptr)
1105 // this macro declares wxLog<level>() macro which logs whatever follows it if
1106 // logging at specified level is enabled (notice that if it is false, the
1107 // following arguments are not even evaluated which is good as it avoids
1108 // unnecessary overhead)
1110 // Note: the strange if/else construct is needed to make the following code
1113 // wxLogError("!!!");
1117 // work as expected, without it the second "else" would match the "if"
1118 // inside wxLogError(). Unfortunately code like
1121 // wxLogError("!!!");
1123 // now provokes "suggest explicit braces to avoid ambiguous 'else'"
1124 // warnings from g++ 4.3 and later with -Wparentheses on but they can be
1125 // easily fixed by adding curly braces around wxLogError() and at least
1126 // the code still does do the right thing.
1127 #define wxDO_LOG_IF_ENABLED(level) \
1128 if ( !wxLog::IsLevelEnabled(wxLOG_##level) ) \
1133 // wxLogFatalError() is special as it can't be disabled
1134 #define wxLogFatalError wxDO_LOG(FatalError)
1135 #define wxVLogFatalError(format, argptr) wxDO_LOGV(FatalError, format, argptr)
1137 #define wxLogError wxDO_LOG_IF_ENABLED(Error)
1138 #define wxVLogError(format, argptr) wxDO_LOGV(Error, format, argptr)
1140 #define wxLogWarning wxDO_LOG_IF_ENABLED(Warning)
1141 #define wxVLogWarning(format, argptr) wxDO_LOGV(Warning, format, argptr)
1143 #define wxLogMessage wxDO_LOG_IF_ENABLED(Message)
1144 #define wxVLogMessage(format, argptr) wxDO_LOGV(Message, format, argptr)
1146 // this one is special as it only logs if we're in verbose mode
1147 #define wxLogVerbose \
1148 if ( !(wxLog::IsLevelEnabled(wxLOG_Info) && wxLog::GetVerbose()) ) \
1152 #define wxVLogVerbose(format, argptr) \
1153 if ( !(wxLog::IsLevelEnabled(wxLOG_Info) && wxLog::GetVerbose()) ) \
1156 wxDO_LOGV(Info, format, argptr)
1158 // deprecated synonyms for wxLogVerbose() and wxVLogVerbose()
1159 #define wxLogInfo wxLogVerbose
1160 #define wxVLogInfo wxVLogVerbose
1163 // another special case: the level is passed as first argument of the function
1164 // and so is not available to the macro
1166 // notice that because of this, arguments of wxLogGeneric() are currently
1167 // always evaluated, unlike for the other log functions
1168 #define wxLogGeneric wxMAKE_LOGGER(Max).LogAtLevel
1169 #define wxVLogGeneric(level, format, argptr) \
1170 if ( !wxLog::IsLevelEnabled(wxLOG_##level) ) \
1173 wxDO_LOGV(level, format, argptr)
1176 // wxLogSysError() needs to stash the error code value in the log record info
1177 // so it needs special handling too; additional complications arise because the
1178 // error code may or not be present as the first argument
1179 #define wxLOG_KEY_SYS_ERROR_CODE "wx.sys_error"
1181 #define wxLogSysError \
1182 if ( !wxLog::IsLevelEnabled(wxLOG_Error) ) \
1185 wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE).Log
1187 // unfortunately we can't have overloaded macros so we can't define versions
1188 // both with and without error code argument and have to rely on LogV()
1189 // overloads in wxLogger to select between them
1190 #define wxVLogSysError \
1191 wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE).LogV
1194 // wxLogStatus() is similar to wxLogSysError() as it allows to optionally
1195 // specify the frame to which the message should go
1196 #define wxLOG_KEY_FRAME "wx.frame"
1198 #define wxLogStatus \
1199 if ( !wxLog::IsLevelEnabled(wxLOG_Status) ) \
1202 wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).Log
1204 #define wxVLogStatus(format, argptr) \
1205 wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).LogV
1211 #undef wxUSE_LOG_DEBUG
1212 #define wxUSE_LOG_DEBUG 0
1214 #undef wxUSE_LOG_TRACE
1215 #define wxUSE_LOG_TRACE 0
1217 #if defined(__WATCOMC__) || defined(__MINGW32__)
1218 // Mingw has similar problem with wxLogSysError:
1219 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x ) x
1221 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x )
1224 // define macros for defining log functions which do nothing at all
1226 // WX_WATCOM_ONLY_CODE is needed to work around
1227 // http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1228 #define wxDEFINE_EMPTY_LOG_FUNCTION(level) \
1229 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxString&)) \
1230 WX_WATCOM_ONLY_CODE( \
1231 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const char*)) \
1232 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wchar_t*)) \
1233 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxCStrData&)) \
1235 inline void wxVLog##level(const wxString& WXUNUSED(format), \
1236 va_list WXUNUSED(argptr)) { } \
1238 #define wxDEFINE_EMPTY_LOG_FUNCTION2(level, argclass) \
1239 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxString&)) \
1240 WX_WATCOM_OR_MINGW_ONLY_CODE( \
1241 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const char*)) \
1242 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wchar_t*)) \
1243 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxCStrData&)) \
1245 inline void wxVLog##level(argclass WXUNUSED(arg), \
1246 const wxString& WXUNUSED(format), \
1247 va_list WXUNUSED(argptr)) {}
1249 wxDEFINE_EMPTY_LOG_FUNCTION(FatalError
);
1250 wxDEFINE_EMPTY_LOG_FUNCTION(Error
);
1251 wxDEFINE_EMPTY_LOG_FUNCTION(SysError
);
1252 wxDEFINE_EMPTY_LOG_FUNCTION2(SysError
, long);
1253 wxDEFINE_EMPTY_LOG_FUNCTION(Warning
);
1254 wxDEFINE_EMPTY_LOG_FUNCTION(Message
);
1255 wxDEFINE_EMPTY_LOG_FUNCTION(Info
);
1256 wxDEFINE_EMPTY_LOG_FUNCTION(Verbose
);
1258 wxDEFINE_EMPTY_LOG_FUNCTION2(Generic
, wxLogLevel
);
1261 wxDEFINE_EMPTY_LOG_FUNCTION(Status
);
1262 wxDEFINE_EMPTY_LOG_FUNCTION2(Status
, wxFrame
*);
1265 // Empty Class to fake wxLogNull
1266 class WXDLLIMPEXP_BASE wxLogNull
1272 // Dummy macros to replace some functions.
1273 #define wxSysErrorCode() (unsigned long)0
1274 #define wxSysErrorMsg( X ) (const wxChar*)NULL
1276 // Fake symbolic trace masks... for those that are used frequently
1277 #define wxTRACE_OleCalls wxEmptyString // OLE interface calls
1279 #endif // wxUSE_LOG/!wxUSE_LOG
1282 // debug functions can be completely disabled in optimized builds
1284 // if these log functions are disabled, we prefer to define them as (empty)
1285 // variadic macros as this completely removes them and their argument
1286 // evaluation from the object code but if this is not supported by compiler we
1287 // use empty inline functions instead (defining them as nothing would result in
1288 // compiler warnings)
1290 // note that making wxVLogDebug/Trace() themselves (empty inline) functions is
1291 // a bad idea as some compilers are stupid enough to not inline even empty
1292 // functions if their parameters are complicated enough, but by defining them
1293 // as an empty inline function we ensure that even dumbest compilers optimise
1296 // but Borland gives "W8019: Code has no effect" for wxLogNop() so we need
1297 // to define it differently for it to avoid these warnings (same problem as
1298 // with wxUnusedVar())
1299 #define wxLogNop() { }
1301 inline void wxLogNop() { }
1305 #define wxLogDebug wxDO_LOG_IF_ENABLED(Debug)
1306 #define wxVLogDebug(format, argptr) wxDO_LOGV(Debug, format, argptr)
1307 #else // !wxUSE_LOG_DEBUG
1308 #define wxVLogDebug(fmt, valist) wxLogNop()
1310 #ifdef HAVE_VARIADIC_MACROS
1311 #define wxLogDebug(fmt, ...) wxLogNop()
1312 #else // !HAVE_VARIADIC_MACROS
1313 WX_DEFINE_VARARG_FUNC_NOP(wxLogDebug
, 1, (const wxString
&))
1315 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
1318 #define wxLogTrace \
1319 if ( !wxLog::IsLevelEnabled(wxLOG_Trace) ) \
1322 wxMAKE_LOGGER(Trace).LogTrace
1323 #else // !wxUSE_LOG_TRACE
1324 #define wxVLogTrace(mask, fmt, valist) wxLogNop()
1326 #ifdef HAVE_VARIADIC_MACROS
1327 #define wxLogTrace(mask, fmt, ...) wxLogNop()
1328 #else // !HAVE_VARIADIC_MACROS
1329 #if WXWIN_COMPATIBILITY_2_8
1330 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (wxTraceMask
, const wxString
&))
1332 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const wxString
&, const wxString
&))
1334 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1335 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const char*, const char*))
1336 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const wchar_t*, const wchar_t*))
1338 #endif // HAVE_VARIADIC_MACROS/!HAVE_VARIADIC_MACROS
1339 #endif // wxUSE_LOG_TRACE/!wxUSE_LOG_TRACE
1341 // wxLogFatalError helper: show the (fatal) error to the user in a safe way,
1342 // i.e. without using wxMessageBox() for example because it could crash
1343 void WXDLLIMPEXP_BASE
1344 wxSafeShowMessage(const wxString
& title
, const wxString
& text
);
1346 // ----------------------------------------------------------------------------
1347 // debug only logging functions: use them with API name and error code
1348 // ----------------------------------------------------------------------------
1351 // make life easier for people using VC++ IDE: clicking on the message
1352 // will take us immediately to the place of the failed API
1354 #define wxLogApiError(api, rc) \
1355 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
1356 __FILE__, __LINE__, api, \
1357 (long)rc, wxSysErrorMsg(rc))
1359 #define wxLogApiError(api, rc) \
1360 wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \
1361 wxT("error 0x%08lx (%s)."), \
1362 __FILE__, __LINE__, api, \
1363 (long)rc, wxSysErrorMsg(rc))
1364 #endif // VC++/!VC++
1366 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
1368 #else // !wxUSE_LOG_DEBUG
1369 #define wxLogApiError(api, err) wxLogNop()
1370 #define wxLogLastError(api) wxLogNop()
1371 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
1373 // wxCocoa has additiional trace masks
1374 #if defined(__WXCOCOA__)
1375 #include "wx/cocoa/log.h"
1378 #ifdef WX_WATCOM_ONLY_CODE
1379 #undef WX_WATCOM_ONLY_CODE
1382 #endif // _WX_LOG_H_