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
This commit is contained in:
Vadim Zeitlin 2009-07-13 13:21:52 +00:00
parent acad886cb4
commit 53ff8df7d5
6 changed files with 68 additions and 6 deletions

View File

@ -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).
*/

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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");

View File

@ -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()