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
from other threads. wxLog does however guarantee that messages logged by each
thread will appear in order in which they were 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).
+
// ----------------------
// these functions allow to completely disable all log messages or disable
// ----------------------
// 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?
// 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
// 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; }
// return the current global log level
static wxLogLevel GetLogLevel() { return ms_logLevel; }
// called from FlushActive() to really log any buffered messages logged
// from the other threads
void FlushThreadMessages();
// 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
#endif // wxUSE_THREADS
// called from OnLog() if it's called from the main thread or if we have a
// NB: this must be a POD to be stored in TLS
struct wxThreadSpecificInfo
{
// 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)
+
+ // 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
};
// currently this is defined in src/common/log.cpp
/**
Globally enable or disable logging.
/**
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()
@see wxLogNull, IsEnabled()
static bool IsEnabled();
/**
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
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
void MyFrame::OnStartGUIThread(wxCommandEvent& WXUNUSED(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();
MyImageDialog dlg(this);
dlg.ShowModal();
wxThread::ExitCode MyGUIThread::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");
// this goes to the main window
wxLogMessage("GUI thread starting");
+/* 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()
#endif // wxUSE_THREADS
void wxLog::Flush()