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
; )
456 // this one can't be marked deprecated as it's used in our own wxLogger
457 // below but it still is deprecated and shouldn't be used
458 static wxTraceMask
GetTraceMask() { return ms_ulTraceMask
; }
459 #endif // WXWIN_COMPATIBILITY_2_8
462 // the logging functions that can be overridden: DoLogRecord() is called
463 // for every "record", i.e. a unit of log output, to be logged and by
464 // default formats the message and passes it to DoLogTextAtLevel() which in
465 // turn passes it to DoLogText() by default
467 // override this method if you want to change message formatting or do
469 virtual void DoLogRecord(wxLogLevel level
,
471 const wxLogRecordInfo
& info
);
473 // override this method to redirect output to different channels depending
474 // on its level only; if even the level doesn't matter, override
475 // DoLogText() instead
476 virtual void DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
);
478 // this function is not pure virtual as it might not be needed if you do
479 // the logging in overridden DoLogRecord() or DoLogTextAtLevel() directly
480 // but if you do not override them in your derived class you must override
481 // this one as the default implementation of it simply asserts
482 virtual void DoLogText(const wxString
& msg
);
485 // the rest of the functions are for backwards compatibility only, don't
486 // use them in new code; if you're updating your existing code you need to
487 // switch to overriding DoLogRecord/Text() above (although as long as these
488 // functions exist, log classes using them will continue to work)
489 #if WXWIN_COMPATIBILITY_2_8
490 wxDEPRECATED_BUT_USED_INTERNALLY(
491 virtual void DoLog(wxLogLevel level
, const char *szString
, time_t t
)
494 wxDEPRECATED_BUT_USED_INTERNALLY(
495 virtual void DoLog(wxLogLevel level
, const wchar_t *wzString
, time_t t
)
498 // these shouldn't be used by new code
499 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
500 virtual void DoLogString(const char *WXUNUSED(szString
),
502 wxEMPTY_PARAMETER_VALUE
505 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
506 virtual void DoLogString(const wchar_t *WXUNUSED(wzString
),
508 wxEMPTY_PARAMETER_VALUE
510 #endif // WXWIN_COMPATIBILITY_2_8
513 // log a message indicating the number of times the previous message was
514 // repeated if previous repetition counter is strictly positive, does
515 // nothing otherwise; return the old value of repetition counter
516 unsigned LogLastRepeatIfNeeded();
519 // implement of LogLastRepeatIfNeeded(): it assumes that the
520 // caller had already locked GetPreviousLogCS()
521 unsigned LogLastRepeatIfNeededUnlocked();
526 // if true, don't log the same message multiple times, only log it once
527 // with the number of times it was repeated
528 static bool ms_bRepetCounting
;
530 static wxLog
*ms_pLogger
; // currently active log sink
531 static bool ms_doLog
; // false => all logging disabled
532 static bool ms_bAutoCreate
; // create new log targets on demand?
533 static bool ms_bVerbose
; // false => ignore LogInfo messages
535 static wxLogLevel ms_logLevel
; // limit logging to levels <= ms_logLevel
537 static size_t ms_suspendCount
; // if positive, logs are not flushed
539 // format string for strftime(), if empty, time stamping log messages is
541 static wxString ms_timestamp
;
543 #if WXWIN_COMPATIBILITY_2_8
544 static wxTraceMask ms_ulTraceMask
; // controls wxLogTrace behaviour
545 #endif // WXWIN_COMPATIBILITY_2_8
547 // currently enabled trace masks
548 static wxArrayString ms_aTraceMasks
;
551 // ----------------------------------------------------------------------------
552 // "trivial" derivations of wxLog
553 // ----------------------------------------------------------------------------
555 // log everything except for the debug/trace messages (which are passed to
556 // wxMessageOutputDebug) to a buffer
557 class WXDLLIMPEXP_BASE wxLogBuffer
: public wxLog
562 // get the string contents with all messages logged
563 const wxString
& GetBuffer() const { return m_str
; }
565 // show the buffer contents to the user in the best possible way (this uses
566 // wxMessageOutputMessageBox) and clear it
567 virtual void Flush();
570 virtual void DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
);
575 wxDECLARE_NO_COPY_CLASS(wxLogBuffer
);
579 // log everything to a "FILE *", stderr by default
580 class WXDLLIMPEXP_BASE wxLogStderr
: public wxLog
583 // redirect log output to a FILE
584 wxLogStderr(FILE *fp
= NULL
);
587 // implement sink function
588 virtual void DoLogText(const wxString
& msg
);
592 wxDECLARE_NO_COPY_CLASS(wxLogStderr
);
595 #if wxUSE_STD_IOSTREAM
597 // log everything to an "ostream", cerr by default
598 class WXDLLIMPEXP_BASE wxLogStream
: public wxLog
601 // redirect log output to an ostream
602 wxLogStream(wxSTD ostream
*ostr
= (wxSTD ostream
*) NULL
);
605 // implement sink function
606 virtual void DoLogText(const wxString
& msg
);
608 // using ptr here to avoid including <iostream.h> from this file
609 wxSTD ostream
*m_ostr
;
612 #endif // wxUSE_STD_IOSTREAM
614 // ----------------------------------------------------------------------------
615 // /dev/null log target: suppress logging until this object goes out of scope
616 // ----------------------------------------------------------------------------
624 // wxFile.Open() normally complains if file can't be opened, we don't
628 if ( !file.Open("bar") )
629 ... process error ourselves ...
631 // ~wxLogNull called, old log sink restored
634 class WXDLLIMPEXP_BASE wxLogNull
637 wxLogNull() : m_flagOld(wxLog::EnableLogging(false)) { }
638 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld
); }
641 bool m_flagOld
; // the previous value of the wxLog::ms_doLog
644 // ----------------------------------------------------------------------------
645 // chaining log target: installs itself as a log target and passes all
646 // messages to the real log target given to it in the ctor but also forwards
647 // them to the previously active one
649 // note that you don't have to call SetActiveTarget() with this class, it
650 // does it itself in its ctor
651 // ----------------------------------------------------------------------------
653 class WXDLLIMPEXP_BASE wxLogChain
: public wxLog
656 wxLogChain(wxLog
*logger
);
657 virtual ~wxLogChain();
659 // change the new log target
660 void SetLog(wxLog
*logger
);
662 // this can be used to temporarily disable (and then reenable) passing
663 // messages to the old logger (by default we do pass them)
664 void PassMessages(bool bDoPass
) { m_bPassMessages
= bDoPass
; }
666 // are we passing the messages to the previous log target?
667 bool IsPassingMessages() const { return m_bPassMessages
; }
669 // return the previous log target (may be NULL)
670 wxLog
*GetOldLog() const { return m_logOld
; }
672 // override base class version to flush the old logger as well
673 virtual void Flush();
675 // call to avoid destroying the old log target
676 void DetachOldLog() { m_logOld
= NULL
; }
679 // pass the record to the old logger if needed
680 virtual void DoLogRecord(wxLogLevel level
,
682 const wxLogRecordInfo
& info
);
685 // the current log target
688 // the previous log target
691 // do we pass the messages to the old logger?
692 bool m_bPassMessages
;
694 wxDECLARE_NO_COPY_CLASS(wxLogChain
);
697 // a chain log target which uses itself as the new logger
699 #define wxLogPassThrough wxLogInterposer
701 class WXDLLIMPEXP_BASE wxLogInterposer
: public wxLogChain
707 wxDECLARE_NO_COPY_CLASS(wxLogInterposer
);
710 // a temporary interposer which doesn't destroy the old log target
711 // (calls DetachOldLog)
713 class WXDLLIMPEXP_BASE wxLogInterposerTemp
: public wxLogChain
716 wxLogInterposerTemp();
719 wxDECLARE_NO_COPY_CLASS(wxLogInterposerTemp
);
723 // include GUI log targets:
724 #include "wx/generic/logg.h"
727 // ----------------------------------------------------------------------------
729 // ----------------------------------------------------------------------------
731 // wxLogger is a helper class used by wxLogXXX() functions implementation,
732 // don't use it directly as it's experimental and subject to change (OTOH it
733 // might become public in the future if it's deemed to be useful enough)
735 // contains information about the context from which a log message originates
736 // and provides Log() vararg method which forwards to wxLog::OnLog() and passes
737 // this context to it
741 // ctor takes the basic information about the log record
742 wxLogger(wxLogLevel level
,
743 const char *filename
,
747 m_info(filename
, line
, func
)
751 // store extra data in our log record and return this object itself (so
752 // that further calls to its functions could be chained)
753 template <typename T
>
754 wxLogger
& Store(const wxString
& key
, T val
)
756 m_info
.StoreValue(key
, val
);
760 // hack for "overloaded" wxLogXXX() functions: calling this method
761 // indicates that we may have an extra first argument preceding the format
762 // string and that if we do have it, we should store it in m_info using the
763 // given key (while by default 0 value will be used)
764 wxLogger
& MaybeStore(const wxString
& key
)
766 wxASSERT_MSG( m_optKey
.empty(), "can only have one optional value" );
769 m_info
.StoreValue(key
, 0);
774 // non-vararg function used by wxVLogXXX():
776 // log the message at the level specified in the ctor if this log message
778 void LogV(const wxString
& format
, va_list argptr
)
780 // remember that fatal errors can't be disabled
781 if ( m_level
== wxLOG_FatalError
|| wxLog::IsLevelEnabled(m_level
) )
782 DoCallOnLog(format
, argptr
);
785 // overloads used by functions with optional leading arguments (whose
786 // values are stored in the key passed to MaybeStore())
787 void LogV(long num
, const wxString
& format
, va_list argptr
)
789 Store(m_optKey
, num
);
791 LogV(format
, argptr
);
794 void LogV(void *ptr
, const wxString
& format
, va_list argptr
)
796 Store(m_optKey
, wxPtrToUInt(ptr
));
798 LogV(format
, argptr
);
802 // vararg functions used by wxLogXXX():
804 // will log the message at the level specified in the ctor
806 // notice that this function supposes that the caller already checked that
807 // the level was enabled and does no checks itself
808 WX_DEFINE_VARARG_FUNC_VOID
811 1, (const wxFormatString
&),
815 // same as Log() but with an extra numeric or pointer parameters: this is
816 // used to pass an optional value by storing it in m_info under the name
817 // passed to MaybeStore() and is required to support "overloaded" versions
818 // of wxLogStatus() and wxLogSysError()
819 WX_DEFINE_VARARG_FUNC_VOID
822 2, (long, const wxFormatString
&),
823 DoLogWithNum
, DoLogWithNumUtf8
826 // unfortunately we can't use "void *" here as we get overload ambiguities
827 // with Log(wxFormatString, ...) when the first argument is a "char *" or
828 // "wchar_t *" then -- so we only allow passing wxObject here, which is
829 // ugly but fine in practice as this overload is only used by wxLogStatus()
830 // whose first argument is a wxFrame
831 WX_DEFINE_VARARG_FUNC_VOID
834 2, (wxObject
*, const wxFormatString
&),
835 DoLogWithPtr
, DoLogWithPtrUtf8
838 // log the message at the level specified as its first argument
840 // as the macros don't have access to the level argument in this case, this
841 // function does check that the level is enabled itself
842 WX_DEFINE_VARARG_FUNC_VOID
845 2, (wxLogLevel
, const wxFormatString
&),
846 DoLogAtLevel
, DoLogAtLevelUtf8
849 // special versions for wxLogTrace() which is passed either string or
850 // integer mask as first argument determining whether the message should be
852 WX_DEFINE_VARARG_FUNC_VOID
855 2, (const wxString
&, const wxFormatString
&),
856 DoLogTrace
, DoLogTraceUtf8
859 #if WXWIN_COMPATIBILITY_2_8
860 WX_DEFINE_VARARG_FUNC_VOID
863 2, (wxTraceMask
, const wxFormatString
&),
864 DoLogTraceMask
, DoLogTraceMaskUtf8
866 #endif // WXWIN_COMPATIBILITY_2_8
869 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
870 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
871 1, (const wxString
&),
872 (wxFormatString(f1
)))
873 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
874 1, (const wxCStrData
&),
875 (wxFormatString(f1
)))
876 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
878 (wxFormatString(f1
)))
879 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
881 (wxFormatString(f1
)))
883 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
884 2, (long, const wxString
&),
885 (f1
, wxFormatString(f2
)))
886 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
887 2, (long, const wxCStrData
&),
888 (f1
, wxFormatString(f2
)))
889 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
890 2, (long, const char *),
891 (f1
, wxFormatString(f2
)))
892 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
893 2, (long, const wchar_t *),
894 (f1
, wxFormatString(f2
)))
896 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
897 2, (wxObject
*, const wxString
&),
898 (f1
, wxFormatString(f2
)))
899 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
900 2, (wxObject
*, const wxCStrData
&),
901 (f1
, wxFormatString(f2
)))
902 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
903 2, (wxObject
*, const char *),
904 (f1
, wxFormatString(f2
)))
905 WX_VARARG_WATCOM_WORKAROUND(void, Log
,
906 2, (wxObject
*, const wchar_t *),
907 (f1
, wxFormatString(f2
)))
909 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
910 2, (wxLogLevel
, const wxString
&),
911 (f1
, wxFormatString(f2
)))
912 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
913 2, (wxLogLevel
, const wxCStrData
&),
914 (f1
, wxFormatString(f2
)))
915 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
916 2, (wxLogLevel
, const char *),
917 (f1
, wxFormatString(f2
)))
918 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel
,
919 2, (wxLogLevel
, const wchar_t *),
920 (f1
, wxFormatString(f2
)))
922 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
923 2, (const wxString
&, const wxString
&),
924 (f1
, wxFormatString(f2
)))
925 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
926 2, (const wxString
&, const wxCStrData
&),
927 (f1
, wxFormatString(f2
)))
928 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
929 2, (const wxString
&, const char *),
930 (f1
, wxFormatString(f2
)))
931 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
932 2, (const wxString
&, const wchar_t *),
933 (f1
, wxFormatString(f2
)))
935 #if WXWIN_COMPATIBILITY_2_8
936 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
937 2, (wxTraceMask
, wxTraceMask
),
938 (f1
, wxFormatString(f2
)))
939 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
940 2, (wxTraceMask
, const wxCStrData
&),
941 (f1
, wxFormatString(f2
)))
942 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
943 2, (wxTraceMask
, const char *),
944 (f1
, wxFormatString(f2
)))
945 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace
,
946 2, (wxTraceMask
, const wchar_t *),
947 (f1
, wxFormatString(f2
)))
948 #endif // WXWIN_COMPATIBILITY_2_8
949 #endif // __WATCOMC__
952 #if !wxUSE_UTF8_LOCALE_ONLY
953 void DoLog(const wxChar
*format
, ...)
956 va_start(argptr
, format
);
957 DoCallOnLog(format
, argptr
);
961 void DoLogWithNum(long num
, const wxChar
*format
, ...)
963 Store(m_optKey
, num
);
966 va_start(argptr
, format
);
967 DoCallOnLog(format
, argptr
);
971 void DoLogWithPtr(void *ptr
, const wxChar
*format
, ...)
973 Store(m_optKey
, wxPtrToUInt(ptr
));
976 va_start(argptr
, format
);
977 DoCallOnLog(format
, argptr
);
981 void DoLogAtLevel(wxLogLevel level
, const wxChar
*format
, ...)
983 if ( !wxLog::IsLevelEnabled(level
) )
987 va_start(argptr
, format
);
988 DoCallOnLog(level
, format
, argptr
);
992 void DoLogTrace(const wxString
& mask
, const wxChar
*format
, ...)
994 if ( !wxLog::IsAllowedTraceMask(mask
) )
997 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1000 va_start(argptr
, format
);
1001 DoCallOnLog(format
, argptr
);
1005 #if WXWIN_COMPATIBILITY_2_8
1006 void DoLogTraceMask(wxTraceMask mask
, const wxChar
*format
, ...)
1008 if ( (wxLog::GetTraceMask() & mask
) != mask
)
1011 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1014 va_start(argptr
, format
);
1015 DoCallOnLog(format
, argptr
);
1018 #endif // WXWIN_COMPATIBILITY_2_8
1019 #endif // !wxUSE_UTF8_LOCALE_ONLY
1021 #if wxUSE_UNICODE_UTF8
1022 void DoLogUtf8(const char *format
, ...)
1025 va_start(argptr
, format
);
1026 DoCallOnLog(format
, argptr
);
1030 void DoLogWithNumUtf8(long num
, const char *format
, ...)
1032 Store(m_optKey
, num
);
1035 va_start(argptr
, format
);
1036 DoCallOnLog(format
, argptr
);
1040 void DoLogWithPtrUtf8(void *ptr
, const char *format
, ...)
1042 Store(m_optKey
, wxPtrToUInt(ptr
));
1045 va_start(argptr
, format
);
1046 DoCallOnLog(format
, argptr
);
1050 void DoLogAtLevelUtf8(wxLogLevel level
, const char *format
, ...)
1052 if ( !wxLog::IsLevelEnabled(level
) )
1056 va_start(argptr
, format
);
1057 DoCallOnLog(level
, format
, argptr
);
1061 void DoLogTraceUtf8(const wxString
& mask
, const char *format
, ...)
1063 if ( !wxLog::IsAllowedTraceMask(mask
) )
1066 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1069 va_start(argptr
, format
);
1070 DoCallOnLog(format
, argptr
);
1074 #if WXWIN_COMPATIBILITY_2_8
1075 void DoLogTraceMaskUtf8(wxTraceMask mask
, const char *format
, ...)
1077 if ( (wxLog::GetTraceMask() & mask
) != mask
)
1080 Store(wxLOG_KEY_TRACE_MASK
, mask
);
1083 va_start(argptr
, format
);
1084 DoCallOnLog(format
, argptr
);
1087 #endif // WXWIN_COMPATIBILITY_2_8
1088 #endif // wxUSE_UNICODE_UTF8
1090 void DoCallOnLog(wxLogLevel level
, const wxString
& format
, va_list argptr
)
1092 wxLog::OnLog(level
, wxString::FormatV(format
, argptr
), m_info
);
1095 void DoCallOnLog(const wxString
& format
, va_list argptr
)
1097 wxLog::OnLog(m_level
, wxString::FormatV(format
, argptr
), m_info
);
1101 const wxLogLevel m_level
;
1102 wxLogRecordInfo m_info
;
1106 wxDECLARE_NO_COPY_CLASS(wxLogger
);
1109 // ============================================================================
1111 // ============================================================================
1113 // ----------------------------------------------------------------------------
1114 // get error code/error message from system in a portable way
1115 // ----------------------------------------------------------------------------
1117 // return the last system error code
1118 WXDLLIMPEXP_BASE
unsigned long wxSysErrorCode();
1120 // return the error message for given (or last if 0) error code
1121 WXDLLIMPEXP_BASE
const wxChar
* wxSysErrorMsg(unsigned long nErrCode
= 0);
1123 // ----------------------------------------------------------------------------
1124 // define wxLog<level>() functions which can be used by application instead of
1125 // stdio, iostream &c for log messages for easy redirection
1126 // ----------------------------------------------------------------------------
1129 The code below is unreadable because it (unfortunately unavoidably)
1130 contains a lot of macro magic but all it does is to define wxLogXXX() such
1131 that you can call them as vararg functions to log a message at the
1132 corresponding level.
1134 More precisely, it defines:
1136 - wxLog{FatalError,Error,Warning,Message,Verbose,Debug}() functions
1137 taking the format string and additional vararg arguments if needed.
1138 - wxLogGeneric(wxLogLevel level, const wxString& format, ...) which
1139 takes the log level explicitly.
1140 - wxLogSysError(const wxString& format, ...) and wxLogSysError(long
1141 err, const wxString& format, ...) which log a wxLOG_Error severity
1142 message with the error message corresponding to the system error code
1143 err or the last error.
1144 - wxLogStatus(const wxString& format, ...) which logs the message into
1145 the status bar of the main application window and its overload
1146 wxLogStatus(wxFrame *frame, const wxString& format, ...) which logs it
1147 into the status bar of the specified frame.
1148 - wxLogTrace(Mask mask, const wxString& format, ...) which only logs
1149 the message is the specified mask is enabled. This comes in two kinds:
1150 Mask can be a wxString or a long. Both are deprecated.
1152 In addition, wxVLogXXX() versions of all the functions above are also
1153 defined. They take a va_list argument instead of "...".
1156 // creates wxLogger object for the current location
1157 #define wxMAKE_LOGGER(level) \
1158 wxLogger(wxLOG_##level, __FILE__, __LINE__, __WXFUNCTION__)
1160 // this macro generates the expression which logs whatever follows it in
1161 // parentheses at the level specified as argument
1162 #define wxDO_LOG(level) wxMAKE_LOGGER(level).Log
1164 // this is the non-vararg equivalent
1165 #define wxDO_LOGV(level, format, argptr) \
1166 wxMAKE_LOGGER(level).LogV(format, argptr)
1168 // this macro declares wxLog<level>() macro which logs whatever follows it if
1169 // logging at specified level is enabled (notice that if it is false, the
1170 // following arguments are not even evaluated which is good as it avoids
1171 // unnecessary overhead)
1173 // Note: the strange if/else construct is needed to make the following code
1176 // wxLogError("!!!");
1180 // work as expected, without it the second "else" would match the "if"
1181 // inside wxLogError(). Unfortunately code like
1184 // wxLogError("!!!");
1186 // now provokes "suggest explicit braces to avoid ambiguous 'else'"
1187 // warnings from g++ 4.3 and later with -Wparentheses on but they can be
1188 // easily fixed by adding curly braces around wxLogError() and at least
1189 // the code still does do the right thing.
1190 #define wxDO_LOG_IF_ENABLED(level) \
1191 if ( !wxLog::IsLevelEnabled(wxLOG_##level) ) \
1196 // wxLogFatalError() is special as it can't be disabled
1197 #define wxLogFatalError wxDO_LOG(FatalError)
1198 #define wxVLogFatalError(format, argptr) wxDO_LOGV(FatalError, format, argptr)
1200 #define wxLogError wxDO_LOG_IF_ENABLED(Error)
1201 #define wxVLogError(format, argptr) wxDO_LOGV(Error, format, argptr)
1203 #define wxLogWarning wxDO_LOG_IF_ENABLED(Warning)
1204 #define wxVLogWarning(format, argptr) wxDO_LOGV(Warning, format, argptr)
1206 #define wxLogMessage wxDO_LOG_IF_ENABLED(Message)
1207 #define wxVLogMessage(format, argptr) wxDO_LOGV(Message, format, argptr)
1209 // this one is special as it only logs if we're in verbose mode
1210 #define wxLogVerbose \
1211 if ( !(wxLog::IsLevelEnabled(wxLOG_Info) && wxLog::GetVerbose()) ) \
1215 #define wxVLogVerbose(format, argptr) \
1216 if ( !(wxLog::IsLevelEnabled(wxLOG_Info) && wxLog::GetVerbose()) ) \
1219 wxDO_LOGV(Info, format, argptr)
1221 // deprecated synonyms for wxLogVerbose() and wxVLogVerbose()
1222 #define wxLogInfo wxLogVerbose
1223 #define wxVLogInfo wxVLogVerbose
1226 // another special case: the level is passed as first argument of the function
1227 // and so is not available to the macro
1229 // notice that because of this, arguments of wxLogGeneric() are currently
1230 // always evaluated, unlike for the other log functions
1231 #define wxLogGeneric wxMAKE_LOGGER(Max).LogAtLevel
1232 #define wxVLogGeneric(level, format, argptr) \
1233 if ( !wxLog::IsLevelEnabled(wxLOG_##level) ) \
1236 wxDO_LOGV(level, format, argptr)
1239 // wxLogSysError() needs to stash the error code value in the log record info
1240 // so it needs special handling too; additional complications arise because the
1241 // error code may or not be present as the first argument
1242 #define wxLOG_KEY_SYS_ERROR_CODE "wx.sys_error"
1244 #define wxLogSysError \
1245 if ( !wxLog::IsLevelEnabled(wxLOG_Error) ) \
1248 wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE).Log
1250 // unfortunately we can't have overloaded macros so we can't define versions
1251 // both with and without error code argument and have to rely on LogV()
1252 // overloads in wxLogger to select between them
1253 #define wxVLogSysError \
1254 wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE).LogV
1257 // wxLogStatus() is similar to wxLogSysError() as it allows to optionally
1258 // specify the frame to which the message should go
1259 #define wxLOG_KEY_FRAME "wx.frame"
1261 #define wxLogStatus \
1262 if ( !wxLog::IsLevelEnabled(wxLOG_Status) ) \
1265 wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).Log
1267 #define wxVLogStatus(format, argptr) \
1268 wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).LogV
1274 #undef wxUSE_LOG_DEBUG
1275 #define wxUSE_LOG_DEBUG 0
1277 #undef wxUSE_LOG_TRACE
1278 #define wxUSE_LOG_TRACE 0
1280 #if defined(__WATCOMC__) || defined(__MINGW32__)
1281 // Mingw has similar problem with wxLogSysError:
1282 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x ) x
1284 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x )
1287 // define macros for defining log functions which do nothing at all
1289 // WX_WATCOM_ONLY_CODE is needed to work around
1290 // http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1291 #define wxDEFINE_EMPTY_LOG_FUNCTION(level) \
1292 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxString&)) \
1293 WX_WATCOM_ONLY_CODE( \
1294 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const char*)) \
1295 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wchar_t*)) \
1296 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxCStrData&)) \
1298 inline void wxVLog##level(const wxString& WXUNUSED(format), \
1299 va_list WXUNUSED(argptr)) { } \
1301 #define wxDEFINE_EMPTY_LOG_FUNCTION2(level, argclass) \
1302 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxString&)) \
1303 WX_WATCOM_OR_MINGW_ONLY_CODE( \
1304 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const char*)) \
1305 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wchar_t*)) \
1306 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxCStrData&)) \
1308 inline void wxVLog##level(argclass WXUNUSED(arg), \
1309 const wxString& WXUNUSED(format), \
1310 va_list WXUNUSED(argptr)) {}
1312 wxDEFINE_EMPTY_LOG_FUNCTION(FatalError
);
1313 wxDEFINE_EMPTY_LOG_FUNCTION(Error
);
1314 wxDEFINE_EMPTY_LOG_FUNCTION(SysError
);
1315 wxDEFINE_EMPTY_LOG_FUNCTION2(SysError
, long);
1316 wxDEFINE_EMPTY_LOG_FUNCTION(Warning
);
1317 wxDEFINE_EMPTY_LOG_FUNCTION(Message
);
1318 wxDEFINE_EMPTY_LOG_FUNCTION(Info
);
1319 wxDEFINE_EMPTY_LOG_FUNCTION(Verbose
);
1321 wxDEFINE_EMPTY_LOG_FUNCTION2(Generic
, wxLogLevel
);
1324 wxDEFINE_EMPTY_LOG_FUNCTION(Status
);
1325 wxDEFINE_EMPTY_LOG_FUNCTION2(Status
, wxFrame
*);
1328 // Empty Class to fake wxLogNull
1329 class WXDLLIMPEXP_BASE wxLogNull
1335 // Dummy macros to replace some functions.
1336 #define wxSysErrorCode() (unsigned long)0
1337 #define wxSysErrorMsg( X ) (const wxChar*)NULL
1339 // Fake symbolic trace masks... for those that are used frequently
1340 #define wxTRACE_OleCalls wxEmptyString // OLE interface calls
1342 #endif // wxUSE_LOG/!wxUSE_LOG
1345 // debug functions can be completely disabled in optimized builds
1347 // if these log functions are disabled, we prefer to define them as (empty)
1348 // variadic macros as this completely removes them and their argument
1349 // evaluation from the object code but if this is not supported by compiler we
1350 // use empty inline functions instead (defining them as nothing would result in
1351 // compiler warnings)
1353 // note that making wxVLogDebug/Trace() themselves (empty inline) functions is
1354 // a bad idea as some compilers are stupid enough to not inline even empty
1355 // functions if their parameters are complicated enough, but by defining them
1356 // as an empty inline function we ensure that even dumbest compilers optimise
1359 // but Borland gives "W8019: Code has no effect" for wxLogNop() so we need
1360 // to define it differently for it to avoid these warnings (same problem as
1361 // with wxUnusedVar())
1362 #define wxLogNop() { }
1364 inline void wxLogNop() { }
1368 #define wxLogDebug wxDO_LOG_IF_ENABLED(Debug)
1369 #define wxVLogDebug(format, argptr) wxDO_LOGV(Debug, format, argptr)
1370 #else // !wxUSE_LOG_DEBUG
1371 #define wxVLogDebug(fmt, valist) wxLogNop()
1373 #ifdef HAVE_VARIADIC_MACROS
1374 #define wxLogDebug(fmt, ...) wxLogNop()
1375 #else // !HAVE_VARIADIC_MACROS
1376 WX_DEFINE_VARARG_FUNC_NOP(wxLogDebug
, 1, (const wxString
&))
1378 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
1381 #define wxLogTrace \
1382 if ( !wxLog::IsLevelEnabled(wxLOG_Trace) ) \
1385 wxMAKE_LOGGER(Trace).LogTrace
1386 #else // !wxUSE_LOG_TRACE
1387 #define wxVLogTrace(mask, fmt, valist) wxLogNop()
1389 #ifdef HAVE_VARIADIC_MACROS
1390 #define wxLogTrace(mask, fmt, ...) wxLogNop()
1391 #else // !HAVE_VARIADIC_MACROS
1392 #if WXWIN_COMPATIBILITY_2_8
1393 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (wxTraceMask
, const wxString
&))
1395 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const wxString
&, const wxString
&))
1397 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1398 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const char*, const char*))
1399 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace
, 2, (const wchar_t*, const wchar_t*))
1401 #endif // HAVE_VARIADIC_MACROS/!HAVE_VARIADIC_MACROS
1402 #endif // wxUSE_LOG_TRACE/!wxUSE_LOG_TRACE
1404 // wxLogFatalError helper: show the (fatal) error to the user in a safe way,
1405 // i.e. without using wxMessageBox() for example because it could crash
1406 void WXDLLIMPEXP_BASE
1407 wxSafeShowMessage(const wxString
& title
, const wxString
& text
);
1409 // ----------------------------------------------------------------------------
1410 // debug only logging functions: use them with API name and error code
1411 // ----------------------------------------------------------------------------
1414 // make life easier for people using VC++ IDE: clicking on the message
1415 // will take us immediately to the place of the failed API
1417 #define wxLogApiError(api, rc) \
1418 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
1419 __FILE__, __LINE__, api, \
1420 (long)rc, wxSysErrorMsg(rc))
1422 #define wxLogApiError(api, rc) \
1423 wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \
1424 wxT("error 0x%08lx (%s)."), \
1425 __FILE__, __LINE__, api, \
1426 (long)rc, wxSysErrorMsg(rc))
1427 #endif // VC++/!VC++
1429 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
1431 #else // !wxUSE_LOG_DEBUG
1432 #define wxLogApiError(api, err) wxLogNop()
1433 #define wxLogLastError(api) wxLogNop()
1434 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
1436 // wxCocoa has additiional trace masks
1437 #if defined(__WXCOCOA__)
1438 #include "wx/cocoa/log.h"
1441 #ifdef WX_WATCOM_ONLY_CODE
1442 #undef WX_WATCOM_ONLY_CODE
1445 #endif // _WX_LOG_H_