Skip to content

Commit

Permalink
refactor: Add logging category to debug log.
Browse files Browse the repository at this point in the history
This is useful to distinguish log sources. In the future, we can use
this to e.g. make more verbose logging optional or allow
category-filtering in the debug log view.
  • Loading branch information
iphydf committed Dec 20, 2024
1 parent 3c1edb6 commit 725bc7b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/appmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,14 @@ void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QSt
}

const QString file = canonicalLogFilePath(ctxt.file);
const QString category =
ctxt.category ? QString::fromUtf8(ctxt.category) : QStringLiteral("default");

Check warning on line 109 in src/appmanager.cpp

View check run for this annotation

Codecov / codecov/patch

src/appmanager.cpp#L109

Added line #L109 was not covered by tests

// Time should be in UTC to save user privacy on log sharing
QTime time = QDateTime::currentDateTime().toUTC().time();
QString logPrefix =
QStringLiteral("[%1 UTC] %2:%3 : ").arg(time.toString("HH:mm:ss.zzz")).arg(file).arg(ctxt.line);
QStringLiteral("[%1 UTC] (%2) %3:%4 : ")
.arg(time.toString("HH:mm:ss.zzz"), category, file, QString::number(ctxt.line));

Check warning on line 115 in src/appmanager.cpp

View check run for this annotation

Codecov / codecov/patch

src/appmanager.cpp#L114-L115

Added lines #L114 - L115 were not covered by tests
switch (type) {
case QtDebugMsg:
logPrefix += "Debug";
Expand Down
33 changes: 18 additions & 15 deletions src/model/debug/debuglogmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ QString renderMsgType(QtMsgType type)

QList<DebugLogModel::LogEntry> parse(const QStringList& logs)
{
// Regex extraction of log entry
// [12:35:16.634 UTC] (default) src/core/core.cpp:370 : Debug: Connected to a TCP relay
// ^ ^ ^ ^ ^ ^
// time category file line type message
static const QRegularExpression re(
R"(\[([0-9:.]*) UTC\](?: \(([^)]*)\))? (.*?):(\d+) : ([^:]+): (.*))");

Check warning on line 57 in src/model/debug/debuglogmodel.cpp

View check run for this annotation

Codecov / codecov/patch

src/model/debug/debuglogmodel.cpp#L57

Added line #L57 was not covered by tests

QList<DebugLogModel::LogEntry> result;
for (const QString& log : logs) {
// Regex extraction of log entry
// [12:35:16.634 UTC] src/core/core.cpp:370 : Debug: Connected to a TCP relay
// ^ ^ ^ ^ ^
// time file line type message
const QRegularExpression re(R"(\[(.*) UTC\] (.*):(\d+) : ([^:]+): (.*))");
const auto match = re.match(log);
if (!match.hasMatch()) {
qWarning() << "Failed to parse log entry:" << log;
Expand All @@ -65,23 +67,24 @@ QList<DebugLogModel::LogEntry> parse(const QStringList& logs)
DebugLogModel::LogEntry entry;
entry.index = result.size();
entry.time = match.captured(1);
entry.file = match.captured(2);
entry.line = match.captured(3).toInt();
entry.type = parseMsgType(match.captured(4));
entry.message = match.captured(5);
entry.category = match.captured(2);
if (entry.category.isEmpty()) {
entry.category = QStringLiteral("default");

Check warning on line 72 in src/model/debug/debuglogmodel.cpp

View check run for this annotation

Codecov / codecov/patch

src/model/debug/debuglogmodel.cpp#L70-L72

Added lines #L70 - L72 were not covered by tests
}
entry.file = match.captured(3);
entry.line = match.captured(4).toInt();
entry.type = parseMsgType(match.captured(5));
entry.message = match.captured(6);

Check warning on line 77 in src/model/debug/debuglogmodel.cpp

View check run for this annotation

Codecov / codecov/patch

src/model/debug/debuglogmodel.cpp#L74-L77

Added lines #L74 - L77 were not covered by tests
result.append(entry);
}
return result;
}

QString render(const DebugLogModel::LogEntry& entry)
{
return QStringLiteral("[%1] %2:%3 : %4: %5")
.arg(entry.time)
.arg(entry.file)
.arg(entry.line)
.arg(renderMsgType(entry.type))
.arg(entry.message);
return QStringLiteral("[%1 UTC] (%2) %3:%4 : %5: %6")
.arg(entry.time, entry.category, entry.file, QString::number(entry.line),
renderMsgType(entry.type), entry.message);

Check warning on line 87 in src/model/debug/debuglogmodel.cpp

View check run for this annotation

Codecov / codecov/patch

src/model/debug/debuglogmodel.cpp#L85-L87

Added lines #L85 - L87 were not covered by tests
}

bool filterAccepts(DebugLogModel::Filter filter, QtMsgType type)
Expand Down
1 change: 1 addition & 0 deletions src/model/debug/debuglogmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class DebugLogModel : public QAbstractListModel
int index;

QString time;
QString category;
QString file;
int line;
QtMsgType type;
Expand Down

0 comments on commit 725bc7b

Please sign in to comment.