diff --git a/CMakeLists.txt b/CMakeLists.txt index 92d9161..1509c51 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ set(SOURCES pages/browse-page.cpp pages/cleanup-page.cpp pages/history-page.cpp + src/log-manager.cpp pages/logs-page.cpp pages/plugins-page.cpp resources/resources.qrc @@ -63,6 +64,7 @@ set(HEADERS include/progress-tracker.h include/plugin-shop.h include/app-core.h + include/log-manager.h pages/page.h pages/ui-kit.h pages/dashboard-page.h diff --git a/include/log-manager.h b/include/log-manager.h new file mode 100644 index 0000000..b026c0a --- /dev/null +++ b/include/log-manager.h @@ -0,0 +1,71 @@ +#pragma once + +#include +#include + +// 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; +}; diff --git a/src/log-manager.cpp b/src/log-manager.cpp new file mode 100644 index 0000000..90bd45a --- /dev/null +++ b/src/log-manager.cpp @@ -0,0 +1,130 @@ +#include "log-manager.h" + +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace { + +// Where a plain text history of what the app did lives on any desktop +// session, inside the user's own data tree (so a Flatpak-sandboxed build +// still finds it in ~/.local/share). +QString logDirectory() { + const QString dir = QStandardPaths::writableLocation( + QStandardPaths::AppConfigLocation); + if (!dir.isEmpty()) { + QDir(dir).mkpath("."); + return dir; + } + return QDir::homePath() + QStringLiteral("/.config/PacManager"); +} + +} // namespace + +LogManager::LogManager(QObject* parent) : QObject(parent) { + m_filePath = logDirectory() + QStringLiteral("/") + QStringLiteral("operations.log"); +} + +QString LogManager::filePath() const { + return m_filePath; +} + +bool LogManager::openFile(QString* error) { + // Append mode: a long session keeps growing, and the file is never + // truncated while the app runs. + m_file = std::fopen(QFile::encodeName(m_filePath).constData(), "a"); + if (!m_file) { + if (error) { + *error = QString::fromLocal8Bit( + strerror(errno)) + QStringLiteral(" : ") + + QFile::decodeName(QFile::encodeName(m_filePath)); + } + return false; + } + if (error) error->clear(); + return true; +} + +void LogManager::closeFile() { + if (m_file) { + std::fclose(m_file); + m_file = nullptr; + } +} + +void LogManager::beginOperation(const QString& manager, const QString& action, + const QString& command) { + closeFile(); + if (!openFile()) return; + + const QString stamp = QDateTime::currentDateTime().toString( + QStringLiteral("yyyy-MM-dd hh:mm:ss")); + const QString line = QStringLiteral("[%1] %2 %3 %4\n") + .arg(stamp) + .arg(manager) + .arg(action) + .arg(command); + std::fputs(QFile::encodeName(line).constData(), m_file); +} + +void LogManager::appendLine(const QString& line) { + if (!m_file) return; + const QString out = QStringLiteral(" %1\n").arg(line); + std::fputs(QFile::encodeName(out).constData(), m_file); +} + +void LogManager::endOperation(bool success, const QString& error) { + const QString stamp = QDateTime::currentDateTime().toString( + QStringLiteral("yyyy-MM-dd hh:mm:ss")); + const QString outcome = + success ? QStringLiteral(" [terminé]") : QStringLiteral(" [erreur]:"); + QString out = QStringLiteral("[%1] %2%3\n") + .arg(stamp) + .arg(QString()) + .arg(outcome); + if (!error.isEmpty()) { + out += error; + out += '\n'; + } + std::fputs(QFile::encodeName(out).constData(), m_file); + closeFile(); + emit fileUpdated(); +} + +void LogManager::appendSectionHeader(QString& text, int width) { + text += QStringLiteral("\n%1\n").arg(QStringLiteral("=").repeated(width)); +} + +QString LogManager::readFile(QString* error) const { + if (!QFile::exists(m_filePath)) return QString(); + QFile file(m_filePath); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + if (error) *error = QStringLiteral("Impossible d'ouvrir %1.") + .arg(m_filePath); + return QString(); + } + if (error) error->clear(); + // Read the file in chunks to avoid loading a very large log into memory + // at once; the file grows with session usage. + const int bufferSize = 8192; + QByteArray chunk; + chunk.resize(bufferSize); + QString result; + while (!file.atEnd()) { + const qint64 read = file.read(chunk.data(), bufferSize); + if (read > 0) { + result += QString::fromUtf8(chunk, static_cast(read)); + } + } + return result; +} + +bool LogManager::hasEntries() const { + return !readFile().isEmpty(); +} diff --git a/src/pacman-manager.cpp b/src/pacman-manager.cpp index d338457..f4dadf8 100644 --- a/src/pacman-manager.cpp +++ b/src/pacman-manager.cpp @@ -1,3 +1,4 @@ +#include "log-manager.h" #include "pacman-manager.h" #include "progress-tracker.h"