diff --git a/QtScrcpy/CMakeLists.txt b/QtScrcpy/CMakeLists.txt index 736dd23..2a151e7 100755 --- a/QtScrcpy/CMakeLists.txt +++ b/QtScrcpy/CMakeLists.txt @@ -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) # diff --git a/QtScrcpy/main.cpp b/QtScrcpy/main.cpp index ed34be8..4b84e0a 100644 --- a/QtScrcpy/main.cpp +++ b/QtScrcpy/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #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); } }