Skip to content

Commit

Permalink
Use logging categories throughout Quaternion
Browse files Browse the repository at this point in the history
Closes #787.
  • Loading branch information
KitsuneRal committed Jul 28, 2023
1 parent 96debe0 commit 28eef67
Show file tree
Hide file tree
Showing 19 changed files with 180 additions and 107 deletions.
17 changes: 9 additions & 8 deletions client/accountselector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "accountselector.h"

#include "logging_categories.h"

#include <Quotient/accountregistry.h>
#include <Quotient/connection.h>

Expand All @@ -23,8 +25,8 @@ AccountSelector::AccountSelector(AccountRegistry* registry, QWidget* parent)
if (const auto idx = indexOfAccount(acc); idx == -1)
addItem(acc->userId(), QVariant::fromValue(acc));
else
qWarning() << "AccountSelector: refusing to add "
"the same account twice";
qCWarning(ACCOUNTSELECTOR)
<< "Refusing to add the same account twice";
}
});
connect(registry, &AccountRegistry::rowsAboutToBeRemoved, this,
Expand All @@ -35,9 +37,8 @@ AccountSelector::AccountSelector(AccountRegistry* registry, QWidget* parent)
if (const auto idx = indexOfAccount(acc); idx != -1)
removeItem(idx);
else
qWarning()
<< "AccountSelector: account to drop not found, "
"ignoring";
qCWarning(ACCOUNTSELECTOR)
<< "Account to drop not found, ignoring";
}
});
}
Expand All @@ -53,9 +54,9 @@ void AccountSelector::setAccount(Connection *newAccount)
return;
}
Q_ASSERT(false);
qWarning() << "AccountComboBox: account for"
<< newAccount->userId() + '/' + newAccount->deviceId()
<< "wasn't found in the full list of accounts";
qCWarning(ACCOUNTSELECTOR)
<< "Account for" << newAccount->userId() + '/' + newAccount->deviceId()
<< "wasn't found in the full list of accounts";
}

Connection* AccountSelector::currentAccount() const
Expand Down
4 changes: 3 additions & 1 deletion client/activitydetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "activitydetector.h"

#include "logging_categories.h"

#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtCore/QDebug>
Expand All @@ -26,7 +28,7 @@ void ActivityDetector::setEnabled(bool enabled)
qApp->installEventFilter(this);
else
qApp->removeEventFilter(this);
qDebug() << "Activity Detector enabled:" << enabled;
qCDebug(MAIN) << "Activity Detector enabled:" << enabled;
}

bool ActivityDetector::eventFilter(QObject* obj, QEvent* ev)
Expand Down
7 changes: 4 additions & 3 deletions client/chatedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "chatroomwidget.h"
#include "htmlfilter.h"
#include "timelinewidget.h"
#include "logging_categories.h"

