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
escapeCsv
(entry.
file
) +
','
+ (entry.
file
.isEmpty() ? QString() : QString::number(entry.
line
));
30
}
31
32
QString
csvHeader
()
33
{
34
return
QStringLiteral(
"timestamp,level,category,message,file,line"
);
35
}
36
37
// ---------------------------------------------------------------------------
38
// Batch formatting
39
// ---------------------------------------------------------------------------
40
41
QByteArray
format
(
const
QList<LogEntry>& entries,
int
fmt)
42
{
43
switch
(fmt) {
44
case
JSON
:
45
return
formatAsJson
(entries);
46
case
CSV
:
47
return
formatAsCsv
(entries);
48
case
JSONLines
:
49
return
formatAsJsonLines
(entries);
50
default
:
51
return
formatAsText
(entries);
52
}
53
}
54
55
QByteArray
formatAsText
(
const
QList<LogEntry>& entries)
56
{
57
QByteArray result;
58
result.reserve(entries.size() * 150);
59
for
(
const
auto
& e : entries) {
60
result.append(e.formatted.toUtf8());
61
if
(!e.file.isEmpty()) {
62
if
(e.line > 0) {
63
result.append(QStringLiteral(
" (%1:%2)"
).arg(e.file).arg(e.line).toUtf8());
64
}
else
{
65
result.append(QStringLiteral(
" (%1)"
).arg(e.file).toUtf8());
66
}
67
}
68
result.append(
'\n'
);
69
}
70
return
result;
71
}
72
73
QJsonObject
entryToJson
(
const
LogEntry
& e,
JsonSchema
schema)
74
{
75
if
(schema ==
RemoteCompactSchema
) {
76
return
{
77
{
"t"
, e.
timestamp
.toMSecsSinceEpoch()},
78
{
"l"
,
static_cast<
int
>
(e.
level
)},
79
{
"c"
, e.
category
},
80
{
"m"
, e.
message
},
81
};
82
}
83
return
{
84
{
"timestamp"
, e.
timestamp
.toString(Qt::ISODateWithMs)},
85
{
"level"
, e.
levelLabel
()},
86
{
"category"
, e.
category
},
87
{
"message"
, e.
message
},
88
};
89
}
90
91
QByteArray
formatAsJson
(
const
QList<LogEntry>& entries)
92
{
93
QJsonArray array;
94
for
(
const
auto
& e : entries) {
95
array.append(
entryToJson
(e));
96
}
97
return
QJsonDocument(array).toJson(QJsonDocument::Indented);
98
}
99
100
QByteArray
formatAsCsv
(
const
QList<LogEntry>& entries)
101
{
102
QByteArray result;
103
result.reserve(entries.size() * 100);
104
result.append(
csvHeader
().toUtf8());
105
result.append(
'\n'
);
106
for
(
const
auto
& e : entries) {
107
result.append(
formatCsvRow
(e).toUtf8());
108
result.append(
'\n'
);
109
}
110
return
result;
111
}
112
113
QByteArray
formatAsJsonLines
(
const
QList<LogEntry>& entries)
114
{
115
QByteArray result;
116
result.reserve(entries.size() * 150);
117
for
(
const
auto
& e : entries) {
118
result.append(QJsonDocument(
entryToJson
(e)).toJson(QJsonDocument::Compact));
119
result.append(
'\n'
);
120
}
121
return
result;
122
}
123
124
}
// namespace LogFormatter
LogEntry.h
LogFormatter.h
LogFormatter
Definition
LogFormatter.cc:9
LogFormatter::formatAsText
QByteArray formatAsText(const QList< LogEntry > &entries)
Definition
LogFormatter.cc:55
LogFormatter::entryToJson
QJsonObject entryToJson(const LogEntry &e, JsonSchema schema)
Definition
LogFormatter.cc:73
LogFormatter::formatCsvRow
QString formatCsvRow(const LogEntry &entry)
Definition
LogFormatter.cc:25
LogFormatter::formatAsCsv
QByteArray formatAsCsv(const QList< LogEntry > &entries)
Definition
LogFormatter.cc:100
LogFormatter::formatAsJson
QByteArray formatAsJson(const QList< LogEntry > &entries)
Definition
LogFormatter.cc:91
LogFormatter::format
QByteArray format(const QList< LogEntry > &entries, int fmt)
Definition
LogFormatter.cc:41
LogFormatter::formatAsJsonLines
QByteArray formatAsJsonLines(const QList< LogEntry > &entries)
Definition
LogFormatter.cc:113
LogFormatter::csvHeader
QString csvHeader()
Definition
LogFormatter.cc:32
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::file
QString file
Definition
LogEntry.h:41
LogEntry::line
int line
Definition
LogEntry.h:46
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
LogManager
LogFormatter.cc
Generated by
1.9.8