2016-08-18 20:31:37 +01:00
|
|
|
#include "LogModel.h"
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
LogModel::LogModel(QObject* parent) : QAbstractListModel(parent)
|
2016-08-18 20:31:37 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_content.resize(m_maxLines);
|
2016-08-18 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
int LogModel::rowCount(const QModelIndex& parent) const
|
2016-08-18 20:31:37 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
if (parent.isValid())
|
|
|
|
return 0;
|
2016-08-18 20:31:37 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
return m_numLines;
|
2016-08-18 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
QVariant LogModel::data(const QModelIndex& index, int role) const
|
2016-08-18 20:31:37 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
if (index.row() < 0 || index.row() >= m_numLines)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
auto row = index.row();
|
|
|
|
auto realRow = (row + m_firstLine) % m_maxLines;
|
2023-08-14 17:16:53 +01:00
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return m_content[realRow].line;
|
|
|
|
}
|
2023-08-14 17:16:53 +01:00
|
|
|
if (role == LevelRole) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return m_content[realRow].level;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
2016-08-18 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogModel::append(MessageLevel::Enum level, QString line)
|
|
|
|
{
|
2023-08-14 17:16:53 +01:00
|
|
|
if (m_suspended) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int lineNum = (m_firstLine + m_numLines) % m_maxLines;
|
|
|
|
// overflow
|
2023-08-14 17:16:53 +01:00
|
|
|
if (m_numLines == m_maxLines) {
|
|
|
|
if (m_stopOnOverflow) {
|
2018-07-15 13:51:05 +01:00
|
|
|
// nothing more to do, the buffer is full
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
beginRemoveRows(QModelIndex(), 0, 0);
|
|
|
|
m_firstLine = (m_firstLine + 1) % m_maxLines;
|
2023-08-14 17:16:53 +01:00
|
|
|
m_numLines--;
|
2018-07-15 13:51:05 +01:00
|
|
|
endRemoveRows();
|
2023-08-14 17:16:53 +01:00
|
|
|
} else if (m_numLines == m_maxLines - 1 && m_stopOnOverflow) {
|
2018-07-15 13:51:05 +01:00
|
|
|
level = MessageLevel::Fatal;
|
|
|
|
line = m_overflowMessage;
|
|
|
|
}
|
|
|
|
beginInsertRows(QModelIndex(), m_numLines, m_numLines);
|
2023-08-14 17:16:53 +01:00
|
|
|
m_numLines++;
|
2018-07-15 13:51:05 +01:00
|
|
|
m_content[lineNum].level = level;
|
|
|
|
m_content[lineNum].line = line;
|
|
|
|
endInsertRows();
|
2016-08-18 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 20:34:02 +01:00
|
|
|
void LogModel::suspend(bool suspend)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_suspended = suspend;
|
2016-10-11 20:34:02 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 00:19:43 +00:00
|
|
|
bool LogModel::suspended()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return m_suspended;
|
2017-12-18 00:19:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 20:31:37 +01:00
|
|
|
void LogModel::clear()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
beginResetModel();
|
|
|
|
m_firstLine = 0;
|
|
|
|
m_numLines = 0;
|
|
|
|
endResetModel();
|
2016-08-18 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString LogModel::toPlainText()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QString out;
|
|
|
|
out.reserve(m_numLines * 80);
|
2023-08-14 17:16:53 +01:00
|
|
|
for (int i = 0; i < m_numLines; i++) {
|
|
|
|
QString& line = m_content[(m_firstLine + i) % m_maxLines].line;
|
2018-07-15 13:51:05 +01:00
|
|
|
out.append(line + '\n');
|
|
|
|
}
|
|
|
|
out.squeeze();
|
|
|
|
return out;
|
2016-08-18 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogModel::setMaxLines(int maxLines)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
// no-op
|
2023-08-14 17:16:53 +01:00
|
|
|
if (maxLines == m_maxLines) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if it all still fits in the buffer, just resize it
|
2023-08-14 17:16:53 +01:00
|
|
|
if (m_firstLine + m_numLines < m_maxLines) {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_maxLines = maxLines;
|
|
|
|
m_content.resize(maxLines);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// otherwise, we need to reorganize the data because it crosses the wrap boundary
|
|
|
|
QVector<entry> newContent;
|
|
|
|
newContent.resize(maxLines);
|
2023-08-14 17:16:53 +01:00
|
|
|
if (m_numLines <= maxLines) {
|
2018-07-15 13:51:05 +01:00
|
|
|
// if it all fits in the new buffer, just copy it over
|
2023-08-14 17:16:53 +01:00
|
|
|
for (int i = 0; i < m_numLines; i++) {
|
2018-07-15 13:51:05 +01:00
|
|
|
newContent[i] = m_content[(m_firstLine + i) % m_maxLines];
|
|
|
|
}
|
|
|
|
m_content.swap(newContent);
|
2023-08-14 17:16:53 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
// if it doesn't fit, part of the data needs to be thrown away (the oldest log messages)
|
|
|
|
int lead = m_numLines - maxLines;
|
|
|
|
beginRemoveRows(QModelIndex(), 0, lead - 1);
|
2023-08-14 17:16:53 +01:00
|
|
|
for (int i = 0; i < maxLines; i++) {
|
2018-07-15 13:51:05 +01:00
|
|
|
newContent[i] = m_content[(m_firstLine + lead + i) % m_maxLines];
|
|
|
|
}
|
|
|
|
m_numLines = m_maxLines;
|
|
|
|
m_content.swap(newContent);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
m_firstLine = 0;
|
|
|
|
m_maxLines = maxLines;
|
2016-08-18 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 19:01:42 +00:00
|
|
|
int LogModel::getMaxLines()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return m_maxLines;
|
2017-02-08 19:01:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 20:31:37 +01:00
|
|
|
void LogModel::setStopOnOverflow(bool stop)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_stopOnOverflow = stop;
|
2016-08-18 20:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogModel::setOverflowMessage(const QString& overflowMessage)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_overflowMessage = overflowMessage;
|
2016-08-18 20:31:37 +01:00
|
|
|
}
|
2017-12-18 00:19:43 +00:00
|
|
|
|
|
|
|
void LogModel::setLineWrap(bool state)
|
|
|
|
{
|
2023-08-14 17:16:53 +01:00
|
|
|
if (m_lineWrap != state) {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_lineWrap = state;
|
|
|
|
}
|
2017-12-18 00:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LogModel::wrapLines() const
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
return m_lineWrap;
|
2017-12-18 00:19:43 +00:00
|
|
|
}
|