From ee0f7fb2eebf6af9a522fb3c0eb51c6db33f49e6 Mon Sep 17 00:00:00 2001 From: EndrII Date: Mon, 24 Jun 2024 13:23:40 +0200 Subject: [PATCH] move to qt logger system --- CMakeLists.txt | 5 +- helpdata.cpp | 1 - locales.cpp | 17 ++---- optiondata.cpp | 1 - params.cpp | 148 +++++++----------------------------------------- params.h | 6 +- qaglobalutils.h | 18 +----- qalogger.cpp | 67 ++++++++++++++++++++++ qalogger.h | 36 ++++++++++++ qaservice.cpp | 1 - quasarapp.cpp | 1 - 11 files changed, 137 insertions(+), 164 deletions(-) create mode 100644 qalogger.cpp create mode 100644 qalogger.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a259ba6..0fd65f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,12 +63,11 @@ file(GLOB SOURCE_CPP "*.cpp" "*.h" ) -add_library(${PROJECT_NAME} ${SOURCE_CPP} - qasecretservice.h qasecretservice.cpp) +add_library(${PROJECT_NAME} ${SOURCE_CPP}) target_link_libraries(${PROJECT_NAME} PUBLIC Qt${QT_VERSION_MAJOR}::Core) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -setVersion(1 5 3) +setVersion(1 5 4) initAll() make_directory("${CMAKE_CURRENT_SOURCE_DIR}/Distro") diff --git a/helpdata.cpp b/helpdata.cpp index ff38aeb..88e02e3 100644 --- a/helpdata.cpp +++ b/helpdata.cpp @@ -12,7 +12,6 @@ #include #else #include -#include #include #endif diff --git a/locales.cpp b/locales.cpp index 20b38d8..24a0fc2 100644 --- a/locales.cpp +++ b/locales.cpp @@ -15,7 +15,6 @@ #include #include #include -#include "params.h" using namespace QuasarAppUtils; @@ -31,17 +30,15 @@ bool QuasarAppUtils::Locales::findQmPrivate(const QString &prefix, auto qmFile = new QTranslator(); if(!qmFile->load(file.absoluteFilePath())) { - QuasarAppUtils::Params::log("Failed to load translation file : " - + file.absoluteFilePath(), - QuasarAppUtils::Warning); + qWarning() << "Failed to load translation file : " + + file.absoluteFilePath(); delete qmFile; continue; } if (qmFile->isEmpty()) { - QuasarAppUtils::Params::log("Translation file is Empty: " + - file.absoluteFilePath(), - QuasarAppUtils::Debug); + qDebug() << "Translation file is Empty: " + + file.absoluteFilePath(); delete qmFile; continue; } @@ -51,7 +48,7 @@ bool QuasarAppUtils::Locales::findQmPrivate(const QString &prefix, auto message = QString("The target language (%0) and a choosed qm file (%1) " "is different, Loading will be skiped: "). arg(language, file.absoluteFilePath()); - QuasarAppUtils::Params::log(message, QuasarAppUtils::Debug); + qDebug() << message; delete qmFile; continue; @@ -85,9 +82,7 @@ void QuasarAppUtils::Locales::installTranslations( QList &qmFiles for (const auto & translator: std::as_const(qmFiles)) { if (!QCoreApplication::installTranslator(translator)) { - QuasarAppUtils::Params::log("Failed to install translation file : " + translator->filePath(), - QuasarAppUtils::Warning); - + qWarning() << "Failed to install translation file : " + translator->filePath(); delete translator; // we use a link of qmFiles so remove all invalid translations. qmFiles.removeAll(translator); diff --git a/optiondata.cpp b/optiondata.cpp index a2aad45..80dc92d 100644 --- a/optiondata.cpp +++ b/optiondata.cpp @@ -7,7 +7,6 @@ #include "optiondata.h" -#include "qaglobalutils.h" namespace QuasarAppUtils{ diff --git a/params.cpp b/params.cpp index 24f1dd6..62c4922 100644 --- a/params.cpp +++ b/params.cpp @@ -9,16 +9,14 @@ #include #include #include -#include #include #include -#include "qaglobalutils.h" +#include #ifdef Q_OS_WIN #include "windows.h" #else #include -#include #endif using namespace QuasarAppUtils; @@ -35,66 +33,27 @@ bool Params::isEndable(const QString& key) { void Params::log(const QString &log, VerboseLvl vLvl) { - writeLoginFile(log, vLvl); - auto lvl = getVerboseLvl(); if (vLvl <= lvl) { switch (vLvl) { case VerboseLvl::Error: -#ifdef Q_OS_WIN32 - std::cerr << QString{lvlToString(vLvl) + ": " + log}.toStdString() << std::endl; -#else - qCritical().noquote() << lvlToString(vLvl) + ": " + log; -#endif - -#ifdef QA_ASSERT_ON_ERROR -#ifdef __GNUC__ - __builtin_trap(); -#else - debug_assert(false, "You requested to throw assert in every error message." - " See The ASSERT_ON_ERROR option in cmake config."); -#endif - -#endif + qCritical().noquote() << log; break; case VerboseLvl::Warning: { -#ifdef Q_OS_WIN32 - std::cerr << QString{lvlToString(vLvl) + ": " + log}.toStdString() << std::endl; -#else - qWarning().noquote() << lvlToString(vLvl) + ": " + log; -#endif - - -#ifdef QA_ASSERT_ON_WARN -#ifdef __GNUC__ - __builtin_trap(); -#else - debug_assert(false, "You requested to throw assert in every warning message." - " See The ASSERT_ON_ERROR option in cmake config."); -#endif - -#endif + qWarning().noquote() << log; break; } case VerboseLvl::Debug: { -#ifdef Q_OS_WIN32 - std::cout << QString{lvlToString(vLvl) + ": " + log}.toStdString() << std::endl; -#else - qDebug().noquote() << lvlToString(vLvl) + ": " + log; -#endif + qDebug().noquote() << log; break; } case VerboseLvl::Info: default: { -#ifdef Q_OS_WIN32 - std::cout << QString{lvlToString(vLvl) + ": " + log}.toStdString() << std::endl; -#else - qInfo().noquote() << lvlToString(vLvl) + ": " + log; -#endif + qInfo().noquote() << log; break; } @@ -191,65 +150,6 @@ int Params::size() { return params.size(); } -QString Params::timeString() { - return QDateTime::currentDateTime().toString(); -} - -QString Params::lvlToString(VerboseLvl vLvl) { - switch (vLvl) { - - case VerboseLvl::Error: { - return "Error"; - } - - case VerboseLvl::Warning: { - return "Warning"; - } - - case VerboseLvl::Info: { - return "Info"; - } - - case VerboseLvl::Debug: { - return "Verbose log"; - } - default: return ""; - } - - return ""; -} - -bool Params::writeLoginFile(const QString &log, VerboseLvl vLvl) { - if (isEndable("fileLog")) { - - auto lvl = getVerboseLvl(); - if (vLvl <= lvl) { - QString path = getCurrentExecutable() + ".log"; - auto file = 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 << timeString() <<"| " << lvlToString(vLvl) + ": " + log << Qt::endl; -#else - stream << timeString() <<"| " << lvlToString(vLvl) + ": " + log << endl; -#endif - logFile.close(); - } else { - return false; - } - } - } - - return true; -} - bool Params::optionsForEach(const QStringList ¶msArray, const OptionsDataList& availableOptions) { @@ -274,8 +174,7 @@ bool Params::optionsForEach(const QStringList ¶msArray, params[paramsArray[i].mid(1)] = paramsArray[i + 1]; i++; } else { - QuasarAppUtils::Params::log("Missing argument for " + paramsArray[i], - QuasarAppUtils::Error); + qCritical() << "Missing argument for " + paramsArray[i]; return false; } } else { @@ -322,8 +221,7 @@ bool Params::parseParams(const QStringList ¶msArray, const OptionsDataList & memset(path, 0, sizeof path); if (readlink("/proc/self/exe", path, 2048) < 0) { - QuasarAppUtils::Params::log("parseParams can't get self path!", - QuasarAppUtils::Error); + qCritical() << "parseParams can't get self path!"; return false; } appPath = QFileInfo(path).absolutePath(); @@ -349,8 +247,7 @@ bool Params::parseParams(const QStringList ¶msArray, const OptionsDataList & } void Params::printWorkingOptions() { - QuasarAppUtils::Params::log("--- Working options table start ---", - QuasarAppUtils::Debug); + qDebug() << "--- Working options table start ---"; QMap::const_iterator iter = params.constBegin(); while (iter != params.constEnd()) { @@ -362,23 +259,24 @@ void Params::printWorkingOptions() { row += QString{": %1"}.arg(value); } - QuasarAppUtils::Params::log(row, QuasarAppUtils::Debug); + qDebug() << row; ++iter; } - QuasarAppUtils::Params::log("--- Working options table end ---", - QuasarAppUtils::Debug); + qDebug() << "--- Working options table end ---"; } bool Params::checkOption(const OptionData& optionData, const QString& rawOptionName) { #ifndef QA_ALLOW_NOT_SUPPORTED_OPTIONS if (!optionData.isValid()) { - QuasarAppUtils::Params::log(QString("The '%0' option not exists!" - " You use wrong option name, please check the help before run your commnad.").arg( - rawOptionName), - QuasarAppUtils::Error); + + qCritical() << QString("The '%0' option not exists!" + " You use wrong option name," + " please check the help before run your commnad."). + arg(rawOptionName); + return false; } #else @@ -390,19 +288,17 @@ bool Params::checkOption(const OptionData& optionData, const QString& rawOptionN if (optionData.isRemoved()) { - QuasarAppUtils::Params::log(optionData.depricatedMsg(), - QuasarAppUtils::Error); + qCritical() << optionData.depricatedMsg(); + return false; } - QuasarAppUtils::Params::log(QString("The %0 option(s) marked as deprecated! " - "And most likely will be removed in next release."). - arg(optionData.names().join("/")), - QuasarAppUtils::Warning); + qWarning() << QString("The %0 option(s) marked as deprecated! " + "And most likely will be removed in next release."). + arg(optionData.names().join("/")); + qWarning() << QString("Option message: %0").arg(optionData.depricatedMsg()); - QuasarAppUtils::Params::log(QString("Option message: %0").arg(optionData.depricatedMsg()), - QuasarAppUtils::Warning); } return true; diff --git a/params.h b/params.h index 351ba49..6291ee3 100644 --- a/params.h +++ b/params.h @@ -131,6 +131,7 @@ public: * @note All messages will be printed according to the current verbose setting. * @note The verbose level sets by verbose option on console. */ + [[deprecated("Use QALogger and qt debug functions(qDebug, qInfo, qError...)")]] static void log(const QString& log, VerboseLvl vLvl = VerboseLvl::Debug); /** @@ -149,6 +150,7 @@ public: * @brief isDebugBuild This method return true if the library buildet in debug mode. * @return true if this library buildet in debug mode. */ + [[deprecated("Use Qt MACROSSSES")]] static bool isDebugBuild(); /** @@ -217,10 +219,6 @@ public: static OptionsDataList availableArguments(); private: - static QString timeString(); - static QString lvlToString(VerboseLvl vLvl); - static bool writeLoginFile(const QString& log, VerboseLvl vLvl = VerboseLvl::Debug); - static bool optionsForEach(const QStringList& paramsArray, const OptionsDataList &availableOptions); diff --git a/qaglobalutils.h b/qaglobalutils.h index ebd03b8..eee8675 100644 --- a/qaglobalutils.h +++ b/qaglobalutils.h @@ -8,14 +8,11 @@ #ifndef QU_GLOBAL_UTILS_H #define QU_GLOBAL_UTILS_H -#include #include -#include #include #include #include "QtGlobal" -#include "params.h" - +#include "quasarapp_global.h" template constexpr inline T operator | (T lhs, T rhs) @@ -150,18 +147,7 @@ uint8_t static_type_hash_8(T& object) noexcept { #define H_32 static_type_hash_32 -#ifndef QT_DEBUG - // The debug_assert it is assert that abort application only in debug mode. - // In the release mode This assert prin Error message only. - #define debug_assert(condition, msg) \ - if (!condition) \ - QuasarAppUtils::Params::log(msg, QuasarAppUtils::Error); - -#else - // The debug_assert it is assert that abort application only in debug mode. - // In the release mode This assert prin Error message only. - #define debug_assert(condition, msg) assert(condition && msg) -#endif +#define debug_assert(C, M) Q_ASSERT(C && M) /** * @brief randomArray This function return random arrat with size @a size diff --git a/qalogger.cpp b/qalogger.cpp new file mode 100644 index 0000000..3d88310 --- /dev/null +++ b/qalogger.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2024-2024 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + +#include "qalogger.h" +#include "params.h" + +#include + +namespace QuasarAppUtils { + + +#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}] " \ + "%{message}" + + +bool checkLogType(QtMsgType type, VerboseLvl lvl) { + switch (type) { + case QtDebugMsg: return lvl >= Debug; + case QtInfoMsg: return lvl >= Info; + case QtWarningMsg: return lvl >= Warning; + + case QtCriticalMsg: + case QtFatalMsg: + return true; + } + + return true; +} +void messageHandler(QtMsgType type, const QMessageLogContext &, 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(); + } + } + } +} + +void QALogger::init() { + qSetMessagePattern(MESSAGE_PATTERN); + qInstallMessageHandler(messageHandler); + +} + +} diff --git a/qalogger.h b/qalogger.h new file mode 100644 index 0000000..70afdbd --- /dev/null +++ b/qalogger.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2024-2024 QuasarApp. + * Distributed under the lgplv3 software license, see the accompanying + * Everyone is permitted to copy and distribute verbatim copies + * of this license document, but changing it is not allowed. +*/ + +#ifndef QALOGGER_H +#define QALOGGER_H + +#include "quasarapp_global.h" + +#include + +namespace QuasarAppUtils { + +struct QUASARAPPSHARED_EXPORT LogData { + char type = 0; + int time = 0; + QString text; +}; + +/** + * @brief The QALogger class is logger handler for app. + */ +class QUASARAPPSHARED_EXPORT QALogger +{ +public: + QALogger(); + + void init(); + + +}; +} +#endif // QALOGGER_H diff --git a/qaservice.cpp b/qaservice.cpp index 9a4da68..f8d18b5 100644 --- a/qaservice.cpp +++ b/qaservice.cpp @@ -5,4 +5,3 @@ * of this license document, but changing it is not allowed. */ -#include "qaservice.h" diff --git a/quasarapp.cpp b/quasarapp.cpp index d1dfa3d..efc6eb9 100644 --- a/quasarapp.cpp +++ b/quasarapp.cpp @@ -5,7 +5,6 @@ * of this license document, but changing it is not allowed. */ -#include "quasarapp.h"