-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgstreamerlogmodel.cpp
208 lines (190 loc) · 6.1 KB
/
gstreamerlogmodel.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "gstreamerlogmodel.h"
#include "timestamp.h"
#include <QtCore/QFile>
#include <QtCore/QMetaProperty>
#include <QtCore/QRegularExpression>
#include <QtCore/QTextStream>
#include <QtGui/QColor>
class GStreamerLogModel::Private
{
public:
QString fileName;
QList<GStreamerLogLine> lines;
static const QMetaObject *mo;
QMap<int, QColor> processColorMap;
QHash<QString, QColor> threadColorMap;
};
const QMetaObject *GStreamerLogModel::Private::mo = &GStreamerLogLine::staticMetaObject;
GStreamerLogModel::GStreamerLogModel(const QString &fileName, QObject *parent)
: QAbstractTableModel(parent)
, d(new Private{fileName})
{
reload();
}
GStreamerLogModel::~GStreamerLogModel() = default;
int GStreamerLogModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return d->lines.count();
}
int GStreamerLogModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return d->mo->propertyCount();
}
QVariant GStreamerLogModel::headerData(int section, Qt::Orientation orientation, int role) const
{
QVariant ret;
if (orientation != Qt::Horizontal)
return ret;
const auto mp = d->mo->property(section);
switch (role) {
case Qt::DisplayRole:
ret = mp.name();
break;
case Qt::TextAlignmentRole:
ret = Qt::AlignLeft;
break;
default:
break;
}
return ret;
}
QVariant GStreamerLogModel::data(const QModelIndex &index, int role) const
{
static const auto foregroundColors = QHash<QString, QColor> {
// { "WARN", QColor(Qt::white) },
{ "ERROR", QColor(Qt::yellow) },
};
static const auto backgroundColors = QHash<QString, QColor> {
{ "WARN", QColor(Qt::yellow) },
{ "ERROR", QColor(Qt::darkRed) },
};
QVariant ret;
if (!index.isValid())
return ret;
const auto column = index.column();
const auto mp = d->mo->property(column);
const auto row = index.row();
const auto line = d->lines.at(row);
switch (role) {
case Qt::DisplayRole:
ret = mp.readOnGadget(&line);
switch (mp.typeId()) {
case QMetaType::QString:
case QMetaType::Int:
break;
default:
if (mp.typeId() == Timestamp::metaTypeId) {
ret = ret.value<Timestamp>().toString();
} else {
qWarning() << mp.typeId() << "not supported";
}
break;
}
break;
case Qt::TextAlignmentRole:
if (mp.typeId() == QMetaType::Int || mp.typeId() == QMetaType::Double)
ret = Qt::AlignRight;
else
ret = Qt::AlignLeft;
break;
case Qt::ForegroundRole:
if (foregroundColors.contains(line.level))
ret = foregroundColors.value(line.level);
break;
case Qt::BackgroundRole:
switch (column) {
case PidColumn:
if (d->processColorMap.contains(line.pid))
ret = d->processColorMap.value(line.pid);
break;
case TidColumn:
if (d->threadColorMap.contains(line.tid))
ret = d->threadColorMap.value(line.tid);
break;
default:
if (backgroundColors.contains(line.level))
ret = backgroundColors.value(line.level);
break;
}
break;
case Qt::UserRole:
ret = line.id;
break;
default:
// ret = QAbstractTableModel::data(index, role);
break;
}
return ret;
}
void GStreamerLogModel::reload()
{
if (!d->lines.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, d->lines.count() - 1);
d->lines.clear();
endRemoveRows();
}
QFile file(d->fileName);
if (!file.open(QIODevice::ReadOnly))
return;
QTextStream in(&file);
int l = 0;
static const QRegularExpression re("^([\\d\\.:]+)\\s+(\\d+)\\s+(0x[0-9a-f]+)\\s+([A-Z]+)\\s+([^\\s]*)\\s+([a-z0-9_\\-\\.]*):(\\d+):([^:]*):(\\s*[^\\s]*)\\s+(.+)$");
while (!in.atEnd()) {
l++;
QString line = in.readLine();
QRegularExpressionMatch match = re.match(line);
if (match.hasMatch()) {
GStreamerLogLine logLine;
logLine.id = l;
for (int i = 0; i < d->mo->propertyCount(); i++) {
const auto mp = d->mo->property(i);
const auto text = match.captured(i + 1);
QVariant value = text;
switch (mp.typeId()) {
case QMetaType::QString:
break;
case QMetaType::Int:
value = text.toInt();
break;
default:
if (mp.typeId() == Timestamp::metaTypeId) {
value = QVariant::fromValue(Timestamp::fromString(text));
} else {
qWarning() << mp.typeId() << "not supported";
}
break;
}
mp.writeOnGadget(&logLine, value);
}
d->processColorMap[logLine.pid] = QColor();
d->threadColorMap[logLine.tid] = QColor();
if (d->lines.isEmpty()) {
d->lines.append(logLine);
} else {
auto timestamp = logLine.timestamp;
for (auto i = d->lines.count() - 1; i >= 0; i--) {
if (timestamp > d->lines.at(i).timestamp) {
d->lines.insert(i + 1, logLine);
break;
}
}
}
} else {
qWarning() << line;
}
}
const auto pids = d->processColorMap.keys();
for (int i = 0; i < pids.count(); i++) {
d->processColorMap[pids[i]] = QColor::fromHsvF((qreal)i / pids.count() * 0.4, 1, 1, 0.25);
}
const auto tids = d->threadColorMap.keys();
for (int i = 0; i < tids.count(); i++) {
d->threadColorMap[tids[i]] = QColor::fromHsvF((qreal)i / tids.count() * 0.4 + 0.5, 1, 1, 0.25);
}
beginInsertRows(QModelIndex(), 0, d->lines.count() - 1);
endInsertRows();
}