QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
LogFormatter.cc
Go to the documentation of this file.
1
#include "
LogFormatter.h
"
2
3
#include <QtCore/QJsonArray>
4
#include <QtCore/QJsonDocument>
5
#include <QtCore/QJsonObject>
6
7
#include "
LogEntry.h
"
8
9
namespace
LogFormatter
{
10
11
// ---------------------------------------------------------------------------
12
// Single-entry formatting
13
// ---------------------------------------------------------------------------
14
15
static
QString
escapeCsv
(
const
QString& field)
16
{
17
if
(field.contains(
','
) || field.contains(
'"'
) || field.contains(
'\n'
)) {
18
QString escaped = field;
19
escaped.replace(
'"'
, QStringLiteral(
"\"\""
));
20
return
'"'
+ escaped +
'"'
;
21
}
22
return
field;
23
}
24
25
QString
formatCsvRow
(
const
LogEntry
& entry)
26
{
27
return
escapeCsv
(entry.
timestamp
.toString(Qt::ISODateWithMs)) +
','
+ entry.
levelLabel
() +
','
+
28
escapeCsv
(entry.
category
) +
','
+
escapeCsv
(entry.
message
);
29
}
30
31
QString
csvHeader
()
32
{
33
return
QStringLiteral(
"timestamp,level,category,message"
);
34
}
35
36
// ---------------------------------------------------------------------------
37
// Batch formatting
38
// ---------------------------------------------------------------------------
39
40
QByteArray
format
(
const
QList<LogEntry>& entries,
int
fmt)
41
{
42
switch
(fmt) {
43
case
JSON
:
44
return
formatAsJson
(entries);
45
case
CSV
:
46
return
formatAsCsv
(entries);
47
case
JSONLines
:
48
return
formatAsJsonLines
(entries);
49
default
:
50
return
formatAsText
(entries);
51
}
52
}
53
54
QByteArray
formatAsText
(
const
QList<LogEntry>& entries)
55
{
56
QByteArray result;
57
result.reserve(entries.size() * 120);
58
for
(
const
auto
& e : entries) {
59
result.append(e.formatted.toUtf8());
60
result.append(
'\n'
);
61
}
62
return
result;
63
}
64
65
QJsonObject
entryToJson
(
const
LogEntry
& e,
JsonSchema
schema)
66
{
67
if
(schema ==
RemoteCompactSchema
) {
68
return
{
69
{
"t"
, e.
timestamp
.toMSecsSinceEpoch()},
70
{
"l"
,
static_cast<
int
>
(e.
level
)},
71
{
"c"
, e.
category
},
72
{
"m"
, e.
message
},
73
};
74
}
75
return
{
76
{
"timestamp"
, e.
timestamp
.toString(Qt::ISODateWithMs)},
77
{
"level"
, e.
levelLabel
()},
78
{
"category"
, e.
category
},
79
{
"message"
, e.
message
},
80
};
81
}
82
83
QByteArray
formatAsJson
(
const
QList<LogEntry>& entries)
84
{
85
QJsonArray array;
86
for
(
const
auto
& e : entries) {
87
array.append(
entryToJson
(e));
88
}
89
return
QJsonDocument(array).toJson(QJsonDocument::Indented);
90
}
91
92
QByteArray
formatAsCsv
(
const
QList<LogEntry>& entries)
93
{
94
QByteArray result;
95
result.reserve(entries.size() * 100);
96
result.append(
csvHeader
().toUtf8());
97
result.append(
'\n'
);
98
for
(
const
auto
& e : entries) {
99
result.append(
formatCsvRow
(e).toUtf8());
100
result.append(
'\n'
);
101
}
102
return
result;
103
}
104
105
QByteArray
formatAsJsonLines
(
const
QList<LogEntry>& entries)
106
{
107
QByteArray result;
108
result.reserve(entries.size() * 150);
109
for
(
const
auto
& e : entries) {
110
result.append(QJsonDocument(
entryToJson
(e)).toJson(QJsonDocument::Compact));
111
result.append(
'\n'
);
112
}
113
return
result;
114
}
115
116
}
// namespace LogFormatter
LogEntry.h
LogFormatter.h
LogFormatter
Definition
LogFormatter.cc:9
LogFormatter::formatAsText
QByteArray formatAsText(const QList< LogEntry > &entries)
Definition
LogFormatter.cc:54
LogFormatter::entryToJson
QJsonObject entryToJson(const LogEntry &e, JsonSchema schema)
Definition
LogFormatter.cc:65
LogFormatter::formatCsvRow
QString formatCsvRow(const LogEntry &entry)
Definition
LogFormatter.cc:25
LogFormatter::formatAsCsv
QByteArray formatAsCsv(const QList< LogEntry > &entries)
Definition
LogFormatter.cc:92
LogFormatter::formatAsJson
QByteArray formatAsJson(const QList< LogEntry > &entries)
Definition
LogFormatter.cc:83
LogFormatter::format
QByteArray format(const QList< LogEntry > &entries, int fmt)
Definition
LogFormatter.cc:40
LogFormatter::formatAsJsonLines
QByteArray formatAsJsonLines(const QList< LogEntry > &entries)
Definition
LogFormatter.cc:105
LogFormatter::csvHeader
QString csvHeader()
Definition
LogFormatter.cc:31
LogFormatter::JsonSchema
JsonSchema
Definition
LogFormatter.h:21
LogFormatter::RemoteCompactSchema
@ RemoteCompactSchema
Definition
LogFormatter.h:23
LogFormatter::CSV
@ CSV
Definition
LogFormatter.h:16
LogFormatter::JSONLines
@ JSONLines
Definition
LogFormatter.h:17
LogFormatter::JSON
@ JSON
Definition
LogFormatter.h:15
LogFormatter::escapeCsv
static QString escapeCsv(const QString &field)
Definition
LogFormatter.cc:15
LogEntry
Definition
LogEntry.h:8
LogEntry::message
QString message
Definition
LogEntry.h:40
LogEntry::timestamp
QDateTime timestamp
Definition
LogEntry.h:37
LogEntry::levelLabel
QString levelLabel() const
Definition
LogEntry.cc:7
LogEntry::level
Level level
Definition
LogEntry.h:38
LogEntry::category
QString category
Definition
LogEntry.h:39
src
Utilities
Logging
LogFormatter.cc
Generated by
1.9.8