From: Vadim Zeitlin Date: Mon, 13 Jul 2009 13:21:52 +0000 (+0000) Subject: Make wxLog::EnableLogging() and wxLogNull thread-specific. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/53ff8df7d54cd427674fc9bffb748c8872c0d658 Make wxLog::EnableLogging() and wxLogNull thread-specific. Disabling logging in a single thread (even the main one) shouldn't disable logs from the background threads which should disable their logging themselves as/if needed. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61423 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/doxygen/overviews/log.h b/docs/doxygen/overviews/log.h index f0d979b73e..3dee437d54 100644 --- a/docs/doxygen/overviews/log.h +++ b/docs/doxygen/overviews/log.h @@ -273,5 +273,9 @@ with later timestamp may appear before messages with earlier timestamp logged from other threads. wxLog does however guarantee that messages logged by each thread will appear in order in which they were logged. +Also notice that wxLog::EnableLogging() and wxLogNull class which uses it only +affect the current thread, i.e. logging messages may still be generated by the +other threads after a call to @c EnableLogging(false). + */ diff --git a/include/wx/log.h b/include/wx/log.h index 522c789f1c..775fac228f 100644 --- a/include/wx/log.h +++ b/include/wx/log.h @@ -328,15 +328,32 @@ public: // ---------------------- // these functions allow to completely disable all log messages or disable - // log messages at level less important than specified + // log messages at level less important than specified for the current + // thread // is logging enabled at all now? - static bool IsEnabled() { return ms_doLog; } + static bool IsEnabled() + { +#if wxUSE_THREADS + if ( !wxThread::IsMain() ) + return IsThreadLoggingEnabled(); +#endif // wxUSE_THREADS + + return ms_doLog; + } // change the flag state, return the previous one - static bool EnableLogging(bool doIt = true) - { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; } + static bool EnableLogging(bool enable = true) + { +#if wxUSE_THREADS + if ( !wxThread::IsMain() ) + return EnableThreadLogging(enable); +#endif // wxUSE_THREADS + bool doLogOld = ms_doLog; + ms_doLog = enable; + return doLogOld; + } // return the current global log level static wxLogLevel GetLogLevel() { return ms_logLevel; } @@ -582,6 +599,11 @@ private: // called from FlushActive() to really log any buffered messages logged // from the other threads void FlushThreadMessages(); + + // these functions are called for non-main thread only by IsEnabled() and + // EnableLogging() respectively + static bool IsThreadLoggingEnabled(); + static bool EnableThreadLogging(bool enable = true); #endif // wxUSE_THREADS // called from OnLog() if it's called from the main thread or if we have a diff --git a/include/wx/private/threadinfo.h b/include/wx/private/threadinfo.h index 87eee51cc6..22cbb6ab60 100644 --- a/include/wx/private/threadinfo.h +++ b/include/wx/private/threadinfo.h @@ -30,7 +30,18 @@ class WXDLLIMPEXP_FWD_BASE wxLog; // NB: this must be a POD to be stored in TLS struct wxThreadSpecificInfo { + // the thread-specific logger or NULL if the thread is using the global one + // (this is not used for the main thread which always uses the global + // logger) wxLog *logger; + + // true if logging is currently disabled for this thread (this is also not + // used for the main thread which uses wxLog::ms_doLog) + // + // NB: we use a counter-intuitive "disabled" flag instead of "enabled" one + // because the default, for 0-initialized struct, should be to enable + // logging + bool loggingDisabled; }; // currently this is defined in src/common/log.cpp diff --git a/interface/wx/log.h b/interface/wx/log.h index d23af95b51..64a5c08de1 100644 --- a/interface/wx/log.h +++ b/interface/wx/log.h @@ -744,7 +744,8 @@ public: /** Globally enable or disable logging. - Calling this function with @false argument disables all log messages. + Calling this function with @false argument disables all log messages + for the current thread. @see wxLogNull, IsEnabled() @@ -845,7 +846,7 @@ public: static bool IsEnabled(); /** - Returns true if logging at this level is enabled. + Returns true if logging at this level is enabled for the current thread. This function only returns @true if logging is globally enabled and if @a level is less than or equal to the maximal log level enabled for the diff --git a/samples/thread/thread.cpp b/samples/thread/thread.cpp index 06232403a4..d8d10716c0 100644 --- a/samples/thread/thread.cpp +++ b/samples/thread/thread.cpp @@ -793,6 +793,11 @@ void MyFrame::OnWorkerEvent(wxThreadEvent& event) void MyFrame::OnStartGUIThread(wxCommandEvent& WXUNUSED(event)) { + // we use this to check that disabling logging only affects the main thread + // but the messages from the worker thread will still be logged + wxLogNull noLog; + wxLogMessage("You shouldn't see this message because of wxLogNull"); + MyImageDialog dlg(this); dlg.ShowModal(); @@ -1003,6 +1008,11 @@ wxThread::ExitCode MyWorkerThread::Entry() wxThread::ExitCode MyGUIThread::Entry() { + // uncomment this to check that disabling logging here does disable it for + // this thread -- but not the main one if you also comment out wxLogNull + // line in MyFrame::OnStartGUIThread() + //wxLogNull noLog; + // this goes to the main window wxLogMessage("GUI thread starting"); diff --git a/src/common/log.cpp b/src/common/log.cpp index 293fcd1dc8..da6f70cb3a 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -657,6 +657,20 @@ void wxLog::FlushThreadMessages() } } +/* static */ +bool wxLog::IsThreadLoggingEnabled() +{ + return !wxThreadInfo.loggingDisabled; +} + +/* static */ +bool wxLog::EnableThreadLogging(bool enable) +{ + const bool wasEnabled = !wxThreadInfo.loggingDisabled; + wxThreadInfo.loggingDisabled = !enable; + return wasEnabled; +} + #endif // wxUSE_THREADS void wxLog::Flush()