mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-29 04:35:41 +08:00
refs #336 Fixed CLogHandler thread safety in swiftcore.
This commit is contained in:
@@ -40,10 +40,7 @@ namespace BlackMisc
|
|||||||
|
|
||||||
CLogHandler::~CLogHandler()
|
CLogHandler::~CLogHandler()
|
||||||
{
|
{
|
||||||
if (m_oldHandler)
|
qInstallMessageHandler(m_oldHandler);
|
||||||
{
|
|
||||||
qInstallMessageHandler(m_oldHandler);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CLogCategoryHandler *CLogHandler::handlerForCategoryPrefix(const QString &category)
|
CLogCategoryHandler *CLogHandler::handlerForCategoryPrefix(const QString &category)
|
||||||
|
|||||||
@@ -45,10 +45,6 @@ namespace BlackMisc
|
|||||||
//! \warning This must only be called from the main thread.
|
//! \warning This must only be called from the main thread.
|
||||||
CLogCategoryHandler *handlerForCategoryPrefix(const QString &prefix);
|
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:
|
signals:
|
||||||
//! Emitted when a message is logged in this process.
|
//! Emitted when a message is logged in this process.
|
||||||
void localMessageLogged(const BlackMisc::CStatusMessage &message);
|
void localMessageLogged(const BlackMisc::CStatusMessage &message);
|
||||||
@@ -63,6 +59,9 @@ namespace BlackMisc
|
|||||||
//! Called by the context to relay a message.
|
//! Called by the context to relay a message.
|
||||||
void logRemoteMessage(const BlackMisc::CStatusMessage &message);
|
void logRemoteMessage(const BlackMisc::CStatusMessage &message);
|
||||||
|
|
||||||
|
//! Enable or disable the default Qt handler.
|
||||||
|
void enableConsoleOutput(bool enable);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void logMessage(const BlackMisc::CStatusMessage &message);
|
void logMessage(const BlackMisc::CStatusMessage &message);
|
||||||
QtMessageHandler m_oldHandler = nullptr;
|
QtMessageHandler m_oldHandler = nullptr;
|
||||||
@@ -80,11 +79,10 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public slots:
|
||||||
/*!
|
/*!
|
||||||
* Enable or disable the default Qt handler for messages in relevant categories.
|
* Enable or disable the default Qt handler for messages in relevant categories.
|
||||||
* This can override the setting of the parent CLogHandler.
|
* 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; }
|
void enableConsoleOutput(bool enable) { Q_ASSERT(thread() == QThread::currentThread()); m_enableFallThrough = enable; }
|
||||||
|
|
||||||
|
|||||||
@@ -14,12 +14,33 @@
|
|||||||
|
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
|
#include <QTimer>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
||||||
namespace BlackMisc
|
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.
|
* Just a subclass of QThread whose destructor waits for the thread to finish.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "blackmisc/avallclasses.h"
|
#include "blackmisc/avallclasses.h"
|
||||||
#include "blackmisc/pqallquantities.h"
|
#include "blackmisc/pqallquantities.h"
|
||||||
#include "blackmisc/loghandler.h"
|
#include "blackmisc/loghandler.h"
|
||||||
|
#include "blackmisc/worker.h"
|
||||||
#include "blacksound/soundgenerator.h"
|
#include "blacksound/soundgenerator.h"
|
||||||
|
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
@@ -37,7 +38,7 @@ namespace BlackMiscTest
|
|||||||
*/
|
*/
|
||||||
void Tool::serverLoop(BlackCore::CRuntime *runtime)
|
void Tool::serverLoop(BlackCore::CRuntime *runtime)
|
||||||
{
|
{
|
||||||
CLogHandler::instance()->enableConsoleOutput(false);
|
QMetaObject::invokeMethod(CLogHandler::instance(), "enableConsoleOutput", Q_ARG(bool, false));
|
||||||
|
|
||||||
Q_ASSERT(runtime);
|
Q_ASSERT(runtime);
|
||||||
QThread::sleep(3); // let the DBus server startup
|
QThread::sleep(3); // let the DBus server startup
|
||||||
@@ -164,7 +165,7 @@ namespace BlackMiscTest
|
|||||||
bool enable = line.endsWith("e");
|
bool enable = line.endsWith("e");
|
||||||
if (line.startsWith("all"))
|
if (line.startsWith("all"))
|
||||||
{
|
{
|
||||||
BlackMisc::CLogHandler::instance()->enableConsoleOutput(enable);
|
QMetaObject::invokeMethod(CLogHandler::instance(), "enableConsoleOutput", Q_ARG(bool, enable));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -177,7 +178,10 @@ namespace BlackMiscTest
|
|||||||
else if (line.startsWith("sim")) category = CLogCategoryList(simulatorContext).back().toQString();
|
else if (line.startsWith("sim")) category = CLogCategoryList(simulatorContext).back().toQString();
|
||||||
if (! category.isEmpty())
|
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