From 945da5be9db46f95ba0df1c6a6dccc91bf059320 Mon Sep 17 00:00:00 2001 From: EndrII Date: Fri, 7 Feb 2025 11:56:30 +0100 Subject: [PATCH 1/4] fix logger --- qalogger.cpp | 85 +++++++++++++++++++++++++++++++++++++++------------- qalogger.h | 4 ++- 2 files changed, 67 insertions(+), 22 deletions(-) diff --git a/qalogger.cpp b/qalogger.cpp index 59e14bd..b4e9bff 100644 --- a/qalogger.cpp +++ b/qalogger.cpp @@ -7,18 +7,33 @@ #include "qalogger.h" #include "params.h" +#include #include namespace QuasarAppUtils { +static QFile* _logFile; +static bool _toFile = false; +static VerboseLvl _verboseLevel = Debug; + #define MESSAGE_PATTERN \ - "[%{time MM-dd h:mm:ss.zzz} %{threadid} " \ - "%{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] " \ +"[%{time MM-dd h:mm:ss.zzz} %{threadid} " \ + "%{if-debug}Debug%{endif}%{if-info}Info%{endif}%{if-warning}Warning%{endif}%{if-critical}Error%{endif}%{if-fatal}Fatal%{endif}] " \ "%{message}" +QALogger::QALogger() { + _logFile = new QFile(); +} + +QALogger::~QALogger() { + _logFile->close(); + delete _logFile; +} + + bool checkLogType(QtMsgType type, VerboseLvl lvl) { switch (type) { case QtDebugMsg: return lvl >= Debug; @@ -32,36 +47,64 @@ bool checkLogType(QtMsgType type, VerboseLvl lvl) { return true; } -void messageHandler(QtMsgType type, const QMessageLogContext &, const QString &msg) { +void messageHandler(QtMsgType type, const QMessageLogContext & context, const QString &msg) { - if (Params::isEndable("fileLog")) { - auto verboselvl = Params::getVerboseLvl(); - if (checkLogType(type, verboselvl)) { - QString path = Params::getCurrentExecutable() + ".log"; - auto file = Params::getArg("fileLog"); - if (file.size()) { - path = file; - } - QFile logFile(path); - if (logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { - QTextStream stream(&logFile); -#if QT_VERSION > QT_VERSION_CHECK(5, 14, 0) - stream << msg << Qt::endl; -#else - stream << msg << endl; -#endif - logFile.close(); - } + if (checkLogType(type, _verboseLevel)) { + if (_toFile && _logFile) { + QTextStream stream(_logFile); + stream << qFormatLogMessage(type, context, msg) << Qt::endl; + _logFile->flush(); + } + + switch (type) { + case QtMsgType::QtFatalMsg: { + Q_ASSERT_X(false, __FUNCTION__ , qFormatLogMessage(type, context, msg).toLatin1().data()); + break; + } + + case QtMsgType::QtCriticalMsg: + case QtMsgType::QtWarningMsg: { + std::cerr << qFormatLogMessage(type, context, msg).toStdString() << std::endl; + break; + } + case QtMsgType::QtDebugMsg: + case QtMsgType::QtInfoMsg: + default: { + std::cout << qFormatLogMessage(type, context, msg).toStdString() << std::endl; + + break; + } + } } } + void QALogger::init() { qSetMessagePattern(MESSAGE_PATTERN); qInstallMessageHandler(messageHandler); + + _verboseLevel = Params::getVerboseLvl(); + + if (Params::isEndable("fileLog")) { + _toFile = true; + QString path = Params::getCurrentExecutable() + ".log"; + auto file = Params::getArg("fileLog"); + if (file.size()) { + path = file; + } + + _logFile->setFileName(path); + + if (!_logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { + qFatal() << "Can't open log file"; + } + + } + } } diff --git a/qalogger.h b/qalogger.h index 95abfc4..199e59f 100644 --- a/qalogger.h +++ b/qalogger.h @@ -10,24 +10,26 @@ #include "quasarapp_global.h" +#include #include namespace QuasarAppUtils { /** * @brief The QALogger class is logger handler for app. + * This class allow to log all message from app to file. */ class QUASARAPPSHARED_EXPORT QALogger { public: QALogger(); + ~QALogger(); /** * @brief init This method initialize logging of all qt message into file. */ void init(); - }; } #endif // QALOGGER_H From 6b8bb9f2cd6619c1776789b57c05ff00a1471d35 Mon Sep 17 00:00:00 2001 From: EndrII Date: Fri, 7 Feb 2025 12:00:00 +0100 Subject: [PATCH 2/4] update docs --- qalogger.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/qalogger.h b/qalogger.h index 199e59f..727d348 100644 --- a/qalogger.h +++ b/qalogger.h @@ -18,6 +18,17 @@ namespace QuasarAppUtils { /** * @brief The QALogger class is logger handler for app. * This class allow to log all message from app to file. + * + * Example: + * @code + * #include + * + * QuasarAppUtils::QALogger logger; + * + * QuasarAppUtils::Params::parseParams(argc, argv); + * + * logger.init(); + * @endcode */ class QUASARAPPSHARED_EXPORT QALogger { @@ -27,6 +38,8 @@ public: /** * @brief init This method initialize logging of all qt message into file. + * @note This function should be invokae after parsing arguments. + * if you invoke this before parsing arguments, verbose level of logs will not created correct. */ void init(); From 86c9036f13d9571087f4555a47b6affd25660ab2 Mon Sep 17 00:00:00 2001 From: EndrII Date: Fri, 7 Feb 2025 12:11:26 +0100 Subject: [PATCH 3/4] fix standart path location --- qalogger.cpp | 4 +++- qalogger.h | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/qalogger.cpp b/qalogger.cpp index b4e9bff..02f9dd6 100644 --- a/qalogger.cpp +++ b/qalogger.cpp @@ -9,7 +9,9 @@ #include "params.h" #include +#include #include +#include namespace QuasarAppUtils { @@ -91,7 +93,7 @@ void QALogger::init() { if (Params::isEndable("fileLog")) { _toFile = true; - QString path = Params::getCurrentExecutable() + ".log"; + QString path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/" + QCoreApplication::applicationName() + ".log"; auto file = Params::getArg("fileLog"); if (file.size()) { path = file; diff --git a/qalogger.h b/qalogger.h index 727d348..dcd636c 100644 --- a/qalogger.h +++ b/qalogger.h @@ -25,6 +25,8 @@ namespace QuasarAppUtils { * * QuasarAppUtils::QALogger logger; * + * QuasarAppUtils::Params::setEnable("fileLog", true); //force enable file writing using efault file location - located in localApplication data. + * * QuasarAppUtils::Params::parseParams(argc, argv); * * logger.init(); From 961e6608d92dcbcd13744284f56a30faa243036a Mon Sep 17 00:00:00 2001 From: EndrII Date: Fri, 7 Feb 2025 12:13:03 +0100 Subject: [PATCH 4/4] update docs --- qalogger.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/qalogger.h b/qalogger.h index dcd636c..b78f868 100644 --- a/qalogger.h +++ b/qalogger.h @@ -31,6 +31,16 @@ namespace QuasarAppUtils { * * logger.init(); * @endcode + * + * + * Standart file log locations per platforms : + * - Windows: %APPDATA%/OrganisationName/YourAppName/YourAppName.log + * - Linux: $HOME/.config/OrganisationName/YourAppName/YourAppName.log + * - Mac: $HOME/Library/Application Support/OrganisationName/YourAppName/YourAppName.log + * - Android: /data/data/com.organisationName.yourAppName/files/YourAppName.log + * - iOS: /var/mobile/Applications/Data/YourAppName/YourAppName.log + * + * you can overiwite this location by setting "fileLog" option in Params. */ class QUASARAPPSHARED_EXPORT QALogger {