-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgelfmessagemodel.cpp
93 lines (83 loc) · 2.51 KB
/
gelfmessagemodel.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
#include "gelfmessagemodel.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <QDateTime>
#include <QDebug>
GELFMessageModel::GELFMessageModel(QObject *parent):
QAbstractTableModel(parent),
column_names(),
messages()
{
// Set common column names
column_names
<< "version"
<< "host"
<< "short_message"
<< "full_message"
<< "timestamp"
<< "level"
<< "facility"
<< "line"
<< "file";
}
QJsonObject GELFMessageModel::rowData(int row) const
{
return messages[row];
}
int GELFMessageModel::rowCount(const QModelIndex &/*parent*/) const
{
return messages.count();
}
int GELFMessageModel::columnCount(const QModelIndex &/*parent*/) const
{
return column_names.count();
}
QVariant GELFMessageModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole) {
if (orientation == Qt::Horizontal ) {
return column_names[section];
} else {
return QString("%1").arg(section);
}
}
return QVariant();
}
QVariant GELFMessageModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
QString column_name = column_names[index.column()];
QJsonObject message = messages[index.row()];
if (column_name == "timestamp") {
QDateTime timestamp;
timestamp.setTime_t(message[column_name].toDouble());
return timestamp;
} else if (message[column_name].isObject()) {
return QJsonDocument(message[column_name].toObject()).toJson(QJsonDocument::Compact);
} else if (message[column_name].isArray()) {
return QJsonDocument(message[column_name].toArray()).toJson(QJsonDocument::Compact);
} else {
return message[column_name].toVariant();
}
}
return QVariant();
}
void GELFMessageModel::onMessage(QJsonObject message)
{
QStringList keys = message.keys();
QStringListIterator i_keys(keys);
while (i_keys.hasNext()) {
QString key = i_keys.next();
if (!column_names.contains(key)) {
auto column_index = column_names.count();
beginInsertColumns(QModelIndex(), column_index, column_index);
column_names << key;
endInsertColumns();
}
}
// Insert row
auto row_index = messages.size();
beginInsertRows(QModelIndex(), row_index, row_index);
messages << message;
endInsertRows();
}