mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-02 15:15:50 +08:00
refs #336 Fixed CLogHandler thread safety in swiftcore.
This commit is contained in:
@@ -40,10 +40,7 @@ namespace BlackMisc
|
||||
|
||||
CLogHandler::~CLogHandler()
|
||||
{
|
||||
if (m_oldHandler)
|
||||
{
|
||||
qInstallMessageHandler(m_oldHandler);
|
||||
}
|
||||
qInstallMessageHandler(m_oldHandler);
|
||||
}
|
||||
|
||||
CLogCategoryHandler *CLogHandler::handlerForCategoryPrefix(const QString &category)
|
||||
|
||||
@@ -45,10 +45,6 @@ namespace BlackMisc
|
||||
//! \warning This must only be called from the main thread.
|
||||
CLogCategoryHandler *handlerForCategoryPrefix(const QString &prefix);
|
||||
|
||||
//! Enable or disable the default Qt handler.
|
||||
//! \warning This must only be called from the main thread.
|
||||
void enableConsoleOutput(bool enable);
|
||||
|
||||
signals:
|
||||
//! Emitted when a message is logged in this process.
|
||||
void localMessageLogged(const BlackMisc::CStatusMessage &message);
|
||||
@@ -63,6 +59,9 @@ namespace BlackMisc
|
||||
//! Called by the context to relay a message.
|
||||
void logRemoteMessage(const BlackMisc::CStatusMessage &message);
|
||||
|
||||
//! Enable or disable the default Qt handler.
|
||||
void enableConsoleOutput(bool enable);
|
||||
|
||||
private:
|
||||
void logMessage(const BlackMisc::CStatusMessage &message);
|
||||
QtMessageHandler m_oldHandler = nullptr;
|
||||
@@ -80,11 +79,10 @@ namespace BlackMisc
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
public slots:
|
||||
/*!
|
||||
* Enable or disable the default Qt handler for messages in relevant categories.
|
||||
* This can override the setting of the parent CLogHandler.
|
||||
* \warning This must only be called from the main thread.
|
||||
*/
|
||||
void enableConsoleOutput(bool enable) { Q_ASSERT(thread() == QThread::currentThread()); m_enableFallThrough = enable; }
|
||||
|
||||
|
||||
@@ -14,12 +14,33 @@
|
||||
|
||||
#include <QThread>
|
||||
#include <QMutex>
|
||||
#include <QTimer>
|
||||
#include <functional>
|
||||
#include <atomic>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
/*!
|
||||
* Starts a single-shot timer which will run in an existing thread and call a task when it times out.
|
||||
*
|
||||
* Useful when a worker thread wants to push small sub-tasks back to the thread which spawned it.
|
||||
* \see QTimer::singleShot()
|
||||
*/
|
||||
template <typename F>
|
||||
void singleShot(int msec, QThread *target, F task)
|
||||
{
|
||||
auto *timer = new QTimer;
|
||||
timer->setSingleShot(true);
|
||||
timer->moveToThread(target);
|
||||
QObject::connect(timer, &QTimer::timeout, [ = ]()
|
||||
{
|
||||
task();
|
||||
timer->deleteLater();
|
||||
});
|
||||
QMetaObject::invokeMethod(timer, "start", Q_ARG(int, msec));
|
||||
}
|
||||
|
||||
/*!
|
||||
* Just a subclass of QThread whose destructor waits for the thread to finish.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user