Files
BlackAngelTV f4aa661b54 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.
2026-10-09 19:22:19 +02:00

119 lines
5.1 KiB
C++

#pragma once
#include <QMutex>
#include <QObject>
#include <QString>
#include <cstdio>
// The application's single, always-on journal: everything the app does ends up
// in one plain-text file that the Logs screen displays and the user can grep
// like a terminal history.
//
// Two kinds of entry share the file:
//
// - a *command* records what a provider is about to run and then every raw
// line it prints, exactly what running the tool by hand would show:
//
// [2026-09-23 14:22:03] ── pacman · install ──────────────────────────
// [2026-09-23 14:22:03] $ install linux-headers
// [2026-09-23 14:22:04] :: Synchronizing package databases...
// [2026-09-23 14:22:05] :: Received timestamp for linux-headers ...
// ...
// [2026-09-23 14:22:10] ⟶ terminé
//
// - an *event* is a one-line note about something that is not a shelled
// command: a search, a plugin toggle, a network fetch, a refused request...
// Screens deliberately do not narrate navigation: a line per click buries
// the ones that matter.
//
// [2026-09-23 14:22:02] [recherche] "firefox" → 12 résultat(s)
// [2026-09-23 14:22:03] [pacman] $ sudo pacman -S --noconfirm firefox
// [2026-09-23 14:22:03] [flatpak] $ flatpak update --appstream
//
// 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. 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
// are the user's to choose on the Settings screen: changing either takes effect
// immediately, the existing journal being moved along (see AppSettings).
//
// All methods are safe to call from any thread.
class LogManager : public QObject {
Q_OBJECT
public:
// Process-wide journal, created on first use (thread-safe).
static LogManager& instance();
explicit LogManager(QObject* parent = nullptr);
// Records a one-line event. `category` is the short tag shown in brackets
// (e.g. "ui", "recherche", "plugin", "erreur").
void logEvent(const QString& category, const QString& message);
// Opens a command block: the provider(s) and action, plus a human-readable
// form of the command about to run (may be empty).
void beginCommand(const QString& provider, const QString& action,
const QString& command = QString());
// Appends one raw output line of the running command.
void appendOutput(const QString& line);
// Closes the current command block with its outcome. `error` carries the
// failure detail when the command failed.
void endCommand(bool success, const QString& error = QString());
// Full, plain text content of the journal (empty when nothing was written
// yet). `error` is filled when the file cannot be read at all.
QString readFile(QString* error = nullptr) const;
// True when the journal file already contains at least one entry.
bool hasEntries() const;
// Path of the journal as written on disk (useful for debugging).
QString filePath() const;
// Size the journal currently takes on disk, 0 when there is none yet. The
// Settings screen shows it next to the cap the user picked.
qint64 currentSizeBytes() const;
// Path of the rotated journal, kept so the user can still read what came
// before the last rotation.
static QString backupFilePath();
// Routes every Qt log message into the journal. Call once, after the
// 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();
private:
// Appends `text` verbatim, opening the file lazily. Caller does not hold the
// lock; this method takes it.
void write(const QString& text);
// Applies the current AppSettings: moves the journal to the configured
// directory and adopts the size cap. Called once from the constructor and
// again whenever a setting changes.
void applySettings();
QString m_filePath;
FILE* m_file = nullptr;
mutable QMutex m_mutex;
};