mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-04-26 17:54:40 +00:00
fix logger
This commit is contained in:
parent
580a4ce1bd
commit
945da5be9d
83
qalogger.cpp
83
qalogger.cpp
@ -7,18 +7,33 @@
|
||||
|
||||
#include "qalogger.h"
|
||||
#include "params.h"
|
||||
#include <iostream>
|
||||
|
||||
#include <QFile>
|
||||
|
||||
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,35 +47,63 @@ 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 (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")) {
|
||||
auto verboselvl = Params::getVerboseLvl();
|
||||
if (checkLogType(type, verboselvl)) {
|
||||
_toFile = true;
|
||||
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)) {
|
||||
_logFile->setFileName(path);
|
||||
|
||||
QTextStream stream(&logFile);
|
||||
#if QT_VERSION > QT_VERSION_CHECK(5, 14, 0)
|
||||
stream << msg << Qt::endl;
|
||||
#else
|
||||
stream << msg << endl;
|
||||
#endif
|
||||
logFile.close();
|
||||
if (!_logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
|
||||
qFatal() << "Can't open log file";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QALogger::init() {
|
||||
qSetMessagePattern(MESSAGE_PATTERN);
|
||||
qInstallMessageHandler(messageHandler);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -10,24 +10,26 @@
|
||||
|
||||
#include "quasarapp_global.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QList>
|
||||
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user