Merge pull request 'refactor(logs): optimisation' (#14) from refactor/logs-optimization into main
Reviewed-on: #14
This commit was merged in pull request #14.
This commit is contained in:
commit
656e365daf
4 files changed
+204
No files matched your search
@@ -0,0 +1,130 @@
|
||||
#include "log-manager.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QStandardPaths>
|
||||
#include <QStringBuilder>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
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<int>(read));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool LogManager::hasEntries() const {
|
||||
return !readFile().isEmpty();
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "log-manager.h"
|
||||
#include "pacman-manager.h"
|
||||
|
||||
#include "progress-tracker.h"
|
||||
|
||||
Reference in new issue
Block a user