#include <QtWidgets/QMenu>
#if QT_VERSION_MAJOR < 6
Expand Down Expand Up @@ -106,7 +107,7 @@ void ChatEdit::alternatePaste()
void ChatEdit::insertFromMimeData(const QMimeData *source)
{
if (!source) {
qWarning() << "Nothing to insert";
qCWarning(MSGINPUT) << "Nothing to insert";
return;
}

Expand All @@ -122,8 +123,8 @@ void ChatEdit::insertFromMimeData(const QMimeData *source)
const auto [cleanHtml, errorPos, errorString] =
HtmlFilter::fromLocalHtml(source->html());
if (errorPos != -1) {
qWarning() << "HTML insertion failed at pos" << errorPos
<< "with error" << errorString;
qCWarning(MSGINPUT) << "HTML insertion failed at pos"
<< errorPos << "with error" << errorString;
// FIXME: Come on... It should be app->showStatusMessage() or smth
emit chatRoomWidget->timelineWidget()->showStatusMessage(
tr("Could not insert HTML - it's either invalid or unsupported"),
Expand Down
5 changes: 3 additions & 2 deletions client/chatroomwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "quaternionroom.h"
#include "chatedit.h"
#include "htmlfilter.h"
#include "logging_categories.h"

static auto DefaultPlaceholderText()
{
Expand Down Expand Up @@ -245,7 +246,7 @@ void ChatRoomWidget::setRoom(QuaternionRoom* newRoom)
connect(newRoom, &Room::encryption, //
this, &ChatRoomWidget::encryptionChanged);
connect(newRoom->connection(), &Connection::loggedOut, this, [this] {
qWarning() << "Logged out, escaping the room";
qCWarning(MSGINPUT) << "Logged out, escaping the room";
setRoom(nullptr);
});
}
Expand Down Expand Up @@ -680,7 +681,7 @@ QString ChatRoomWidget::sendCommand(QStringView command,
return {};
}
// --- Add more room commands here
qDebug() << "Unknown command:" << command;
qCDebug(MSGINPUT) << "Unknown command:" << command;
return tr("Unknown /command. Use // to send this line literally");
}

Expand Down
29 changes: 17 additions & 12 deletions client/htmlfilter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "htmlfilter.h"

#include "logging_categories.h"

#include <Quotient/util.h>

#include <QtGui/QTextDocument>
Expand Down Expand Up @@ -96,7 +98,7 @@ static const auto mxBgColorAttr = u"data-mx-bg-color";
|| reader.qualifiedName() != u"p")
if (reader.atEnd()) {
Q_ASSERT_X(false, __FUNCTION__, "Malformed Qt markup");
qCritical()
qCCritical(HTMLFILTER)
<< "The passed text doesn't seem to come from QTextDocument";
return {};
}
Expand Down Expand Up @@ -135,7 +137,7 @@ static const auto mxBgColorAttr = u"data-mx-bg-color";
case QXmlStreamReader::Comment:
continue; // Just drop comments
default:
qWarning() << "Unexpected token, type" << tokenType;
qCWarning(HTMLFILTER) << "Unexpected token, type" << tokenType;
}
if (depth < 0) {
Q_ASSERT(tokenType == QXmlStreamReader::EndElement
Expand Down Expand Up @@ -458,7 +460,8 @@ void Processor::runOn(const QString &html)

tagsStack.emplace();
if (tagsStack.size() > 100)
qCritical() << "CS API spec limits HTML tags depth at 100";
qCCritical(HTMLFILTER)
<< "CS API spec limits HTML tags depth at 100";

// Qt hardcodes the link style in a `<span>` under `<a>`.
// This breaks the looks on the receiving side if the sender
Expand Down Expand Up @@ -525,8 +528,9 @@ void Processor::runOn(const QString &html)
if (tagsStack.empty()) {
const auto& tagName = reader.qualifiedName();
if (tagName != u"body" && tagName != u"html")
qWarning() << "filterHtml(): empty tags stack, skipping"
<< ('/' + tagName.toString());
qCWarning(HTMLFILTER)
<< "filterHtml(): empty tags stack, skipping"
<< ('/' + tagName.toString());
break;
}
// Close as many elements as were opened in case StartElement
Expand All @@ -539,8 +543,8 @@ void Processor::runOn(const QString &html)
break;
case QXmlStreamReader::EndDocument:
if (!tagsStack.empty())
qWarning().noquote().nospace()
<< __FUNCTION__ << ": Not all HTML tags closed";
qCWarning(HTMLFILTER).noquote().nospace()
<< ": Not all HTML tags closed";
if (mode == GenericToQt)
writer.writeEndDocument(); // </body></html>
break;
Expand All @@ -550,14 +554,15 @@ void Processor::runOn(const QString &html)
case QXmlStreamReader::Invalid: {
errorPos = reader.characterOffset() - bodyOffset;
errorString = reader.errorString();
qCritical() << "Invalid XHTML:" << html;
qCritical().nospace()
qCCritical(HTMLFILTER) << "Invalid XHTML:" << html;
qCCritical(HTMLFILTER).nospace()
<< "Error at char " << errorPos << ": " << errorString;
const auto remainder =
QStringView(html).mid(reader.characterOffset());
qCritical().nospace() << "Buffer at error: " << remainder << ", "
<< html.size() - reader.characterOffset()
<< " character(s) remaining";
qCCritical(HTMLFILTER).nospace()
<< "Buffer at error: " << remainder << ", "
<< html.size() - reader.characterOffset()
<< " character(s) remaining";
break;
}
case QXmlStreamReader::Comment:
Expand Down
23 changes: 14 additions & 9 deletions client/imageprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "imageprovider.h"

#include "logging_categories.h"

#include <Quotient/connection.h>
#include <Quotient/jobs/mediathumbnailjob.h>

Expand All @@ -32,17 +34,18 @@ class ThumbnailResponse : public QQuickImageResponse
tr("Media id '%1' doesn't follow server/mediaId pattern")
.arg(mediaId);
else if (requestedSize.isEmpty()) {
qDebug() << "ThumbnailResponse: returning an empty image for"
<< mediaId << "due to empty" << requestedSize;
qCDebug(IMAGEPROVIDER)
<< "Returning an empty image for" << mediaId
<< "due to empty" << requestedSize;
image = {requestedSize, QImage::Format_Invalid};
}
if (!errorStr.isEmpty() || requestedSize.isEmpty()) {
emit finished();
return;
}
// We are good to go
qDebug().nospace() << "ThumbnailResponse: requesting " << mediaId
<< ", " << size;
qCDebug(IMAGEPROVIDER).nospace()
<< "Requesting " << mediaId << ", " << size;
errorStr = tr("Image request is pending");

// Execute a request on the main thread asynchronously
Expand Down Expand Up @@ -75,15 +78,17 @@ class ThumbnailResponse : public QQuickImageResponse
{
image = job->thumbnail();
errorStr.clear();
qDebug().nospace() << "ThumbnailResponse: image ready for "
<< mediaId << ", " << image.size();
qCDebug(IMAGEPROVIDER).nospace()
<< "Image ready for " << mediaId << ", "
<< image.size();
} else if (job->error() == BaseJob::Abandoned) {
errorStr = tr("Image request has been cancelled");
qDebug() << "ThumbnailResponse: cancelled for" << mediaId;
qCDebug(IMAGEPROVIDER)
<< "Request cancelled for" << mediaId;
} else {
errorStr = job->errorString();
qWarning() << "ThumbnailResponse: no valid image for"
<< mediaId << "-" << errorStr;
qCWarning(IMAGEPROVIDER).nospace()
<< "No valid image for" << mediaId << ": " << errorStr;
}
}
job = nullptr;
Expand Down
23 changes: 23 additions & 0 deletions client/logging_categories.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

// NB: Only include this file from .cpp

#include <QtCore/QLoggingCategory>

// Reusing the macro defined in Quotient - these must never cross ways
#define QUO_LOGGING_CATEGORY(Name, Id) \
inline Q_LOGGING_CATEGORY((Name), (Id), QtInfoMsg)

namespace {
QUO_LOGGING_CATEGORY(MAIN, "quaternion.main")
QUO_LOGGING_CATEGORY(ACCOUNTSELECTOR, "quaternion.accountselector")
QUO_LOGGING_CATEGORY(MODELS, "quaternion.models")
QUO_LOGGING_CATEGORY(EVENTMODEL, "quaternion.models.events")
QUO_LOGGING_CATEGORY(TIMELINE, "quaternion.timeline")
QUO_LOGGING_CATEGORY(HTMLFILTER, "quaternion.htmlfilter")
QUO_LOGGING_CATEGORY(MSGINPUT, "quaternion.messageinput")
QUO_LOGGING_CATEGORY(IMAGEPROVIDER, "quaternion.imageprovider")

// Only to be used in QML; shows up here for documentation purpose only
[[maybe_unused]] QUO_LOGGING_CATEGORY(TIMELINEQML, "quaternion.timeline.qml")
}
6 changes: 4 additions & 2 deletions client/logindialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "logindialog.h"

#include "logging_categories.h"

#include <Quotient/connection.h>
#include <Quotient/accountregistry.h>
#include <Quotient/qt_connection_util.h>
Expand Down Expand Up @@ -177,7 +179,7 @@ void LoginDialog::setup(const QString& statusMessage)
// the server URL from a changed MXID
connect(m_connection.get(), &Connection::resolveError, this,
[this](const QString& message) {
qDebug() << "Resolve error";
qCDebug(MAIN) << "Failed to resolve the homeserver:" << message;
serverEdit->clear();
setStatusMessage(message);
});
Expand Down Expand Up @@ -237,7 +239,7 @@ void LoginDialog::apply()
// Wait for new flows and check them
connectSingleShot(m_connection.get(), &Connection::loginFlowsChanged,
this, [this] {
qDebug()
qCDebug(MAIN)
<< "Received login flows, trying to login";
loginWithBestFlow();
});
Expand Down
19 changes: 11 additions & 8 deletions client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <QtQuickControls2/QQuickStyle>

#include "mainwindow.h"
#include "logging_categories.h"
#include "linuxutils.h"

#include <Quotient/networksettings.h>
Expand All @@ -32,12 +33,14 @@ void loadTranslations(
|| translator->load(QLocale(), configName, "_", configPath)) {
auto path = translator->filePath();
if ((loaded = QApplication::installTranslator(translator)))
qDebug().noquote() << "Loaded translations from" << path;
qCDebug(MAIN).noquote()
<< "Loaded translations from" << path;
else
qWarning().noquote()
qCWarning(MAIN).noquote()
<< "Failed to load translations from" << path;
} else
qDebug() << "No translations for" << configName << "at" << configPath;
qCDebug(MAIN) << "No translations for" << configName << "at"
<< configPath;
if (!loaded)
delete translator;
}
Expand Down Expand Up @@ -100,15 +103,15 @@ int main( int argc, char* argv[] )
fontPointSize > 0)
font.setPointSizeF(fontPointSize);

