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 mask as first argument determining whether the message should be
843 WX_DEFINE_VARARG_FUNC_VOID
846 2, (const wxString
&, const wxFormatString
&),
847 DoLogTrace
, DoLogTraceUtf8
850 #if WXWIN_COMPATIBILITY_2_8
851 WX_DEFINE_VARARG_FUNC_VOID
854 2, (wxTraceMask
, const wxFormatString
&),
855 DoLogTraceMask
, DoLogTraceMaskUtf8
857 #endif // WXWIN_COMPATIBILITY_2_8
860 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
861 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
862 1, (const wxString
&),
863 (wxFormatString(f1
)))
864 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
865 1, (const wxCStrData
&),
866 (wxFormatString(f1
)))
867 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
869 (wxFormatString(f1
)))
870 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
872 (wxFormatString(f1
)))
874 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
875 2, (long, const wxString
&),
876 (f1
, wxFormatString(f2
)))
877 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
878 2, (long, const wxCStrData
&),
879 (f1
, wxFormatString(f2
)))
880 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
881 2, (long, const char *),
882 (f1
, wxFormatString(f2
)))
883 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
884 2, (long, const wchar_t *),
885 (f1
, wxFormatString(f2
)))
887 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
888 2, (void *, const wxString
&),
889 (f1
, wxFormatString(f2
)))
890 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
891 2, (void *, const wxCStrData
&),
892 (f1
, wxFormatString(f2
)))
893 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
894 2, (void *, const char *),
895 (f1
, wxFormatString(f2
)))
896 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
897 2, (void *, const wchar_t *),
898 (f1
, wxFormatString(f2
)))
900 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
901 2, (wxLogLevel
, const wxString
&),
902 (f1
, wxFormatString(f2
)))
903 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
904 2, (wxLogLevel
, const wxCStrData
&),
905 (f1
, wxFormatString(f2
)))
906 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
907 2, (wxLogLevel
, const char *),
908 (f1
, wxFormatString(f2
)))
909 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
910 2, (wxLogLevel
, const wchar_t *),
911 (f1
, wxFormatString(f2
)))
913 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
914 2, (const wxString
&, const wxString
&),
915 (f1
, wxFormatString(f2
)))
916 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
917 2, (const wxString
&, const wxCStrData
&),
918 (f1
, wxFormatString(f2
)))
919 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
920 2, (const wxString
&, const char *),
921 (f1
, wxFormatString(f2
)))
922 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
923 2, (const wxString
&, const wchar_t *),
924 (f1
, wxFormatString(f2
)))
926 #if WXWIN_COMPATIBILITY_2_8
927 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
928 2, (wxTraceMask
, wxTraceMask
),
929 (f1
, wxFormatString(f2
)))
930 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
931 2, (wxTraceMask
, const wxCStrData
&),
932 (f1
, wxFormatString(f2
)))
933 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
934 2, (wxTraceMask
, const char *),
935 (f1
, wxFormatString(f2
)))
936 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
937 2, (wxTraceMask
, const wchar_t *),
938 (f1
, wxFormatString(f2
)))
939 #endif // WXWIN_COMPATIBILITY_2_8
940 #endif // __WATCOMC__
943 #if !wxUSE_UTF8_LOCALE_ONLY
944 void DoLog(const wxChar
*format
, ...)
947 va_start(argptr
, format
);
948 DoCallOnLog(format
, argptr
);
952 void DoLogWithNum(long num
, const wxChar
*format
, ...)
954 Store(m_optKey
, num
);
957 va_start(argptr
, format
);
958 DoCallOnLog(format
, argptr
);
962 void DoLogWithPtr(void *ptr
, const wxChar
*format
, ...)
964 Store(m_optKey
, wxPtrToUInt(ptr
));
967 va_start(argptr
, format
);
968 DoCallOnLog(format
, argptr
);
972 void DoLogAtLevel(wxLogLevel level
, const wxChar
*format
, ...)
974 if ( !wxLog::IsLevelEnabled(level
) )
978 va_start(argptr
, format
);
979 DoCallOnLog(level
, format
, argptr
);
983 void DoLogTrace(const wxString
& mask
, const wxChar
*format
, ...)
985 if ( !wxLog::IsAllowedTraceMask(mask
) )
988 Store(wxLOG_KEY_TRACE_MASK
, mask
);
991 va_start(argptr
, format
);
992 DoCallOnLog(format
, argptr
);
996 #if WXWIN_COMPATIBILITY_2_8
997 void DoLogTraceMask(wxTraceMask mask
, const wxChar
*format
, ...)
999 if ( (wxLog::GetTraceMask() & mask
) != mask
)
1002 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1005 va_start(argptr
, format
);
1006 DoCallOnLog(format
, argptr
);
1009 #endif // WXWIN_COMPATIBILITY_2_8
1010 #endif // !wxUSE_UTF8_LOCALE_ONLY
1012 #if wxUSE_UNICODE_UTF8
1013 void DoLogUtf8(const char *format
, ...)
1016 va_start(argptr
, format
);
1017 DoCallOnLog(format
, argptr
);
1021 void DoLogWithNumUtf8(long num
, const char *format
, ...)
1023 Store(m_optKey
, num
);
1026 va_start(argptr
, format
);
1027 DoCallOnLog(format
, argptr
);
1031 void DoLogWithPtrUtf8(void *ptr
, const char *format
, ...)
1033 Store(m_optKey
, wxPtrToUInt(ptr
));
1036 va_start(argptr
, format
);
1037 DoCallOnLog(format
, argptr
);
1041 void DoLogAtLevelUtf8(wxLogLevel level
, const char *format
, ...)
1043 if ( !wxLog::IsLevelEnabled(level
) )
1047 va_start(argptr
, format
);
1048 DoCallOnLog(level
, format
, argptr
);
1052 void DoLogTraceUtf8(const wxString
& mask
, const char *format
, ...)
1054 if ( !wxLog::IsAllowedTraceMask(mask
) )
1057 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1060 va_start(argptr
, format
);
1061 DoCallOnLog(format
, argptr
);
1065 #if WXWIN_COMPATIBILITY_2_8
1066 void DoLogTraceMaskUtf8(wxTraceMask mask
, const char *format
, ...)
1068 if ( (wxLog::GetTraceMask() & mask
) != mask
)
1071 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1074 va_start(argptr
, format
);
1075 DoCallOnLog(format
, argptr
);
1078 #endif // WXWIN_COMPATIBILITY_2_8
1079 #endif // wxUSE_UNICODE_UTF8
1081 void DoCallOnLog(wxLogLevel level
, const wxString
& format
, va_list argptr
)
1083 wxLog::OnLog(level
, wxString::FormatV(format
, argptr
), m_info
);
1086 void DoCallOnLog(const wxString
& format
, va_list argptr
)
1088 wxLog::OnLog(m_level
, wxString::FormatV(format
, argptr
), m_info
);
1092 const wxLogLevel m_level
;
1093 wxLogRecordInfo m_info
;
1097 wxDECLARE_NO_COPY_CLASS(wxLogger
);
1100 // ============================================================================
1102 // ============================================================================
1104 // ----------------------------------------------------------------------------
1105 // get error code/error message from system in a portable way
1106 // ----------------------------------------------------------------------------
1108 // return the last system error code
1109 WXDLLIMPEXP_BASE
unsigned long wxSysErrorCode();
1111 // return the error message for given (or last if 0) error code
1112 WXDLLIMPEXP_BASE
const wxChar
* wxSysErrorMsg(unsigned long nErrCode
= 0);
1114 // ----------------------------------------------------------------------------
1115 // define wxLog<level>() functions which can be used by application instead of
1116 // stdio, iostream &c for log messages for easy redirection
1117 // ----------------------------------------------------------------------------
1120 The code below is unreadable because it (unfortunately unavoidably)
1121 contains a lot of macro magic but all it does is to define wxLogXXX() such
1122 that you can call them as vararg functions to log a message at the
1123 corresponding level.
1125 More precisely, it defines:
1127 - wxLog{FatalError,Error,Warning,Message,Verbose,Debug}() functions
1128 taking the format string and additional vararg arguments if needed.
1129 - wxLogGeneric(wxLogLevel level, const wxString& format, ...) which
1130 takes the log level explicitly.
1131 - wxLogSysError(const wxString& format, ...) and wxLogSysError(long
1132 err, const wxString& format, ...) which log a wxLOG_Error severity
1133 message with the error message corresponding to the system error code
1134 err or the last error.
1135 - wxLogStatus(const wxString& format, ...) which logs the message into
1136 the status bar of the main application window and its overload
1137 wxLogStatus(wxFrame *frame, const wxString& format, ...) which logs it
1138 into the status bar of the specified frame.
1139 - wxLogTrace(Mask mask, const wxString& format, ...) which only logs
1140 the message is the specified mask is enabled. This comes in two kinds:
1141 Mask can be a wxString or a long. Both are deprecated.
1143 In addition, wxVLogXXX() versions of all the functions above are also
1144 defined. They take a va_list argument instead of "...".
1147 // creates wxLogger object for the current location
1148 #define wxMAKE_LOGGER(level) \
1149 wxLogger(wxLOG_##level, __FILE__, __LINE__, __WXFUNCTION__)
1151 // this macro generates the expression which logs whatever follows it in
1152 // parentheses at the level specified as argument
1153 #define wxDO_LOG(level) wxMAKE_LOGGER(level).Log
1155 // this is the non-vararg equivalent
1156 #define wxDO_LOGV(level, format, argptr) \
1157 wxMAKE_LOGGER(level).LogV(format, argptr)
1159 // this macro declares wxLog<level>() macro which logs whatever follows it if
1160 // logging at specified level is enabled (notice that if it is false, the
1161 // following arguments are not even evaluated which is good as it avoids
1162 // unnecessary overhead)
1164 // Note: the strange if/else construct is needed to make the following code
1167 // wxLogError("!!!");
1171 // work as expected, without it the second "else" would match the "if"
1172 // inside wxLogError(). Unfortunately code like
1175 // wxLogError("!!!");
1177 // now provokes "suggest explicit braces to avoid ambiguous 'else'"
1178 // warnings from g++ 4.3 and later with -Wparentheses on but they can be
1179 // easily fixed by adding curly braces around wxLogError() and at least
1180 // the code still does do the right thing.
1181 #define wxDO_LOG_IF_ENABLED(level) \
1182 if ( !wxLog::IsLevelEnabled(wxLOG_##level) ) \
1187 // wxLogFatalError() is special as it can't be disabled
1188 #define wxLogFatalError wxDO_LOG(FatalError)
1189 #define wxVLogFatalError(format, argptr) wxDO_LOGV(FatalError, format, argptr)
1191 #define wxLogError wxDO_LOG_IF_ENABLED(Error)
1192 #define wxVLogError(format, argptr) wxDO_LOGV(Error, format, argptr)
1194 #define wxLogWarning wxDO_LOG_IF_ENABLED(Warning)
1195 #define wxVLogWarning(format, argptr) wxDO_LOGV(Warning, format, argptr)
1197 #define wxLogMessage wxDO_LOG_IF_ENABLED(Message)
1198 #define wxVLogMessage(format, argptr) wxDO_LOGV(Message, format, argptr)
1200 // this one is special as it only logs if we're in verbose mode
1201 #define wxLogVerbose \
1202 if ( !(wxLog::IsLevelEnabled(wxLOG_Info) && wxLog::GetVerbose()) ) \
1206 #define wxVLogVerbose(format, argptr) \
1207 if ( !(wxLog::IsLevelEnabled(wxLOG_Info) && wxLog::GetVerbose()) ) \
1210 wxDO_LOGV(Info, format, argptr)
1212 // deprecated synonyms for wxLogVerbose() and wxVLogVerbose()
1213 #define wxLogInfo wxLogVerbose
1214 #define wxVLogInfo wxVLogVerbose
1217 // another special case: the level is passed as first argument of the function
1218 // and so is not available to the macro
1220 // notice that because of this, arguments of wxLogGeneric() are currently
1221 // always evaluated, unlike for the other log functions
1222 #define wxLogGeneric wxMAKE_LOGGER(Max).LogAtLevel
1223 #define wxVLogGeneric(level, format, argptr) \
1224 if ( !wxLog::IsLevelEnabled(wxLOG_##level) ) \
1227 wxDO_LOGV(level, format, argptr)
1230 // wxLogSysError() needs to stash the error code value in the log record info
1231 // so it needs special handling too; additional complications arise because the
1232 // error code may or not be present as the first argument
1233 #define wxLOG_KEY_SYS_ERROR_CODE "wx.sys_error"
1235 #define wxLogSysError \
1236 if ( !wxLog::IsLevelEnabled(wxLOG_Error) ) \
1239 wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE).Log
1241 // unfortunately we can't have overloaded macros so we can't define versions
1242 // both with and without error code argument and have to rely on LogV()
1243 // overloads in wxLogger to select between them
1244 #define wxVLogSysError \
1245 wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE).LogV
1248 // wxLogStatus() is similar to wxLogSysError() as it allows to optionally
1249 // specify the frame to which the message should go
1250 #define wxLOG_KEY_FRAME "wx.frame"
1252 #define wxLogStatus \
1253 if ( !wxLog::IsLevelEnabled(wxLOG_Status) ) \
1256 wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).Log
1258 #define wxVLogStatus(format, argptr) \
1259 wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).LogV
1265 #undef wxUSE_LOG_DEBUG
1266 #define wxUSE_LOG_DEBUG 0
1268 #undef wxUSE_LOG_TRACE
1269 #define wxUSE_LOG_TRACE 0
1271 #if defined(__WATCOMC__) || defined(__MINGW32__)
1272 // Mingw has similar problem with wxLogSysError:
1273 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x ) x
1275 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x )
1278 // define macros for defining log functions which do nothing at all
1280 // WX_WATCOM_ONLY_CODE is needed to work around
1281 // http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1282 #define wxDEFINE_EMPTY_LOG_FUNCTION(level) \
1283 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxString&)) \
1284 WX_WATCOM_ONLY_CODE( \
1285 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const char*)) \
1286 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wchar_t*)) \
1287 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxCStrData&)) \
1289 inline void wxVLog##level(const wxString& WXUNUSED(format), \
1290 va_list WXUNUSED(argptr)) { } \
1292 #define wxDEFINE_EMPTY_LOG_FUNCTION2(level, argclass) \
1293 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxString&)) \
1294 WX_WATCOM_OR_MINGW_ONLY_CODE( \
1295 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const char*)) \
1296 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wchar_t*)) \
1297 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxCStrData&)) \
1299 inline void wxVLog##level(argclass WXUNUSED(arg), \
1300 const wxString& WXUNUSED(format), \
1301 va_list WXUNUSED(argptr)) {}
1303 wxDEFINE_EMPTY_LOG_FUNCTION(FatalError
);
1304 wxDEFINE_EMPTY_LOG_FUNCTION(Error
);
1305 wxDEFINE_EMPTY_LOG_FUNCTION(SysError
);
1306 wxDEFINE_EMPTY_LOG_FUNCTION2(SysError
, long);
1307 wxDEFINE_EMPTY_LOG_FUNCTION(Warning
);
1308 wxDEFINE_EMPTY_LOG_FUNCTION(Message
);
1309 wxDEFINE_EMPTY_LOG_FUNCTION(Info
);
1310 wxDEFINE_EMPTY_LOG_FUNCTION(Verbose
);
1312 wxDEFINE_EMPTY_LOG_FUNCTION2(Generic
, wxLogLevel
);
1315 wxDEFINE_EMPTY_LOG_FUNCTION(Status
);
1316 wxDEFINE_EMPTY_LOG_FUNCTION2(Status
, wxFrame
*);
1319 // Empty Class to fake wxLogNull
1320 class WXDLLIMPEXP_BASE wxLogNull
1326 // Dummy macros to replace some functions.
1327 #define wxSysErrorCode() (unsigned long)0
1328 #define wxSysErrorMsg( X ) (const wxChar*)NULL
1330 // Fake symbolic trace masks... for those that are used frequently
1331 #define wxTRACE_OleCalls wxEmptyString // OLE interface calls
1333 #endif // wxUSE_LOG/!wxUSE_LOG
1336 // debug functions can be completely disabled in optimized builds
1338 // if these log functions are disabled, we prefer to define them as (empty)
1339 // variadic macros as this completely removes them and their argument
1340 // evaluation from the object code but if this is not supported by compiler we
1341 // use empty inline functions instead (defining them as nothing would result in
1342 // compiler warnings)
1344 // note that making wxVLogDebug/Trace() themselves (empty inline) functions is
1345 // a bad idea as some compilers are stupid enough to not inline even empty
1346 // functions if their parameters are complicated enough, but by defining them
1347 // as an empty inline function we ensure that even dumbest compilers optimise
1350 // but Borland gives "W8019: Code has no effect" for wxLogNop() so we need
1351 // to define it differently for it to avoid these warnings (same problem as
1352 // with wxUnusedVar())
1353 #define wxLogNop() { }
1355 inline void wxLogNop() { }
1359 #define wxLogDebug wxDO_LOG_IF_ENABLED(Debug)
1360 #define wxVLogDebug(format, argptr) wxDO_LOGV(Debug, format, argptr)
1361 #else // !wxUSE_LOG_DEBUG
1362 #define wxVLogDebug(fmt, valist) wxLogNop()
1364 #ifdef HAVE_VARIADIC_MACROS
1365 #define wxLogDebug(fmt, ...) wxLogNop()
1366 #else // !HAVE_VARIADIC_MACROS
1367 WX_DEFINE_VARARG_FUNC_NOP(wxLogDebug
, 1, (const wxString
&))
1369 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
1372 #define wxLogTrace \
1373 if ( !wxLog::IsLevelEnabled(wxLOG_Trace) ) \
1376 wxMAKE_LOGGER(Trace).LogTrace
1377 #else // !wxUSE_LOG_TRACE
1378 #define wxVLogTrace(mask, fmt, valist) wxLogNop()
1380 #ifdef HAVE_VARIADIC_MACROS
1381 #define wxLogTrace(mask, fmt, ...) wxLogNop()
1382 #else // !HAVE_VARIADIC_MACROS
1383 #if WXWIN_COMPATIBILITY_2_8
1384 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (wxTraceMask
, const wxString
&))
1386 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const wxString
&, const wxString
&))
1388 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1389 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const char*, const char*))
1390 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const wchar_t*, const wchar_t*))
1392 #endif // HAVE_VARIADIC_MACROS/!HAVE_VARIADIC_MACROS
1393 #endif // wxUSE_LOG_TRACE/!wxUSE_LOG_TRACE
1395 // wxLogFatalError helper: show the (fatal) error to the user in a safe way,
1396 // i.e. without using wxMessageBox() for example because it could crash
1397 void WXDLLIMPEXP_BASE
1398 wxSafeShowMessage(const wxString
& title
, const wxString
& text
);
1400 // ----------------------------------------------------------------------------
1401 // debug only logging functions: use them with API name and error code
1402 // ----------------------------------------------------------------------------
1405 // make life easier for people using VC++ IDE: clicking on the message
1406 // will take us immediately to the place of the failed API
1408 #define wxLogApiError(api, rc) \
1409 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
1410 __FILE__, __LINE__, api, \
1411 (long)rc, wxSysErrorMsg(rc))
1413 #define wxLogApiError(api, rc) \
1414 wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \
1415 wxT("error 0x%08lx (%s)."), \
1416 __FILE__, __LINE__, api, \
1417 (long)rc, wxSysErrorMsg(rc))
1418 #endif // VC++/!VC++
1420 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
1422 #else // !wxUSE_LOG_DEBUG
1423 #define wxLogApiError(api, err) wxLogNop()
1424 #define wxLogLastError(api) wxLogNop()
1425 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
1427 // wxCocoa has additiional trace masks
1428 #if defined(__WXCOCOA__)
1429 #include "wx/cocoa/log.h"
1432 #ifdef WX_WATCOM_ONLY_CODE
1433 #undef WX_WATCOM_ONLY_CODE
1436 #endif // _WX_LOG_H_