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

fix logger

This commit is contained in:
Andrei Yankovich 2025-02-07 11:56:30 +01:00
parent 580a4ce1bd
commit 945da5be9d
2 changed files with 67 additions and 22 deletions

@ -7,18 +7,33 @@
#include "qalogger.h" #include "qalogger.h"
#include "params.h" #include "params.h"
#include <iostream>
#include <QFile> #include <QFile>
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,35 +47,63 @@ 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 (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")) { if (Params::isEndable("fileLog")) {
auto verboselvl = Params::getVerboseLvl(); _toFile = true;
if (checkLogType(type, verboselvl)) {
QString path = Params::getCurrentExecutable() + ".log"; QString path = Params::getCurrentExecutable() + ".log";
auto file = Params::getArg("fileLog"); auto file = Params::getArg("fileLog");
if (file.size()) { if (file.size()) {
path = file; path = file;
} }
QFile logFile(path); _logFile->setFileName(path);
if (logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
QTextStream stream(&logFile); if (!_logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
#if QT_VERSION > QT_VERSION_CHECK(5, 14, 0) qFatal() << "Can't open log file";
stream << msg << Qt::endl;
#else
stream << msg << endl;
#endif
logFile.close();
} }
}
}
}
void QALogger::init() { }
qSetMessagePattern(MESSAGE_PATTERN);
qInstallMessageHandler(messageHandler);
} }

@ -10,24 +10,26 @@
#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.
*/ */
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.
*/ */
void init(); void init();
}; };
} }
#endif // QALOGGER_H #endif // QALOGGER_H