-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogParser.cpp
More file actions
244 lines (219 loc) · 6.38 KB
/
LogParser.cpp
File metadata and controls
244 lines (219 loc) · 6.38 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (C) 2023 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "LogParser.h"
#include "AppConfig.h"
#include "LogProfile.h"
#include <QRegularExpression>
#include <memory>
namespace
{
const QString MATCH_GROUP_DATE = "date";
const QString MATCH_GROUP_TIME = "time";
const QString MATCH_GROUP_THREAD = "thread";
const QString MATCH_GROUP_SUBSYS = "subsys";
const QString MATCH_GROUP_MESSAGE = "message";
const QString MATCH_GROUP_WHERE = "where";
const QString MATCH_GROUP_LEVEL = "level";
}
LogParser::~LogParser()
{
if (ownsFile)
{
delete inputFile;
}
}
void LogParser::FindLogProfile(QTextStream* inputStream)
{
int line = 0;
while (!inputStream->atEnd())
{
logProfile = AppConfig::GetInstance()->FindProfile(inputStream->readLine(), ++line);
if (logProfile)
{
break;
}
}
if (!logProfile) logProfile = LogProfile::GetDefault();
inputStream->seek(0);
for(const auto& level : logProfile->GetLogLevels())
{
logLevelMap.insert(level->GetLevelName(), level);
}
}
std::vector<LogEntry> LogParser::Parse()
{
std::vector<LogEntry> entries;
entries.reserve(100000);
std::unique_ptr<QTextStream> inputStream;
if (inputFile)
{
if (!inputFile->open(QIODevice::ReadOnly)) { return entries; }
inputStream = std::make_unique<QTextStream>(inputFile);
}
else
{
inputStream = std::make_unique<QTextStream>(&logMessage, QIODevice::ReadOnly);
}
inputStream->setEncoding(QStringConverter::Utf8);
FindLogProfile(inputStream.get());
LoadRegexesFromProfile();
//TODO fill logType with known log types from profile
QString msg;
uint64_t currentLine = 1;
while (!(msg = GetNextMessage(*inputStream)).isEmpty())
{
if (!msg.isEmpty()) entries.push_back(ParseMessage(msg, currentLine));
currentLine = lineNumber;
}
if (inputFile)
{
inputFile->close();
}
return entries;
}
void LogParser::LoadRegexesFromProfile()
{
logEntryRegex = QRegularExpression(logProfile->GetLogEntryRegex());
newLogEntryStart = QRegularExpression(logProfile->GetNewLogEntryStartRegex());
versionRegex = QRegularExpression(logProfile->GetSystemInfoVersionRegex());
deviceRegex = QRegularExpression(logProfile->GetSystemInfoDeviceRegex());
osRegex = QRegularExpression(logProfile->GetSystemInfoOsRegex());
}
bool LogParser::IsNewLogMessage(const QString& string)
{
if (string.isEmpty()) return false;
return newLogEntryStart.match(string).hasMatch();
}
QString LogParser::GetNextMessage(QTextStream& inputStream)
{
QString message;
while((message.isEmpty() || !IsNewLogMessage(readAhead)) && (!inputStream.atEnd() || !readAhead.isNull()))
{
if (message.isEmpty())
{
message = readAhead;
}
else if (!readAhead.isEmpty())
{
message += QChar(0x000023CE); //("⏎");
message += readAhead;
}
readAhead = inputStream.readLine();
lineNumber++;
}
return message;
}
QString GetMatchFromRawData(const QRegularExpressionMatch& match, const QString& group)
{
QStringView view = match.capturedView(group);
return QString::fromRawData((QChar*)view.utf16(), view.length());
}
LogEntry LogParser::ParseMessage(const QString& message, uint64_t startLineNumber)
{
LogEntry e;
e.entryNumber = ++entryCount;
e.lineNumber = startLineNumber;
const auto match = logEntryRegex.match(message);
TryExtractEnvironment(message);
e.components[LogComponent::ORIGINAL_MESSAGE] = message;
if (match.hasMatch())
{
e.components[LogComponent::DATE] = GetMatchFromRawData(match, MATCH_GROUP_DATE);
e.components[LogComponent::TIME] = GetMatchFromRawData(match, MATCH_GROUP_TIME);
e.components[LogComponent::THREAD] = GetMatchFromRawData(match, MATCH_GROUP_THREAD);
e.components[LogComponent::SUB_SYS] = GetMatchFromRawData(match, MATCH_GROUP_SUBSYS);
e.components[LogComponent::MESSAGE] = GetMatchFromRawData(match, MATCH_GROUP_MESSAGE);
e.components[LogComponent::WHERE] = GetMatchFromRawData(match, MATCH_GROUP_WHERE);
// Read log level
const QString type = GetMatchFromRawData(match, MATCH_GROUP_LEVEL);
e.level = GetLogLevel(type);
//TODO
e.timeStamp = QDateTime::fromString("20" + e.components[LogComponent::DATE] + ' ' + e.components[LogComponent::TIME], Qt::ISODateWithMs);
}
else
{
e.components[LogComponent::MESSAGE] = message;
e.level = GetLogLevel("");
}
return e;
}
inline void ExtractEnvironmentComponent(const QString& message, QString& targetVar, const QString& captureGroupName, const QRegularExpression& expression)
{
if (targetVar.isEmpty())
{
const auto match = expression.match(message);
if (match.hasMatch())
{
targetVar = match.captured(captureGroupName);
}
}
}
void LogParser::TryExtractEnvironment(const QString& message)
{
if (entryCount > logProfile->GetSystemInfoLinesToCheck()) return;
if (version.isEmpty())
{
const auto match = versionRegex.match(message);
if (match.hasMatch())
{
version = match.captured("version") + match.captured("tags");
auto bnr = match.captured("buildnr");
if (!bnr.isEmpty())
{
version += " (" + bnr + ")";
}
}
}
ExtractEnvironmentComponent(message, device, "device", deviceRegex);
ExtractEnvironmentComponent(message, os, "os", osRegex);
}
[[nodiscard]] QString LogParser::GetSystemInfo() const
{
QString systemInfo;
if (!version.isEmpty())
{
systemInfo += "Version: " + version;
}
if (!device.isEmpty())
{
systemInfo += "\tDevice: " + device;
}
if (!os.isEmpty())
{
systemInfo += "\tOS: " + os;
}
return systemInfo;
}
std::shared_ptr<LogLevel> LogParser::GetLogLevel(const QString& type)
{
if (logLevelMap.contains(type))
{
return logLevelMap[type];
}
auto level = std::make_shared<LogLevel>(type);
logLevelMap.insert(type, level);
return level;
}
std::vector<std::shared_ptr<LogLevel>> LogParser::GetUsedLogLevels() const
{
std::vector<std::shared_ptr<LogLevel>> usedLevels;
for(const auto& level : logLevelMap)
{
usedLevels.push_back(level);
}
return usedLevels;
}