Files
FWS-PacManage/include/log-manager.h
T

72 lines
3.0 KiB
C++

#pragma once
#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:
//
// [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é]
//
// 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.
class LogManager : public QObject {
Q_OBJECT
public:
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.
QString readFile(QString* error = nullptr) const;
// True when the log file already contains at least one entry.
bool hasEntries() const;
// Path of the log file as written on disk (useful for debugging).
QString filePath() const;
signals:
// The log file was rewritten (e.g. after an operation finished) and a new
// 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);
QString m_filePath;
mutable QString m_lastError;
FILE* m_file = nullptr;
};