ixes of the loger and settings classes
All checks were successful
buildbot/DocsGenerator Build finished.
buildbot/LinuxCMakeBuilderQt6 Build finished.

This commit is contained in:
Andrei Yankovich 2025-02-18 14:12:54 +01:00
parent 9c6c6d257c
commit 7e84073393
3 changed files with 29 additions and 19 deletions

View File

@ -16,7 +16,8 @@
namespace QuasarAppUtils { namespace QuasarAppUtils {
static QFile* _logFile; Q_GLOBAL_STATIC(QString, _logFile)
static bool _toFile = false; static bool _toFile = false;
static VerboseLvl _verboseLevel = Debug; static VerboseLvl _verboseLevel = Debug;
@ -28,12 +29,9 @@ static VerboseLvl _verboseLevel = Debug;
QALogger::QALogger() { QALogger::QALogger() {
_logFile = new QFile();
} }
QALogger::~QALogger() { QALogger::~QALogger() {
_logFile->close();
delete _logFile;
} }
@ -55,18 +53,17 @@ void messageHandler(QtMsgType type, const QMessageLogContext & context, const QS
if (checkLogType(type, _verboseLevel)) { if (checkLogType(type, _verboseLevel)) {
if (_toFile && _logFile) { if (_toFile && _logFile->size()) {
QTextStream stream(_logFile); QFile logFile(*_logFile);
stream << qFormatLogMessage(type, context, msg) << Qt::endl; if (logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
_logFile->flush(); QTextStream stream(&logFile);
stream << qFormatLogMessage(type, context, msg) << Qt::endl;
}
} }
switch (type) { switch (type) {
case QtMsgType::QtFatalMsg: { case QtMsgType::QtFatalMsg:
Q_ASSERT_X(false, __FUNCTION__ , qFormatLogMessage(type, context, msg).toLatin1().data());
break;
}
case QtMsgType::QtCriticalMsg: case QtMsgType::QtCriticalMsg:
case QtMsgType::QtWarningMsg: { case QtMsgType::QtWarningMsg: {
std::cerr << qFormatLogMessage(type, context, msg).toStdString() << std::endl; std::cerr << qFormatLogMessage(type, context, msg).toStdString() << std::endl;
@ -95,7 +92,8 @@ void QALogger::init() {
if (Params::isEndable("fileLog")) { if (Params::isEndable("fileLog")) {
_toFile = true; _toFile = true;
QString path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation); 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"); auto file = Params::getArg("fileLog");
if (file.size()) { if (file.size()) {
filePath = file; filePath = file;
@ -103,11 +101,7 @@ void QALogger::init() {
QDir().mkpath(path); QDir().mkpath(path);
_logFile->setFileName(filePath); *_logFile = filePath;
if (!_logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
qFatal() << "Can't open log file";
}
} }

View File

@ -69,4 +69,13 @@ bool Settings::initService() {
return ISettings::initService(std::make_unique<Settings>()); return ISettings::initService(std::make_unique<Settings>());
} }
ISettings *Settings::autoInstance() {
if (auto result = instance()) {
return result;
}
Settings::initService();
return ISettings::instance();
}
} }

View File

@ -68,6 +68,13 @@ public:
*/ */
static bool initService(); static bool initService();
/**
* @brief deinitService This method destroy default object of the QuasarAppUtils::Settings type.
* @see ISettings::deinitService
*/
static ISettings *autoInstance();
protected: protected:
void syncImplementation() override; void syncImplementation() override;