Add support for thread-specific log targets.

A worker thread can now have its own log target which will be used directly
by the log functions instead of buffering log output in the main thread; the
GUI thread in the thread sample shows how it works.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61422 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2009-07-13 11:09:26 +00:00
parent 409aa9e1ef
commit acad886cb4
6 changed files with 228 additions and 45 deletions

View File

@@ -385,24 +385,22 @@ public:
// 17 modal dialogs one after another)
virtual void Flush();
// flush the active target if any
static void FlushActive()
{
if ( !ms_suspendCount )
{
wxLog *log = GetActiveTarget();
if ( log )
log->Flush();
}
}
// flush the active target if any and also output any pending messages from
// background threads
static void FlushActive();
// only one sink is active at each moment
// get current log target, will call wxApp::CreateLogTarget() to
// create one if none exists
// only one sink is active at each moment get current log target, will call
// wxAppTraits::CreateLogTarget() to create one if none exists
static wxLog *GetActiveTarget();
// change log target, pLogger may be NULL
static wxLog *SetActiveTarget(wxLog *pLogger);
// change log target, logger may be NULL
static wxLog *SetActiveTarget(wxLog *logger);
#if wxUSE_THREADS
// change log target for the current thread only, shouldn't be called from
// the main thread as it doesn't use thread-specific log target
static wxLog *SetThreadActiveTarget(wxLog *logger);
#endif // wxUSE_THREADS
// suspend the message flushing of the main target until the next call
// to Resume() - this is mainly for internal use (to prevent wxYield()
@@ -580,11 +578,18 @@ protected:
unsigned LogLastRepeatIfNeeded();
private:
// called from OnLog() if it's called from the main thread and from Flush()
#if wxUSE_THREADS
// called from FlushActive() to really log any buffered messages logged
// from the other threads
void FlushThreadMessages();
#endif // wxUSE_THREADS
// called from OnLog() if it's called from the main thread or if we have a
// (presumably MT-safe) thread-specific logger and by FlushThreadMessages()
// when it plays back the buffered messages logged from the other threads
void OnLogInMainThread(wxLogLevel level,
const wxString& msg,
const wxLogRecordInfo& info);
void CallDoLogNow(wxLogLevel level,
const wxString& msg,
const wxLogRecordInfo& info);
// static variables

View File

@@ -0,0 +1,43 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/private/threadinfo.h
// Purpose: declaration of wxThreadSpecificInfo: thread-specific information
// Author: Vadim Zeitlin
// Created: 2009-07-13
// RCS-ID: $Id: wxhead.h,v 1.11 2009-06-29 10:23:04 zeitlin Exp $
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_PRIVATE_THREADINFO_H_
#define _WX_PRIVATE_THREADINFO_H_
#if wxUSE_THREADS
#include "wx/tls.h"
class WXDLLIMPEXP_FWD_BASE wxLog;
// ----------------------------------------------------------------------------
// wxThreadSpecificInfo: contains all thread-specific information used by wx
// ----------------------------------------------------------------------------
// currently the only thread-specific information we use is the active wxLog
// target but more could be added in the future (e.g. current wxLocale would be
// a likely candidate) and we will group all of them in this struct to avoid
// consuming more TLS slots than necessary as there is only a limited number of
// them
// NB: this must be a POD to be stored in TLS
struct wxThreadSpecificInfo
{
wxLog *logger;
};
// currently this is defined in src/common/log.cpp
extern wxTLS_TYPE(wxThreadSpecificInfo) wxThreadInfoVar;
#define wxThreadInfo wxTLS_VALUE(wxThreadInfoVar)
#endif // wxUSE_THREADS
#endif // _WX_PRIVATE_THREADINFO_H_