From 5025a68891fc7ffd72300327de7a1a43b6ca80b6 Mon Sep 17 00:00:00 2001 From: Filip Bucek Date: Wed, 20 Nov 2019 17:57:57 +0100 Subject: [PATCH 1/2] WIP: better logger output --- src/serversdk/logger.cpp | 36 ++++++++++++++++++++++++++---------- src/serversdk/logger.h | 3 +++ src/testapp/main.cpp | 4 +--- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/serversdk/logger.cpp b/src/serversdk/logger.cpp index bd6481f..b4c0b3e 100644 --- a/src/serversdk/logger.cpp +++ b/src/serversdk/logger.cpp @@ -3,10 +3,12 @@ #include #include +#include Q_LOGGING_CATEGORY(sdkcore, "sdk.core"); Q_LOGGING_CATEGORY(sdknetwork, "sdk.network"); Q_LOGGING_CATEGORY(sdkutils, "sdk.utils"); +Q_LOGGING_CATEGORY(sdkutilsdb, "sdk.utils.db"); Q_LOGGING_CATEGORY(sdkdb, "sdk.db"); namespace serversdk { @@ -43,6 +45,11 @@ QList Logger::getNewMessages(int currentCount) return list; } +QString Logger::toColor(const QString &color, const QString &text) +{ + return color + text + "\033[0m"; +} + void Logger::outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg) { @@ -50,23 +57,29 @@ void Logger::outputMessage(QtMsgType type, const QMessageLogContext &context, co QString text; QString textType; + QString colorTextType; switch (type) { case QtDebugMsg: - textType = QStringLiteral("debug"); + textType = QStringLiteral("DEBUG"); + colorTextType = Logger::toColor("\033[0;31m", textType); break; case QtWarningMsg: - textType = QStringLiteral("warning"); + textType = QStringLiteral("WARN"); + colorTextType = Logger::toColor("\033[0;33m", textType); break; case QtCriticalMsg: - textType = QStringLiteral("critical"); + textType = QStringLiteral("CRITICAL"); + colorTextType = Logger::toColor("\033[0;31m", textType); break; case QtFatalMsg: - textType = QStringLiteral("fatal"); + textType = QStringLiteral("FATAL"); + colorTextType = Logger::toColor("\033[0;31m", textType); break; #if QT_VERSION >= 0x050500 case QtInfoMsg: - textType = QStringLiteral("info"); + textType = QStringLiteral("INFO"); + colorTextType = Logger::toColor("\033[0;34m", textType); break; #endif } @@ -78,14 +91,17 @@ void Logger::outputMessage(QtMsgType type, const QMessageLogContext &context, co // Output message //text += QString("%1 %2:%3 %4").arg(context.category).arg(shortFile).arg(context.line).arg(msg); // New output syntax - text = QStringLiteral("[%1] %2 [ %3:%4 | %5 | %6 ]") - .arg(textType, msg, shortFile) - .arg(context.line) - .arg(context.function, context.category); + + QDateTime now = QDateTime::currentDateTime(); + + text = QStringLiteral("[%1 %2 %3] %4") // [ %3:%4 | %5 | %6 ]") + .arg(now.toString("dd-MM-yyyy hh:mm:ss.z")) + .arg(colorTextType) + .arg(context.category) + .arg(msg); QTextStream out(stderr); out << text << "\n"; //.toLocal8Bit() + "\n"; - //out.flush(); // INVSDK-37 ORK-102 crash on windows when flush() -> possible threading problem if (type == QtFatalMsg) abort(); diff --git a/src/serversdk/logger.h b/src/serversdk/logger.h index b515529..a14d727 100644 --- a/src/serversdk/logger.h +++ b/src/serversdk/logger.h @@ -11,6 +11,7 @@ Q_DECLARE_LOGGING_CATEGORY(sdkcore) Q_DECLARE_LOGGING_CATEGORY(sdknetwork) Q_DECLARE_LOGGING_CATEGORY(sdkutils) +Q_DECLARE_LOGGING_CATEGORY(sdkutilsdb) Q_DECLARE_LOGGING_CATEGORY(sdkdb) namespace serversdk { @@ -94,6 +95,8 @@ public: virtual void pushMessage(const LoggerMessage& logmsg); QList getNewMessages(int currentCount); + static QString toColor(const QString &color, const QString &text); + signals: void messageReceived(const serversdk::LoggerMessage &message); diff --git a/src/testapp/main.cpp b/src/testapp/main.cpp index 306bce6..724e10d 100644 --- a/src/testapp/main.cpp +++ b/src/testapp/main.cpp @@ -3,15 +3,13 @@ #include #include - int main(int argc, char *argv[]) { serversdk::initAppLogger(); - qCDebug(sdkcore) << "test message"; + qCDebug(sdkcore) << "Application starting"; QCoreApplication app(argc, argv); QCoreApplication::setApplicationName("example-app"); QCoreApplication::setApplicationVersion("0.1.0"); - } From 38a7da300dc3681864fd4b69f4fab3c46b07c9c4 Mon Sep 17 00:00:00 2001 From: Filip Bucek Date: Mon, 25 Nov 2019 08:46:59 +0100 Subject: [PATCH 2/2] disabled color output for Windows --- src/serversdk/logger.cpp | 16 ++++++++++++++-- src/serversdk/logger.h | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/serversdk/logger.cpp b/src/serversdk/logger.cpp index b4c0b3e..b79cb41 100644 --- a/src/serversdk/logger.cpp +++ b/src/serversdk/logger.cpp @@ -14,7 +14,11 @@ Q_LOGGING_CATEGORY(sdkdb, "sdk.db"); namespace serversdk { Logger* Logger::sConsumer = nullptr; -//bool Logger::sShowFunction = false; +#ifdef Q_OS_UNIX + bool Logger::mColorOutput = true; +#else + bool Logger::mColorOutput = false; +#endif void Logger::setConsumer(Logger *consumer) @@ -25,6 +29,7 @@ void Logger::setConsumer(Logger *consumer) Logger::Logger(QObject *parent) : QObject(parent) { qRegisterMetaType(); + } QList Logger::getNewMessages(int currentCount) @@ -50,6 +55,11 @@ QString Logger::toColor(const QString &color, const QString &text) return color + text + "\033[0m"; } +void Logger::setColorOutput(bool colorOutput) +{ + mColorOutput = colorOutput; +} + void Logger::outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg) { @@ -94,9 +104,11 @@ void Logger::outputMessage(QtMsgType type, const QMessageLogContext &context, co QDateTime now = QDateTime::currentDateTime(); + const QString textTypeUni = mColorOutput ? colorTextType : textType; + text = QStringLiteral("[%1 %2 %3] %4") // [ %3:%4 | %5 | %6 ]") .arg(now.toString("dd-MM-yyyy hh:mm:ss.z")) - .arg(colorTextType) + .arg(textTypeUni) .arg(context.category) .arg(msg); diff --git a/src/serversdk/logger.h b/src/serversdk/logger.h index a14d727..dea05ac 100644 --- a/src/serversdk/logger.h +++ b/src/serversdk/logger.h @@ -97,6 +97,8 @@ public: static QString toColor(const QString &color, const QString &text); + /** I will enable color output for terminal ( linux only ) */ + void setColorOutput(bool colorOutput); signals: void messageReceived(const serversdk::LoggerMessage &message); @@ -104,6 +106,7 @@ signals: private: QList mLogMessages; //!< Test QMutex mMutex; + static bool mColorOutput; };