feat: optimize log output system with dual-mode support
Some checks failed
MacOS / Build (push) Has been cancelled
Ubuntu / Build (push) Has been cancelled
Windows / Build (push) Has been cancelled

- Add detailed log mode with timestamp and location info
- Add log level prefixes for better readability
- Integrate CMake option for build-time log mode control
- Ensure consistent output between terminal and log window

Log: Enhanced logging system provides two modes: detailed mode for
development with full context information, and simple mode for
production with minimal overhead. Both modes maintain consistent
output across terminal and application log window.
This commit is contained in:
zhanghongyuan 2025-07-31 14:52:24 +08:00 committed by Barry
commit 5a9536e7a0
2 changed files with 62 additions and 4 deletions

View file

@ -52,6 +52,14 @@ if(NOT CMAKE_BUILD_TYPE)
endif()
message(STATUS "[${PROJECT_NAME}] BUILD_TYPE:${CMAKE_BUILD_TYPE}")
# Log configuration
option(ENABLE_DETAILED_LOGS "Enable detailed log output with file and line info" OFF)
if(ENABLE_DETAILED_LOGS)
message(STATUS "[${PROJECT_NAME}] Detailed logs enabled")
else()
message(STATUS "[${PROJECT_NAME}] Simple logs enabled")
endif()
# Compiler set
message(STATUS "[${PROJECT_NAME}] C++ compiler ID is: ${CMAKE_CXX_COMPILER_ID}")
if (MSVC)
@ -268,6 +276,11 @@ endif()
add_executable(${PROJECT_NAME} ${QC_RUNTIME_TYPE} ${QC_PROJECT_SOURCES})
# Log compile definitions
if(ENABLE_DETAILED_LOGS)
target_compile_definitions(${PROJECT_NAME} PRIVATE ENABLE_DETAILED_LOGS)
endif()
#
# Internal include path (todo: remove this, use absolute path include)
#

View file

@ -5,6 +5,7 @@
#include <QTcpServer>
#include <QTcpSocket>
#include <QTranslator>
#include <QDateTime>
#include "config.h"
#include "dialog.h"
@ -194,10 +195,54 @@ QtMsgType covertLogLevel(const QString &logLevel)
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
if (g_oldMessageHandler) {
g_oldMessageHandler(type, context, msg);
QString outputMsg;
#ifdef ENABLE_DETAILED_LOGS
QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
if (context.file && context.line > 0) {
QString fileName = QString::fromUtf8(context.file);
int lastSlash = fileName.lastIndexOf('/');
if (lastSlash >= 0) {
fileName = fileName.mid(lastSlash + 1);
}
lastSlash = fileName.lastIndexOf('\\');
if (lastSlash >= 0) {
fileName = fileName.mid(lastSlash + 1);
}
outputMsg = QString("[ %1 %2: %3 ] %4").arg(timestamp).arg(fileName).arg(context.line).arg(msg);
} else {
outputMsg = QString("[%1] %2").arg(timestamp).arg(msg);
}
switch (type) {
case QtDebugMsg:
outputMsg.prepend("[debug] ");
break;
case QtInfoMsg:
outputMsg.prepend("[info] ");
break;
case QtWarningMsg:
outputMsg.prepend("[warring] ");
break;
case QtCriticalMsg:
outputMsg.prepend("[critical] ");
break;
case QtFatalMsg:
outputMsg.prepend("[fatal] ");
break;
}
fprintf(stderr, "%s\n", outputMsg.toUtf8().constData());
#else
outputMsg = msg;
if (g_oldMessageHandler) {
g_oldMessageHandler(type, context, outputMsg);
}
#endif
// Is Qt log level higher than warning?
float fLogLevel = g_msgType;
if (QtInfoMsg == g_msgType) {
@ -209,8 +254,8 @@ void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QS
}
if (fLogLevel <= fLogLevel2) {
if (g_mainDlg && g_mainDlg->isVisible() && !g_mainDlg->filterLog(msg)) {
g_mainDlg->outLog(msg);
if (g_mainDlg && g_mainDlg->isVisible() && !g_mainDlg->filterLog(outputMsg)) {
g_mainDlg->outLog(outputMsg);
}
}