qDebug() << "Using application font:" << font.toString();
qCInfo(MAIN) << "Using application font:" << font.toString();
QApplication::setFont(font);
}

// We should not need to do the following, as quitOnLastWindowClosed is
// set to "true" by default; might be a bug, see
// https://forum.qt.io/topic/71112/application-does-not-quit
QObject::connect(&app, &QApplication::lastWindowClosed, &app, [&app]{
qDebug() << "Last window closed!";
qCDebug(MAIN) << "Last window closed!";
QApplication::postEvent(&app, new QEvent(QEvent::Quit));
});

Expand Down Expand Up @@ -137,7 +140,7 @@ int main( int argc, char* argv[] )
if (!overrideLocale.isEmpty())
{
QLocale::setDefault(QLocale(overrideLocale));
qInfo() << "Using locale" << QLocale().name();
qCInfo(MAIN) << "Using locale" << QLocale().name();
}

loadTranslations(
Expand All @@ -160,11 +163,11 @@ int main( int argc, char* argv[] )

MainWindow window;
if (parser.isSet(hideMainWindow)) {
qDebug() << "--- Hide time!";
qCDebug(MAIN) << "--- Hide time!";
window.hide();
}
else {
qDebug() << "--- Show time!";
qCDebug(MAIN) << "--- Show time!";
window.show();
}

Expand Down
Loading

0 comments on commit 28eef67

Please sign in to comment.