2017-06-06 02:56:52 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2017-06-05 16:16:12 +00:00
|
|
|
#include "messagehandlermodel.h"
|
2020-04-17 20:02:25 +00:00
|
|
|
#include "core/qthelper.h"
|
2017-06-06 02:56:52 +00:00
|
|
|
|
2018-04-13 23:53:51 +00:00
|
|
|
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
|
|
|
|
extern void writeToAppLogFile(QString logText);
|
|
|
|
#endif
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
void logMessageHandler(QtMsgType type, const QMessageLogContext&, const QString &msg)
|
2017-06-05 16:16:12 +00:00
|
|
|
{
|
|
|
|
MessageHandlerModel::self()->addLog(type, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageHandlerModel * MessageHandlerModel::self()
|
|
|
|
{
|
|
|
|
static MessageHandlerModel *self = new MessageHandlerModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
MessageHandlerModel::MessageHandlerModel(QObject*)
|
2017-06-05 16:16:12 +00:00
|
|
|
{
|
|
|
|
// no more than one message handler.
|
|
|
|
qInstallMessageHandler(logMessageHandler);
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int MessageHandlerModel::rowCount(const QModelIndex&) const
|
2017-06-05 16:16:12 +00:00
|
|
|
{
|
|
|
|
return m_data.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
void MessageHandlerModel::addLog(QtMsgType type, const QString& message)
|
|
|
|
{
|
2020-03-29 21:38:09 +00:00
|
|
|
if (!m_data.isEmpty()) {
|
|
|
|
struct MessageData *lm = &m_data.last();
|
|
|
|
QString lastMessage = lm->message.mid(lm->message.indexOf(':'));
|
|
|
|
QString newMessage = message.mid(message.indexOf(':'));
|
|
|
|
if (lastMessage == newMessage)
|
|
|
|
return;
|
|
|
|
}
|
2021-01-13 22:25:25 +00:00
|
|
|
// filter extremely noisy and unhelpful messages
|
|
|
|
if (message.contains("Updating RSSI for") || (message.contains(QRegExp(".*kirigami.*onFoo properties in Connections"))))
|
|
|
|
return;
|
|
|
|
|
2017-06-05 16:16:12 +00:00
|
|
|
beginInsertRows(QModelIndex(), rowCount(), rowCount());
|
|
|
|
m_data.append({message, type});
|
|
|
|
endInsertRows();
|
2020-04-17 20:02:25 +00:00
|
|
|
SSRF_INFO("%s", qPrintable(message));
|
2018-04-13 23:53:51 +00:00
|
|
|
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
|
2017-07-10 00:03:57 +00:00
|
|
|
writeToAppLogFile(message);
|
|
|
|
#endif
|
2017-06-05 16:16:12 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:50:17 +00:00
|
|
|
const QString MessageHandlerModel::logAsString()
|
|
|
|
{
|
|
|
|
QString copyString;
|
|
|
|
|
|
|
|
// Loop through m_data and build big string to be put on the clipboard
|
|
|
|
for(const MessageData &data: m_data)
|
|
|
|
copyString += data.message + "\n";
|
|
|
|
return copyString;
|
|
|
|
}
|
2017-06-05 16:16:12 +00:00
|
|
|
QVariant MessageHandlerModel::data(const QModelIndex& idx, int role) const
|
|
|
|
{
|
|
|
|
switch(role) {
|
|
|
|
case Message:
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return m_data.at(idx.row()).message;
|
|
|
|
}
|
|
|
|
return QVariant(QString("Role: %1").arg(role));
|
|
|
|
};
|
|
|
|
|
|
|
|
QHash<int, QByteArray> MessageHandlerModel::roleNames() const {
|
|
|
|
static QHash<int, QByteArray> roles = {
|
|
|
|
{Message, "message"},
|
|
|
|
{Severity, "severity"}
|
|
|
|
};
|
|
|
|
return roles;
|
|
|
|
};
|
|
|
|
|
|
|
|
void MessageHandlerModel::reset()
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_data.size()-1);
|
|
|
|
m_data.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
|
|
|
|
}
|