Files
FWS-PacManage/include/log-manager.h
T
BlackAngelTV 8670c02eb3 feat(logs): record every operation and event in an application journal
The app kept no durable trace of what it ran: LogManager existed but was
never instantiated, so only a handful of console-only qCInfo calls survived
a session. Wire it up as a process-wide journal that captures each command's
raw output next to UI, search, plugin and update events, and funnel every Qt
log message into it, so a failure can still be diagnosed after the fact.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-09-27 18:18:55 +02:00

90 lines
3.4 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: navigation, a search, a plugin toggle, a network fetch, a refused
// request...
//
// [2026-09-23 14:22:02] [ui] navigation → recherche
// [2026-09-23 14:22:03] [recherche] "firefox" → 12 résultat(s)
//
// 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.
//
// The file lives in the user's own data tree (XDG_CONFIG_HOME / home) so it is
// never lost when a sandbox is rebuilt. 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;
// Routes every Qt log message into the journal. Call once, after the
// QApplication exists. Safe to call more than once.
static void installQtMessageHandler();
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);
QString m_filePath;
FILE* m_file = nullptr;
mutable QMutex m_mutex;
};