diff --git a/qalogger.cpp b/qalogger.cpp index 59e14bd..b4e9bff 100644 --- a/qalogger.cpp +++ b/qalogger.cpp @@ -7,18 +7,33 @@ #include "qalogger.h" #include "params.h" +#include #include 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,36 +47,64 @@ 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 (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(); - } + 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")) { + _toFile = true; + QString path = Params::getCurrentExecutable() + ".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"; + } + + } + } } diff --git a/qalogger.h b/qalogger.h index 95abfc4..199e59f 100644 --- a/qalogger.h +++ b/qalogger.h @@ -10,24 +10,26 @@ #include "quasarapp_global.h" +#include #include 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