From 1abbc4593aa9e6a3f035c908b2346c5695537cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=AD=90=E6=A5=9A=5Czhuzi?= Date: Thu, 7 Dec 2023 00:20:32 +0800 Subject: [PATCH] add log level --- example/src/helper/Log.cpp | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/example/src/helper/Log.cpp b/example/src/helper/Log.cpp index 07f42dcb..ba98deec 100644 --- a/example/src/helper/Log.cpp +++ b/example/src/helper/Log.cpp @@ -29,8 +29,22 @@ static bool g_logError = false; static std::unique_ptr g_logFile = nullptr; static std::unique_ptr g_logStream = nullptr; +static int g_logLevel = 4; + +std::map logLevelMap = { + {QtFatalMsg,0}, + {QtCriticalMsg,1}, + {QtWarningMsg,2}, + {QtInfoMsg,3}, + {QtDebugMsg,4} +}; + + static inline void myMessageHandler(const QtMsgType type, const QMessageLogContext &context, const QString &message) { + if(logLevelMap[type]>g_logLevel){ + return; + } if (context.file && !message.isEmpty()) { std::string strFileTmp = context.file; const char* ptr = strrchr(strFileTmp.c_str(), '/'); @@ -116,20 +130,20 @@ void Log::setup(const QString &app) } g_file_path = logDir.filePath(logFileName); qInstallMessageHandler(myMessageHandler); - qDebug()<