-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogPage.cpp
executable file
·112 lines (96 loc) · 2.74 KB
/
LogPage.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
#include "LogPage.h"
#include "ui_LogPage.h"
#include <QDateTime>
LogPage::LogPage(QWidget *parent) :
QWidget(parent),
ui(new Ui::LogPage),
m_re(QStringLiteral(R"--(time="(.+?)" level=(.+?) msg="(.+)")--"))
{
ui->setupUi(this);
ui->logEdit->setFont(QFont(QStringLiteral("Consolas"), 10));
}
LogPage::~LogPage()
{
delete ui;
}
int LogPage::avgCharWidth() const
{
return ui->logEdit->fontMetrics().averageCharWidth();
}
void LogPage::appendLog(LogLevel level, const char *text)
{
QString html(genLogHeader(level));
html += text;
ui->logEdit->appendHtml(html);
}
void LogPage::appendLog(LogLevel level, const QString &text)
{
QString html(genLogHeader(level));
html += text;
ui->logEdit->appendHtml(html);
}
void LogPage::appendClashLog(const QString &text)
{
enum {
CapTime = 1,
CapLevel,
CapMsg,
};
QRegularExpressionMatchIterator iter = m_re.globalMatch(text);
if (!iter.hasNext()) {
ui->logEdit->appendPlainText(text);
return;
}
do {
QRegularExpressionMatch match = iter.next();
LogLevel level;
QStringRef levelStr = match.capturedRef(CapLevel);
if (levelStr == "debug") {
level = LogLevel::Debug;
} else if (levelStr == "info") {
level = LogLevel::Info;
} else if (levelStr == "warning") {
level = LogLevel::Warning;
} else if (levelStr == "error") {
level = LogLevel::Error;
} else {
level = LogLevel::Unknown;
}
QString html = genLogHeader(level, match.captured(CapTime));
html += match.capturedRef(CapMsg);
ui->logEdit->appendHtml(html);
} while (iter.hasNext());
}
QString LogPage::genLogHeader(LogLevel level, const QString &time)
{
QString result;
QDateTime dateTime;
if (time.isEmpty()) {
result += "<font color='#5c00e6'>";
dateTime = QDateTime::currentDateTime();
} else {
dateTime = QDateTime::fromString(time, Qt::ISODate);
}
result += dateTime.toString(QStringLiteral("yyyy-MM-dd HH:mm:ss"));
if (time.isEmpty()) {
result += "</font>";
}
switch (level) {
case LogLevel::Debug:
result += " DEBUG: ";
break;
case LogLevel::Info:
result += " INFO: ";
break;
case LogLevel::Warning:
result += " <font color='#ffcc00'>WARN</font>: ";
break;
case LogLevel::Error:
result += " <font color='#e63900'>ERROR</font>: ";
break;
default:
result += " <font color='#0000e6'>UNKNO</font>: ";
break;
}
return result;
}