4
0
mirror of https://github.com/QuasarApp/QuasarAppLib.git synced 2025-04-30 03:34:42 +00:00

Merge pull request from QuasarApp/fix_log_handler

Fix log handler
This commit is contained in:
Andrei Yankovich 2025-02-07 12:14:38 +01:00 committed by GitHub
commit 37a1f76ba1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 94 additions and 22 deletions

@ -7,18 +7,35 @@
#include "qalogger.h" #include "qalogger.h"
#include "params.h" #include "params.h"
#include <iostream>
#include <QCoreApplication>
#include <QFile> #include <QFile>
#include <QStandardPaths>
namespace QuasarAppUtils { namespace QuasarAppUtils {
static QFile* _logFile;
static bool _toFile = false;
static VerboseLvl _verboseLevel = Debug;
#define MESSAGE_PATTERN \ #define MESSAGE_PATTERN \
"[%{time MM-dd h:mm:ss.zzz} %{threadid} " \ "[%{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}] " \ "%{if-debug}Debug%{endif}%{if-info}Info%{endif}%{if-warning}Warning%{endif}%{if-critical}Error%{endif}%{if-fatal}Fatal%{endif}] " \
"%{message}" "%{message}"
QALogger::QALogger() {
_logFile = new QFile();
}
QALogger::~QALogger() {
_logFile->close();
delete _logFile;
}
bool checkLogType(QtMsgType type, VerboseLvl lvl) { bool checkLogType(QtMsgType type, VerboseLvl lvl) {
switch (type) { switch (type) {
case QtDebugMsg: return lvl >= Debug; case QtDebugMsg: return lvl >= Debug;
@ -32,36 +49,64 @@ bool checkLogType(QtMsgType type, VerboseLvl lvl) {
return true; 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 (checkLogType(type, _verboseLevel)) {
#if QT_VERSION > QT_VERSION_CHECK(5, 14, 0) if (_toFile && _logFile) {
stream << msg << Qt::endl; QTextStream stream(_logFile);
#else stream << qFormatLogMessage(type, context, msg) << Qt::endl;
stream << msg << endl; _logFile->flush();
#endif }
logFile.close();
} 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() { void QALogger::init() {
qSetMessagePattern(MESSAGE_PATTERN); qSetMessagePattern(MESSAGE_PATTERN);
qInstallMessageHandler(messageHandler); qInstallMessageHandler(messageHandler);
_verboseLevel = Params::getVerboseLvl();
if (Params::isEndable("fileLog")) {
_toFile = true;
QString path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/" + QCoreApplication::applicationName() + ".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";
}
}
} }
} }

@ -10,24 +10,51 @@
#include "quasarapp_global.h" #include "quasarapp_global.h"
#include <QFile>
#include <QList> #include <QList>
namespace QuasarAppUtils { namespace QuasarAppUtils {
/** /**
* @brief The QALogger class is logger handler for app. * @brief The QALogger class is logger handler for app.
* This class allow to log all message from app to file.
*
* Example:
* @code
* #include <qalogger.h>
*
* 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();
* @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 class QUASARAPPSHARED_EXPORT QALogger
{ {
public: public:
QALogger(); QALogger();
~QALogger();
/** /**
* @brief init This method initialize logging of all qt message into file. * @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(); void init();
}; };
} }
#endif // QALOGGER_H #endif // QALOGGER_H