mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 04:25:35 +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.
|
||||
*/
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "blackmisc/avallclasses.h"
|
||||
#include "blackmisc/pqallquantities.h"
|
||||
#include "blackmisc/loghandler.h"
|
||||
#include "blackmisc/worker.h"
|
||||
#include "blacksound/soundgenerator.h"
|
||||
|
||||
#include <QTextStream>
|
||||
@@ -37,7 +38,7 @@ namespace BlackMiscTest
|
||||
*/
|
||||
void Tool::serverLoop(BlackCore::CRuntime *runtime)
|
||||
{
|
||||
CLogHandler::instance()->enableConsoleOutput(false);
|
||||
QMetaObject::invokeMethod(CLogHandler::instance(), "enableConsoleOutput", Q_ARG(bool, false));
|
||||
|
||||
Q_ASSERT(runtime);
|
||||
QThread::sleep(3); // let the DBus server startup
|
||||
@@ -164,7 +165,7 @@ namespace BlackMiscTest
|
||||
bool enable = line.endsWith("e");
|
||||
if (line.startsWith("all"))
|
||||
{
|
||||
BlackMisc::CLogHandler::instance()->enableConsoleOutput(enable);
|
||||
QMetaObject::invokeMethod(CLogHandler::instance(), "enableConsoleOutput", Q_ARG(bool, enable));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -177,7 +178,10 @@ namespace BlackMiscTest
|
||||
else if (line.startsWith("sim")) category = CLogCategoryList(simulatorContext).back().toQString();
|
||||
if (! category.isEmpty())
|
||||
{
|
||||
BlackMisc::CLogHandler::instance()->handlerForCategoryPrefix(category)->enableConsoleOutput(enable);
|
||||
BlackMisc::singleShot(0, BlackMisc::CLogHandler::instance()->thread(), [ = ]()
|
||||
{
|
||||
BlackMisc::CLogHandler::instance()->handlerForCategoryPrefix(category)->enableConsoleOutput(enable);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user