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>
This commit is contained in:
7 files changed
+335
-130
No files matched your search
@@ -121,6 +121,9 @@ public:
|
||||
// Full content of /var/log/pacman.log; `error` is filled when it cannot be
|
||||
// read (file missing, no permission).
|
||||
QString readPackageLog(QString* error = nullptr) const;
|
||||
// Full content of the application's own journal (every operation and event,
|
||||
// including the raw output of the commands that ran).
|
||||
QString readAppLog(QString* error = nullptr) const;
|
||||
// Extra pacman information for the details panel (-Qi / -Si), cached per
|
||||
// package so selecting rows repeatedly does not re-spawn pacman.
|
||||
QString packageDetails(const PackageInfo& package);
|
||||
|
||||
+63
-45
@@ -1,71 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include <QMutex>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
// Writes a complete, human readable (tty style) log of every package
|
||||
// operation: each command that runs, the raw output it produced, the outcome
|
||||
// when it finishes, and the time it happened. Everything is appended to one
|
||||
// plain text file so the Logs screen can display it and the user can grep it
|
||||
// like any command history:
|
||||
#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.
|
||||
//
|
||||
// [2026-09-23 14:22:03] pacman install linux-headers
|
||||
// [2026-09-23 14:22:03] ──────────────────────────────────────────────
|
||||
// [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] ==> Aucun paquet à installer, vérification...
|
||||
// [2026-09-23 14:22:10] Installation de linux-headers... [terminé]
|
||||
// Two kinds of entry share the file:
|
||||
//
|
||||
// The file lives next to the application in a directory that exists on every
|
||||
// desktop session (XDG_CONFIG_HOME or the user home), so it is never lost when
|
||||
// the sandbox is rebuilt.
|
||||
// - 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);
|
||||
|
||||
// Full, plain text content of the log file (empty when nothing was
|
||||
// written yet). `error` is filled when the file cannot be read at all.
|
||||
// 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 log file already contains at least one entry.
|
||||
// True when the journal file already contains at least one entry.
|
||||
bool hasEntries() const;
|
||||
|
||||
// Path of the log file as written on disk (useful for debugging).
|
||||
// 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 log file was rewritten (e.g. after an operation finished) and a new
|
||||
// copy of its content is available.
|
||||
// The journal changed on disk and a fresh copy of its content is available.
|
||||
void fileUpdated();
|
||||
|
||||
private:
|
||||
// Starts a new operation in the log. `manager`, `action` and `command` are
|
||||
// the fields the user cares about; the rest is filled in by the caller.
|
||||
void beginOperation(const QString& manager, const QString& action,
|
||||
const QString& command);
|
||||
|
||||
// Appends one line of output from the running command to the log.
|
||||
void appendLine(const QString& line);
|
||||
|
||||
// Closes the current operation with the final outcome. `success` tells
|
||||
// whether the command exited normally, `error` carries the last output
|
||||
// lines when it failed (so a clean failure is still a full log).
|
||||
void endOperation(bool success, const QString& error = QString());
|
||||
|
||||
// Opens (or creates) the log file for appending and returns true on
|
||||
// success. `error` is filled with the reason when opening failed.
|
||||
bool openFile(QString* error = nullptr);
|
||||
|
||||
// Closes the file if it was left open by the previous operation.
|
||||
void closeFile();
|
||||
|
||||
// Appends a header line the way a terminal would: one line of "=" long
|
||||
// enough to sit between the section title and the content.
|
||||
static void appendSectionHeader(QString& text, int width = 46);
|
||||
// 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;
|
||||
mutable QString m_lastError;
|
||||
FILE* m_file = nullptr;
|
||||
mutable QMutex m_mutex;
|
||||
};
|
||||
Reference in new issue
Block a user