From 7e84073393c2ce09879f19ba4c685adf031e07d1 Mon Sep 17 00:00:00 2001 From: EndrII Date: Tue, 18 Feb 2025 14:12:54 +0100 Subject: [PATCH] ixes of the loger and settings classes --- qalogger.cpp | 32 +++++++++++++------------------- settings.cpp | 9 +++++++++ settings.h | 7 +++++++ 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/qalogger.cpp b/qalogger.cpp index fa4538b..2c29eaf 100644 --- a/qalogger.cpp +++ b/qalogger.cpp @@ -16,7 +16,8 @@ namespace QuasarAppUtils { -static QFile* _logFile; +Q_GLOBAL_STATIC(QString, _logFile) + static bool _toFile = false; static VerboseLvl _verboseLevel = Debug; @@ -28,12 +29,9 @@ static VerboseLvl _verboseLevel = Debug; QALogger::QALogger() { - _logFile = new QFile(); } QALogger::~QALogger() { - _logFile->close(); - delete _logFile; } @@ -55,18 +53,17 @@ void messageHandler(QtMsgType type, const QMessageLogContext & context, const QS if (checkLogType(type, _verboseLevel)) { - if (_toFile && _logFile) { - QTextStream stream(_logFile); - stream << qFormatLogMessage(type, context, msg) << Qt::endl; - _logFile->flush(); + if (_toFile && _logFile->size()) { + QFile logFile(*_logFile); + if (logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { + QTextStream stream(&logFile); + stream << qFormatLogMessage(type, context, msg) << Qt::endl; + } + } switch (type) { - case QtMsgType::QtFatalMsg: { - Q_ASSERT_X(false, __FUNCTION__ , qFormatLogMessage(type, context, msg).toLatin1().data()); - break; - } - + case QtMsgType::QtFatalMsg: case QtMsgType::QtCriticalMsg: case QtMsgType::QtWarningMsg: { std::cerr << qFormatLogMessage(type, context, msg).toStdString() << std::endl; @@ -95,7 +92,8 @@ void QALogger::init() { if (Params::isEndable("fileLog")) { _toFile = true; QString path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation); - QString filePath = path + "/" + QCoreApplication::applicationName() + ".log"; + QString filePath = path + "/" + QCoreApplication::applicationName() + + " " + QDate::currentDate().toString(Qt::DateFormat::ISODate) + ".log"; auto file = Params::getArg("fileLog"); if (file.size()) { filePath = file; @@ -103,11 +101,7 @@ void QALogger::init() { QDir().mkpath(path); - _logFile->setFileName(filePath); - - if (!_logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { - qFatal() << "Can't open log file"; - } + *_logFile = filePath; } diff --git a/settings.cpp b/settings.cpp index 0e52fb8..b60be5b 100644 --- a/settings.cpp +++ b/settings.cpp @@ -69,4 +69,13 @@ bool Settings::initService() { return ISettings::initService(std::make_unique()); } +ISettings *Settings::autoInstance() { + if (auto result = instance()) { + return result; + } + + Settings::initService(); + return ISettings::instance(); +} + } diff --git a/settings.h b/settings.h index 7c29de6..8382d4b 100644 --- a/settings.h +++ b/settings.h @@ -68,6 +68,13 @@ public: */ static bool initService(); + /** + * @brief deinitService This method destroy default object of the QuasarAppUtils::Settings type. + * @see ISettings::deinitService + */ + static ISettings *autoInstance(); + + protected: void syncImplementation() override;