fix(logging): keep Qt's platform chatter out of the journal
Opening a dialog under Wayland made Qt write a burst of qt.qpa warnings into the application journal — the text-input focus one, repeated at every prompt — where the user can do nothing with them: they describe the display server, not the application. Those categories stay on stderr from now on, next to everything else, while what the application logs (its own categories, and a bare qCWarning) keeps going to the journal.
This commit is contained in:
4 files changed
+85
-15
No files matched your search
@@ -51,6 +51,15 @@ Depuis un paquet installé, la sortie part dans le journal :
|
||||
journalctl --user -f | grep pacmgr
|
||||
```
|
||||
|
||||
Le journal de l'application **ne reprend pas** la sortie interne de Qt : les
|
||||
catégories de la couche graphique (`qt.qpa.*`, par exemple
|
||||
`qt.qpa.wayland.textinput`, qui redit un avertissement de focus à chaque boîte de
|
||||
dialogue ouverte sous Wayland) décrivent le serveur d'affichage, pas
|
||||
l'application, et noieraient le journal sous des lignes sur lesquelles
|
||||
l'utilisateur ne peut rien. Elles restent sur la sortie standard — c'est là
|
||||
qu'un développeur les cherche — et tout ce que l'application journalise
|
||||
d'elle-même (événements, commandes, `pacmgr.*`) y est toujours.
|
||||
|
||||
Les commandes réellement lancées par les fournisseurs y sont tracées (ligne
|
||||
`$ …`, cache `cache · …`, compteurs de résultats), ce qui permet de reproduire
|
||||
l'échec à la main dans un terminal. Le détail est dans
|
||||
|
||||
+25
-14
@@ -229,6 +229,13 @@ bool LogManager::hasEntries() const {
|
||||
return !readFile().isEmpty();
|
||||
}
|
||||
|
||||
bool LogManager::journalsQtCategory(const QString& category) {
|
||||
// Only the platform plugin is filtered. A category the application chose
|
||||
// (pacmgr.*, the "default" one a bare qCWarning carries) describes what the
|
||||
// app did and stays in the journal.
|
||||
return !category.startsWith(QStringLiteral("qt.qpa"));
|
||||
}
|
||||
|
||||
void LogManager::installQtMessageHandler() {
|
||||
// Make sure the singleton is created on the calling (main) thread before any
|
||||
// worker emits a log message.
|
||||
@@ -237,23 +244,27 @@ void LogManager::installQtMessageHandler() {
|
||||
qInstallMessageHandler([](QtMsgType type, const QMessageLogContext& context,
|
||||
const QString& message) {
|
||||
// Keep the usual console output too, so a developer still sees the logs
|
||||
// in a terminal.
|
||||
const char* category = context.category ? context.category : "default";
|
||||
std::fprintf(stderr, "[%s] %s\n", category, message.toLocal8Bit().constData());
|
||||
// in a terminal — including the platform chatter the journal leaves
|
||||
// out.
|
||||
const char* rawCategory = context.category ? context.category : "default";
|
||||
std::fprintf(stderr, "[%s] %s\n", rawCategory, message.toLocal8Bit().constData());
|
||||
|
||||
QString level;
|
||||
switch (type) {
|
||||
case QtDebugMsg: level = QStringLiteral("debug"); break;
|
||||
case QtInfoMsg: level = QStringLiteral("info"); break;
|
||||
case QtWarningMsg: level = QCoreApplication::translate("LogManager", "avertissement"); break;
|
||||
case QtCriticalMsg: level = QCoreApplication::translate("LogManager", "critique"); break;
|
||||
case QtFatalMsg: level = QStringLiteral("fatal"); break;
|
||||
const QString category = QString::fromLatin1(rawCategory);
|
||||
if (journalsQtCategory(category)) {
|
||||
QString level;
|
||||
switch (type) {
|
||||
case QtDebugMsg: level = QStringLiteral("debug"); break;
|
||||
case QtInfoMsg: level = QStringLiteral("info"); break;
|
||||
case QtWarningMsg: level = QCoreApplication::translate("LogManager", "avertissement"); break;
|
||||
case QtCriticalMsg: level = QCoreApplication::translate("LogManager", "critique"); break;
|
||||
case QtFatalMsg: level = QStringLiteral("fatal"); break;
|
||||
}
|
||||
|
||||
LogManager::instance().logEvent(
|
||||
QStringLiteral("qt/%1").arg(category),
|
||||
QStringLiteral("%1: %2").arg(level, message));
|
||||
}
|
||||
|
||||
LogManager::instance().logEvent(
|
||||
QStringLiteral("qt/%1").arg(QString::fromLatin1(category)),
|
||||
QStringLiteral("%1: %2").arg(level, message));
|
||||
|
||||
if (type == QtFatalMsg) std::abort();
|
||||
});
|
||||
}
|
||||
+12
-1
@@ -33,7 +33,9 @@
|
||||
//
|
||||
// Every Qt log message (qCDebug/qCInfo/qCWarning/qCCritical) is funnelled into
|
||||
// the same journal by installQtMessageHandler(), so the categories already used
|
||||
// across the code base feed it without extra plumbing.
|
||||
// across the code base feed it without extra plumbing. Qt's own platform
|
||||
// categories (`qt.qpa.*`) are the one exception: they describe the display
|
||||
// server, not the application, and stay on stderr (see journalsQtCategory).
|
||||
//
|
||||
// The file lives in the user's own data tree (XDG_CONFIG_HOME / home) so it is
|
||||
// never lost when a sandbox is rebuilt, and both that directory and a size cap
|
||||
@@ -87,6 +89,15 @@ public:
|
||||
// QApplication exists. Safe to call more than once.
|
||||
static void installQtMessageHandler();
|
||||
|
||||
// Whether a Qt log category belongs in the journal at all. Qt's platform
|
||||
// plugins (`qt.qpa.*`: xcb, wayland, ...) narrate the display server, not
|
||||
// the application: the Wayland one, for instance, repeats a warning about
|
||||
// its text-input focus for every dialog the user opens, which is a line the
|
||||
// user can neither read nor act on. Those keep going to stderr, where a
|
||||
// developer looks for them; everything the application itself logs — its
|
||||
// own categories, and a bare qCWarning — still lands in the journal.
|
||||
static bool journalsQtCategory(const QString& category);
|
||||
|
||||
signals:
|
||||
// The journal changed on disk and a fresh copy of its content is available.
|
||||
void fileUpdated();
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <QDateTime>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QLoggingCategory>
|
||||
#include <QObject>
|
||||
#include <QSettings>
|
||||
#include <QString>
|
||||
@@ -271,6 +272,43 @@ void testJournalMovesAndRotates() {
|
||||
assert(journal.currentSizeBytes() > before);
|
||||
}
|
||||
|
||||
// Qt's platform plugins narrate the display server, not the application: the
|
||||
// Wayland text-input warning, for one, repeats for every dialog the user opens,
|
||||
// and a journal full of it is unreadable. Those categories stay on stderr; what
|
||||
// the application logs — its own categories, and the "default" one a bare
|
||||
// qCWarning carries — still lands in the journal.
|
||||
void testPlatformChatterStaysOutOfTheJournal() {
|
||||
// The decision itself first, so the test says which side a category is on
|
||||
// even when nothing is written.
|
||||
assert(LogManager::journalsQtCategory(QStringLiteral("default")));
|
||||
assert(LogManager::journalsQtCategory(QStringLiteral("pacmgr.update")));
|
||||
assert(LogManager::journalsQtCategory(QStringLiteral("qt.sql")));
|
||||
assert(!LogManager::journalsQtCategory(QStringLiteral("qt.qpa.wayland.textinput")));
|
||||
assert(!LogManager::journalsQtCategory(QStringLiteral("qt.qpa.xcb")));
|
||||
|
||||
resetSettings();
|
||||
AppSettings& prefs = AppSettings::instance();
|
||||
const QString dir = QDir::homePath() + QStringLiteral("/journal-c");
|
||||
QFile::remove(dir + QStringLiteral("/operations.log"));
|
||||
prefs.setLogDirectory(dir);
|
||||
prefs.setLogMaxBytes(0);
|
||||
|
||||
LogManager::installQtMessageHandler();
|
||||
LogManager& journal = LogManager::instance();
|
||||
|
||||
// An event the application writes, then the warning Qt's Wayland plugin
|
||||
// repeats: the first must be in the journal, the second must not.
|
||||
journal.logEvent(QStringLiteral("ui"), QStringLiteral("mot de passe fourni"));
|
||||
qCWarning(QLoggingCategory("qt.qpa.wayland.textinput"))
|
||||
<< "Trying to disable 0x1 but 0x2 is focused";
|
||||
qCWarning(QLoggingCategory("pacmgr.update")) << "une alerte de l'application";
|
||||
|
||||
const QString content = journal.readFile();
|
||||
assert(content.contains(QStringLiteral("mot de passe fourni")));
|
||||
assert(content.contains(QStringLiteral("une alerte de l'application")));
|
||||
assert(!content.contains(QStringLiteral("Trying to disable")));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
@@ -299,6 +337,7 @@ int main(int argc, char* argv[]) {
|
||||
testUpdateBlockIsPersistedAndCleared();
|
||||
testAutomaticCheckCanBeTurnedOff();
|
||||
testJournalMovesAndRotates();
|
||||
testPlatformChatterStaysOutOfTheJournal();
|
||||
|
||||
std::cout << "app-settings: all tests passed" << std::endl;
|
||||
return 0;
|
||||
|
||||
Reference in new issue
Block a user