diff --git a/src/cli/src/main.cpp b/src/cli/src/main.cpp index 5c8becf03..cdec290a9 100644 --- a/src/cli/src/main.cpp +++ b/src/cli/src/main.cpp @@ -3,9 +3,10 @@ #include #include #include +#include "cli.h" #include "functions.h" +#include "logger.h" #include "models/profile.h" -#include "cli.h" int main(int argc, char *argv[]) @@ -17,6 +18,8 @@ int main(int argc, char *argv[]) app.setOrganizationDomain("bionus.fr.cr"); QSettings::setDefaultFormat(QSettings::IniFormat); + Logger::getInstance().initialize(); + auto *profile = new Profile(savePath()); profile->purgeTemp(24 * 60 * 60); diff --git a/src/e2e/src/main.cpp b/src/e2e/src/main.cpp index 3916b62ce..15852a539 100644 --- a/src/e2e/src/main.cpp +++ b/src/e2e/src/main.cpp @@ -51,6 +51,9 @@ int main(int argc, char *argv[]) { const QCoreApplication app(argc, argv); + Logger::getInstance().initialize(); + Logger::getInstance().setLogLevel(Logger::Warning); + QCommandLineParser parser; parser.addHelpOption(); @@ -60,8 +63,6 @@ int main(int argc, char *argv[]) parser.addOption(outputOption); parser.process(app); - Logger::getInstance().setLogLevel(Logger::Warning); - QFile f(parser.value(inputOption)); if (!f.open(QFile::ReadOnly | QFile::Text)) { return 1; diff --git a/src/gui-qml/src/main.cpp b/src/gui-qml/src/main.cpp index ec511b14a..e56af44c4 100644 --- a/src/gui-qml/src/main.cpp +++ b/src/gui-qml/src/main.cpp @@ -10,6 +10,7 @@ #include "loaders/gallery-search-loader.h" #include "loaders/image-loader.h" #include "loaders/tag-search-loader.h" +#include "logger.h" #include "main-screen.h" #include "models/image.h" #include "models/profile.h" @@ -50,6 +51,8 @@ int main(int argc, char *argv[]) app.setOrganizationName("Bionus"); app.setOrganizationDomain("bionus.fr.cr"); + Logger::getInstance().initialize(); + qmlRegisterType("StatusBar", 0, 1, "StatusBar"); qmlRegisterType("Grabber", 1, 0, "SyntaxHighlighterHelper"); qmlRegisterType("Grabber", 1, 0, "GallerySearchLoader"); diff --git a/src/gui/src/main/main.cpp b/src/gui/src/main/main.cpp index 8a70113d1..3035f3095 100644 --- a/src/gui/src/main/main.cpp +++ b/src/gui/src/main/main.cpp @@ -38,6 +38,7 @@ #include "analytics.h" #include "cli.h" #include "functions.h" +#include "logger.h" #include "main-window.h" #include "models/page-api.h" #include "models/profile.h" @@ -74,6 +75,8 @@ int main(int argc, char *argv[]) app.setOrganizationDomain("bionus.fr.cr"); QSettings::setDefaultFormat(QSettings::IniFormat); + Logger::getInstance().initialize(); + // Handler for custom URL protocols, redirecting to the main program through HTTP calls if (argc == 3 && QString(argv[1]) == "--url-protocol") { QNetworkAccessManager manager; diff --git a/src/lib/src/logger.cpp b/src/lib/src/logger.cpp index dd98f6561..1cb62fe40 100644 --- a/src/lib/src/logger.cpp +++ b/src/lib/src/logger.cpp @@ -1,5 +1,6 @@ #include "logger.h" #include +#include #include #include #include @@ -9,6 +10,31 @@ #include #endif + +void Logger::initialize() +{ + // Create the "logs/" directory if it doesn't exist + const QDir logsDirectory = savePath(QStringLiteral("logs/"), false, true); + if (!logsDirectory.exists()) { + logsDirectory.mkpath("."); + } + + // Clean-up old log files if we have too many + const QFileInfoList list = logsDirectory.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoSymLinks, QDir::Time | QDir::Reversed); + if (list.count() > MAX_LOG_FILES) { + for (int i = 0; i < (list.count() - MAX_LOG_FILES); i++) { + QFile::remove(list[i].absoluteFilePath()); + } + } + + // Generate a new file for each run + const QString logFilename = QString("main_%1-%2.log") + .arg(QDate::currentDate().toString("yyyy.MM.dd")) + .arg(QTime::currentTime().toString("hh.mm.ss.zzz")); + setLogFile(logsDirectory.filePath(logFilename)); +} + + void Logger::logToConsole() { if (m_logFile.isOpen()) { @@ -94,6 +120,7 @@ void Logger::log(const QString &l, LogLevel level) return; } + // Fallback to "main.log" if no log file is open if (!m_logFile.isOpen()) { setLogFile(savePath(QStringLiteral("main.log"), false, true)); } diff --git a/src/lib/src/logger.h b/src/lib/src/logger.h index 8aaeef76c..f4bc14b03 100644 --- a/src/lib/src/logger.h +++ b/src/lib/src/logger.h @@ -7,6 +7,8 @@ #include #include +#define MAX_LOG_FILES 20 + class QString; @@ -37,6 +39,7 @@ class Logger : public QObject static void noMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &message); static void setupMessageOutput(bool log); + void initialize(); void setExitOnError(bool val); void setLogFile(const QString &path); void setLogLevel(LogLevel level);