mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-04-26 17:54:40 +00:00
Merge pull request #43 from QuasarApp/qt_logger
move to qt logger system
This commit is contained in:
commit
a21c8d38ed
@ -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")
|
||||
|
@ -12,7 +12,6 @@
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
|
17
locales.cpp
17
locales.cpp
@ -15,7 +15,6 @@
|
||||
#include <QRegularExpression>
|
||||
#include <QLocale>
|
||||
#include <QMap>
|
||||
#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<QTranslator *> &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);
|
||||
|
@ -7,7 +7,6 @@
|
||||
|
||||
|
||||
#include "optiondata.h"
|
||||
#include "qaglobalutils.h"
|
||||
|
||||
namespace QuasarAppUtils{
|
||||
|
||||
|
148
params.cpp
148
params.cpp
@ -9,16 +9,14 @@
|
||||
#include <QVariantMap>
|
||||
#include <QDebug>
|
||||
#include <QFileInfo>
|
||||
#include <iostream>
|
||||
#include <QDateTime>
|
||||
#include <QCoreApplication>
|
||||
#include "qaglobalutils.h"
|
||||
#include <QtLogging>
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include "windows.h"
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#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<QString, QString>::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;
|
||||
|
6
params.h
6
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);
|
||||
|
@ -8,14 +8,11 @@
|
||||
#ifndef QU_GLOBAL_UTILS_H
|
||||
#define QU_GLOBAL_UTILS_H
|
||||
|
||||
#include <cstdlib>
|
||||
#include <type_traits>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
#include <QByteArray>
|
||||
#include "QtGlobal"
|
||||
#include "params.h"
|
||||
|
||||
#include "quasarapp_global.h"
|
||||
|
||||
template <typename T>
|
||||
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
|
||||
|
67
qalogger.cpp
Normal file
67
qalogger.cpp
Normal file
@ -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 <QFile>
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
33
qalogger.h
Normal file
33
qalogger.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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 <QList>
|
||||
|
||||
namespace QuasarAppUtils {
|
||||
|
||||
/**
|
||||
* @brief The QALogger class is logger handler for app.
|
||||
*/
|
||||
class QUASARAPPSHARED_EXPORT QALogger
|
||||
{
|
||||
public:
|
||||
QALogger();
|
||||
|
||||
/**
|
||||
* @brief init This method initialize logging of all qt message into file.
|
||||
*/
|
||||
void init();
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
#endif // QALOGGER_H
|
@ -5,4 +5,3 @@
|
||||
* of this license document, but changing it is not allowed.
|
||||
*/
|
||||
|
||||
#include "qaservice.h"
|
||||
|
@ -5,7 +5,6 @@
|
||||
* of this license document, but changing it is not allowed.
|
||||
*/
|
||||
|
||||
#include "quasarapp.h"
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user