2022-05-10 20:10:34 +00:00
|
|
|
#include "MonitorQtOutput.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
namespace Status::Testing
|
|
|
|
{
|
2022-05-31 21:26:41 +00:00
|
|
|
|
2022-05-10 20:10:34 +00:00
|
|
|
std::weak_ptr<QString> MonitorQtOutput::m_qtMessageOutputForSharing;
|
|
|
|
std::mutex MonitorQtOutput::m_mutex;
|
|
|
|
QtMessageHandler MonitorQtOutput::m_previousHandler = nullptr;
|
|
|
|
|
|
|
|
MonitorQtOutput::MonitorQtOutput()
|
|
|
|
{
|
|
|
|
// Ensure only one instance registers a handler
|
|
|
|
// Warning: don't call QT's logger functions inside the critical section
|
|
|
|
std::unique_lock<std::mutex> localLock(m_mutex);
|
|
|
|
auto globalMsgOut = m_qtMessageOutputForSharing.lock();
|
|
|
|
auto prev = qInstallMessageHandler(qtMessageOutput);
|
2022-10-19 13:41:53 +00:00
|
|
|
if(prev != qtMessageOutput) m_previousHandler = prev;
|
|
|
|
if(!globalMsgOut)
|
|
|
|
{
|
2022-05-10 20:10:34 +00:00
|
|
|
m_thisMessageOutput = std::make_shared<QString>();
|
|
|
|
m_qtMessageOutputForSharing = m_thisMessageOutput;
|
|
|
|
}
|
2022-10-19 13:41:53 +00:00
|
|
|
else
|
|
|
|
{
|
2022-05-10 20:10:34 +00:00
|
|
|
m_thisMessageOutput = globalMsgOut;
|
|
|
|
m_start = m_thisMessageOutput->length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MonitorQtOutput::~MonitorQtOutput()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> localLock(m_mutex);
|
2022-10-19 13:41:53 +00:00
|
|
|
if(m_thisMessageOutput.use_count() == 1)
|
|
|
|
{
|
2022-05-10 20:10:34 +00:00
|
|
|
// Last instance, deregister the handler
|
|
|
|
qInstallMessageHandler(0);
|
|
|
|
m_thisMessageOutput.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
void MonitorQtOutput::qtMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
|
2022-05-10 20:10:34 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> localLock(m_mutex);
|
|
|
|
auto globalMsgOut = m_qtMessageOutputForSharing.lock();
|
|
|
|
assert(globalMsgOut != nullptr);
|
|
|
|
globalMsgOut->append(msg + '\n');
|
|
|
|
// Also reproduce the default output
|
|
|
|
m_previousHandler(type, context, msg);
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
QString MonitorQtOutput::qtOuput()
|
2022-05-10 20:10:34 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> localLock(m_mutex);
|
|
|
|
assert(m_thisMessageOutput->length() >= m_start);
|
|
|
|
return m_thisMessageOutput->right(m_thisMessageOutput->length() - m_start);
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
void MonitorQtOutput::restartCapturing()
|
2022-05-10 20:10:34 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> localLock(m_mutex);
|
|
|
|
// Ensure the messageHandler is installed. Foun to be reset at test initializaiton
|
|
|
|
auto prev = qInstallMessageHandler(qtMessageOutput);
|
2022-10-19 13:41:53 +00:00
|
|
|
if(prev != qtMessageOutput) m_previousHandler = prev;
|
2022-05-10 20:10:34 +00:00
|
|
|
m_start = m_thisMessageOutput->length();
|
|
|
|
}
|
2022-05-31 21:26:41 +00:00
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
} // namespace Status::Testing
|