+// ----------------------------------------------------------------------------
+// wxLogger
+// ----------------------------------------------------------------------------
+
+// wxLogger is a helper class used by wxLogXXX() functions implementation,
+// don't use it directly as it's experimental and subject to change (OTOH it
+// might become public in the future if it's deemed to be useful enough)
+
+// contains information about the context from which a log message originates
+// and provides Log() vararg method which forwards to wxLog::OnLog() and passes
+// this context to it
+class wxLogger
+{
+public:
+ // ctor takes the basic information about the log record
+ wxLogger(wxLogLevel level,
+ const char *filename,
+ int line,
+ const char *func)
+ : m_level(level),
+ m_info(filename, line, func)
+ {
+ }
+
+ // store extra data in our log record and return this object itself (so
+ // that further calls to its functions could be chained)
+ template <typename T>
+ wxLogger& Store(const wxString& key, T val)
+ {
+ m_info.StoreValue(key, val);
+ return *this;
+ }
+
+ // hack for "overloaded" wxLogXXX() functions: calling this method
+ // indicates that we may have an extra first argument preceding the format
+ // string and that if we do have it, we should store it in m_info using the
+ // given key (while by default 0 value will be used)
+ wxLogger& MaybeStore(const wxString& key)
+ {
+ wxASSERT_MSG( m_optKey.empty(), "can only have one optional value" );
+ m_optKey = key;
+
+ m_info.StoreValue(key, 0);
+ return *this;
+ }
+
+
+ // non-vararg function used by wxVLogXXX():
+
+ // log the message at the level specified in the ctor if this log message
+ // is enabled
+ void LogV(const wxString& format, va_list argptr)
+ {
+ // remember that fatal errors can't be disabled
+ if ( m_level == wxLOG_FatalError || wxLog::IsLevelEnabled(m_level) )
+ DoCallOnLog(format, argptr);
+ }
+
+ // overloads used by functions with optional leading arguments (whose
+ // values are stored in the key passed to MaybeStore())
+ void LogV(long num, const wxString& format, va_list argptr)
+ {
+ Store(m_optKey, num);
+
+ LogV(format, argptr);
+ }
+
+ void LogV(void *ptr, const wxString& format, va_list argptr)
+ {
+ Store(m_optKey, wxPtrToUInt(ptr));
+
+ LogV(format, argptr);
+ }
+
+
+ // vararg functions used by wxLogXXX():
+
+ // will log the message at the level specified in the ctor
+ //
+ // notice that this function supposes that the caller already checked that
+ // the level was enabled and does no checks itself
+ WX_DEFINE_VARARG_FUNC_VOID
+ (
+ Log,
+ 1, (const wxFormatString&),
+ DoLog, DoLogUtf8
+ )
+
+ // same as Log() but with an extra numeric or pointer parameters: this is
+ // used to pass an optional value by storing it in m_info under the name
+ // passed to MaybeStore() and is required to support "overloaded" versions
+ // of wxLogStatus() and wxLogSysError()
+ WX_DEFINE_VARARG_FUNC_VOID
+ (
+ Log,
+ 2, (long, const wxFormatString&),
+ DoLogWithNum, DoLogWithNumUtf8
+ )
+
+ WX_DEFINE_VARARG_FUNC_VOID
+ (
+ Log,
+ 2, (void *, const wxFormatString&),
+ DoLogWithPtr, DoLogWithPtrUtf8
+ )
+
+ // log the message at the level specified as its first argument
+ //
+ // as the macros don't have access to the level argument in this case, this
+ // function does check that the level is enabled itself
+ WX_DEFINE_VARARG_FUNC_VOID
+ (
+ LogAtLevel,
+ 2, (wxLogLevel, const wxFormatString&),
+ DoLogAtLevel, DoLogAtLevelUtf8
+ )
+
+ // special versions for wxLogTrace() which is passed either string or
+ // integer (TODO) mask as first argument determining whether the message
+ // should be logged or not
+ WX_DEFINE_VARARG_FUNC_VOID
+ (
+ LogTrace,
+ 2, (const wxString&, const wxFormatString&),
+ DoLogTrace, DoLogTraceUtf8
+ )
+
+#ifdef __WATCOMC__
+ // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 1, (const wxString&),
+ (wxFormatString(f1)))
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 1, (const wxCStrData&),
+ (wxFormatString(f1)))
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 1, (const char*),
+ (wxFormatString(f1)))
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 1, (const wchar_t*),
+ (wxFormatString(f1)))
+
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 2, (long, const wxString&),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 2, (long, const wxCStrData&),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 2, (long, const char *),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 2, (long, const wchar_t *),
+ (f1, wxFormatString(f2)))
+
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 2, (void *, const wxString&),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 2, (void *, const wxCStrData&),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 2, (void *, const char *),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, Log,
+ 2, (void *, const wchar_t *),
+ (f1, wxFormatString(f2)))
+
+ WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel,
+ 2, (wxLogLevel, const wxString&),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel,
+ 2, (wxLogLevel, const wxCStrData&),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel,
+ 2, (wxLogLevel, const char *),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel,
+ 2, (wxLogLevel, const wchar_t *),
+ (f1, wxFormatString(f2)))
+
+ WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
+ 2, (const wxString&, const wxString&),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
+ 2, (const wxString&, const wxCStrData&),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
+ 2, (const wxString&, const char *),
+ (f1, wxFormatString(f2)))
+ WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
+ 2, (const wxString&, const wchar_t *),
+ (f1, wxFormatString(f2)))
+#endif // __WATCOMC__
+
+private:
+#if !wxUSE_UTF8_LOCALE_ONLY
+ void DoLog(const wxChar *format, ...)
+ {
+ va_list argptr;
+ va_start(argptr, format);
+ DoCallOnLog(format, argptr);
+ va_end(argptr);
+ }
+
+ void DoLogWithNum(long num, const wxChar *format, ...)
+ {
+ Store(m_optKey, num);
+
+ va_list argptr;
+ va_start(argptr, format);
+ DoCallOnLog(format, argptr);
+ va_end(argptr);
+ }
+
+ void DoLogWithPtr(void *ptr, const wxChar *format, ...)
+ {
+ Store(m_optKey, wxPtrToUInt(ptr));
+
+ va_list argptr;
+ va_start(argptr, format);
+ DoCallOnLog(format, argptr);
+ va_end(argptr);
+ }
+
+ void DoLogAtLevel(wxLogLevel level, const wxChar *format, ...)
+ {
+ if ( !wxLog::IsLevelEnabled(level) )
+ return;
+
+ va_list argptr;
+ va_start(argptr, format);
+ DoCallOnLog(level, format, argptr);
+ va_end(argptr);
+ }
+
+ void DoLogTrace(const wxString& mask, const wxChar *format, ...)
+ {
+ if ( !wxLog::IsAllowedTraceMask(mask) )
+ return;
+
+ Store(wxLOG_KEY_TRACE_MASK, mask);
+
+ va_list argptr;
+ va_start(argptr, format);
+ DoCallOnLog(format, argptr);
+ va_end(argptr);
+ }
+#endif // !wxUSE_UTF8_LOCALE_ONLY
+
+#if wxUSE_UNICODE_UTF8
+ void DoLogUtf8(const char *format, ...)
+ {
+ va_list argptr;
+ va_start(argptr, format);
+ DoCallOnLog(format, argptr);
+ va_end(argptr);
+ }
+
+ void DoLogWithNumUtf8(long num, const char *format, ...)
+ {
+ Store(m_optKey, num);
+
+ va_list argptr;
+ va_start(argptr, format);
+ DoCallOnLog(format, argptr);
+ va_end(argptr);
+ }
+
+ void DoLogWithPtrUtf8(void *ptr, const char *format, ...)
+ {
+ Store(m_optKey, wxPtrToUInt(ptr));
+
+ va_list argptr;
+ va_start(argptr, format);
+ DoCallOnLog(format, argptr);
+ va_end(argptr);
+ }
+
+ void DoLogAtLevelUtf8(wxLogLevel level, const char *format, ...)
+ {
+ if ( !wxLog::IsLevelEnabled(level) )
+ return;
+
+ va_list argptr;
+ va_start(argptr, format);
+ DoCallOnLog(level, format, argptr);
+ va_end(argptr);
+ }
+
+ void DoLogTraceUtf8(const wxString& mask, const char *format, ...)
+ {
+ if ( !wxLog::IsAllowedTraceMask(mask) )
+ return;
+
+ Store(wxLOG_KEY_TRACE_MASK, mask);
+
+ va_list argptr;
+ va_start(argptr, format);
+ DoCallOnLog(format, argptr);
+ va_end(argptr);
+ }
+#endif // wxUSE_UNICODE_UTF8
+
+ void DoCallOnLog(wxLogLevel level, const wxString& format, va_list argptr)
+ {
+ wxLog::OnLog(level, wxString::FormatV(format, argptr), m_info);
+ }
+
+ void DoCallOnLog(const wxString& format, va_list argptr)
+ {
+ wxLog::OnLog(m_level, wxString::FormatV(format, argptr), m_info);
+ }
+
+
+ const wxLogLevel m_level;
+ wxLogRecordInfo m_info;
+
+ wxString m_optKey;
+
+ wxDECLARE_NO_COPY_CLASS(wxLogger);
+};
+