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"
|
|
|
|
|
2017-06-06 02:56:52 +00:00
|
|
|
/* based on logging bits from libdivecomputer */
|
|
|
|
#ifndef __ANDROID__
|
|
|
|
#define INFO(fmt, ...) fprintf(stderr, "INFO: " fmt "\n", ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#include <android/log.h>
|
|
|
|
#define INFO(fmt, ...) __android_log_print(ANDROID_LOG_DEBUG, __FILE__, "INFO: " fmt "\n", ##__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
2017-06-05 16:16:12 +00:00
|
|
|
void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
|
|
|
{
|
2017-06-11 22:46:26 +00:00
|
|
|
Q_UNUSED(context)
|
2017-06-05 16:16:12 +00:00
|
|
|
MessageHandlerModel::self()->addLog(type, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageHandlerModel * MessageHandlerModel::self()
|
|
|
|
{
|
|
|
|
static MessageHandlerModel *self = new MessageHandlerModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageHandlerModel::MessageHandlerModel(QObject *parent)
|
|
|
|
{
|
2017-06-11 22:46:26 +00:00
|
|
|
Q_UNUSED(parent)
|
2017-06-05 16:16:12 +00:00
|
|
|
// no more than one message handler.
|
|
|
|
qInstallMessageHandler(logMessageHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
int MessageHandlerModel::rowCount(const QModelIndex& parent) const
|
|
|
|
{
|
2017-06-11 22:46:26 +00:00
|
|
|
Q_UNUSED(parent)
|
2017-06-05 16:16:12 +00:00
|
|
|
return m_data.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
void MessageHandlerModel::addLog(QtMsgType type, const QString& message)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), rowCount(), rowCount());
|
|
|
|
m_data.append({message, type});
|
|
|
|
endInsertRows();
|
2017-06-06 02:56:52 +00:00
|
|
|
INFO("%s", message.toUtf8().constData());
|
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();
|
|
|
|
|
|
|
|
}
|