-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogmodel.cpp
More file actions
133 lines (110 loc) · 3.8 KB
/
Copy pathlogmodel.cpp
File metadata and controls
133 lines (110 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <QDateTime>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include "logmodel.h"
/**
* @brief Override
*/
QHash<int, QByteArray> LogModel::roleNames() const
{
QHash<int, QByteArray> mapping {
{Timestamp, "timestamp"},
{Filename, "file"},
{Msg, "msg"}
};
return mapping;
}
/**
* @brief Override
*/
int LogModel::rowCount(const QModelIndex &parent) const
{
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid())
return 0;
return m_logLines.size();
}
/**
* @brief Override
*
*/
QVariant LogModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
switch(auto row = index.row(); role)
{
case Timestamp:
// Return data as QString and not as ISODateWithMs to keep nanosecs precision
// return QDateTime::fromString(m_logLines[row].at(0), Qt::ISODateWithMs);
return m_logLines[row].at(0); break;
case Filename:
return m_logLines[row].at(1); break;
case Msg:
return m_logLines[row].at(2); break;
default:
qDebug() << "Error: no valid role";
}
return QVariant();
}
/**
* @brief The data insertion func for LogModel
* @param line follows the following template { timestamp, filename, logmessage }
*
*/
void LogModel::insertLogLine(const QStringList line)
{
const int newRow = m_logLines.size();
beginInsertRows(QModelIndex(),newRow,newRow);
m_logLines.push_back(line);
endInsertRows();
}
/**
* @brief Fill the model from the log files
*
*/
void LogModel::populate(const QStringList &filesToCombine)
{
for(const auto& path : filesToCombine)
{
// qDebug() << "Adding " << path;
QFile logFile(path);
QString baseLogName = QFileInfo(logFile).baseName();
if ( !logFile.open(QIODevice::ReadOnly) )
{
qDebug() << "Could not open logfile: " << baseLogName;
return;
}
QTextStream in(&logFile);
while ( !in.atEnd() )
{
const QString line = in.readLine();
// Filter out lines which should not be in the model at the first place. The purpose
// is different from LogSFPModel, where we may want subsets of valid log lines
QRegularExpressionMatch match = m_rxLogLine.match(line);
if (match.hasMatch())
{
const QString timestamp = match.captured(1);
const QString logMessage = match.captured(2);
const auto ISOTimestamp = QDateTime::fromString(timestamp, Qt::ISODateWithMs);
if ( timestamp.isEmpty() || !ISOTimestamp.isValid()) // discard non-timestamped non-ISO lines
{
qDebug() << "Invalid ISO timestamp: " << timestamp;
continue;
}
if ( !logMessage.isEmpty() )// discard blank lines
{
/*
// Code to filter out log messages which should never be inserted in the model at the first place (decoration,etc)
if ( std::any_of(filters.begin(), filters.end(),
[&logMessage](const auto& filter){ return QRegularExpression(filter).match(logMessage).hasMatch(); }))
continue;
*/
insertLogLine( { timestamp, " [" + baseLogName.leftJustified(10, ' ', true) + "] ", logMessage });
}
}
}
}
}