2022-05-10 20:10:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QQuickItem>
|
|
|
|
#include <QtGlobal>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
|
2022-05-31 21:26:41 +00:00
|
|
|
namespace Status::Testing {
|
|
|
|
|
2022-05-10 20:10:34 +00:00
|
|
|
///
|
|
|
|
/// \brief Monitor output for tests and declaratively control message handler availability
|
|
|
|
///
|
|
|
|
/// The captured buffer is global and each instance has a reference to it and a start pointer
|
|
|
|
/// from its creation or last clear call
|
|
|
|
/// The first instance installs a QT message handler @see Qt::qInstallMessageHandler then
|
|
|
|
/// All other instances share the global buffer until the last instance goes out of scope and deregisters
|
|
|
|
/// from Qt's global message handler and destroyes the buffer
|
|
|
|
///
|
|
|
|
/// \todo Check that QML doesn't keep instance between test runs
|
|
|
|
///
|
|
|
|
class MonitorQtOutput : public QQuickItem
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
MonitorQtOutput();
|
|
|
|
~MonitorQtOutput();
|
|
|
|
|
|
|
|
/// Return captured output from the global buffer. That is from the instantiation or last `clear()` was called
|
|
|
|
Q_INVOKABLE QString qtOuput();
|
|
|
|
/// Reset buffer start after the last line. qtOutput won't return anything until new output is captured
|
|
|
|
Q_INVOKABLE void restartCapturing();
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void qtMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);
|
|
|
|
static QtMessageHandler m_previousHandler;
|
|
|
|
|
|
|
|
// Use it to keep track of qInstallMessageHandler call
|
|
|
|
static std::weak_ptr<QString> m_qtMessageOutputForSharing;
|
|
|
|
static std::mutex m_mutex;
|
|
|
|
std::shared_ptr<QString> m_thisMessageOutput;
|
|
|
|
int m_start = 0;
|
|
|
|
};
|
2022-05-31 21:26:41 +00:00
|
|
|
|
|
|
|
}
|