diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 4413c63c..71693c44 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -79,10 +79,10 @@ if(WIN32) ${FLUENTUI_DIRECTORY}/.cmake/version_exe.rc.in ${EXAMPLE_VERSION_RC_PATH} ) - configure_file( - ${FLUENTUI_DIRECTORY}/.cmake/InstallerScript.iss.in - ${FLUENTUI_DIRECTORY}/action-cli/InstallerScript.iss - ) +configure_file( + ${FLUENTUI_DIRECTORY}/.cmake/InstallerScript.iss.in + ${FLUENTUI_DIRECTORY}/action-cli/InstallerScript.iss +) endif() #添加可执行文件 @@ -155,6 +155,11 @@ set_target_properties(example PROPERTIES WIN32_EXECUTABLE TRUE ) +target_compile_definitions(example + PRIVATE + QT_MESSAGELOGCONTEXT +) + target_link_libraries(example PRIVATE Qt${QT_VERSION_MAJOR}::Quick Qt${QT_VERSION_MAJOR}::Svg diff --git a/example/src/helper/Log.cpp b/example/src/helper/Log.cpp index 24e48414..07f42dcb 100644 --- a/example/src/helper/Log.cpp +++ b/example/src/helper/Log.cpp @@ -2,9 +2,17 @@ #include #include #include +#include #include +#include #include #include +#include "Version.h" +#ifdef WIN32 +#include +#else +#include +#endif #ifndef QT_ENDL # if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) @@ -23,32 +31,69 @@ static std::unique_ptr g_logStream = nullptr; static inline void myMessageHandler(const QtMsgType type, const QMessageLogContext &context, const QString &message) { - if (message.isEmpty()) { - return; - } - const QString finalMessage = qFormatLogMessage(type, context, message).trimmed(); - if ((type == QtInfoMsg) || (type == QtDebugMsg)) { - std::cout << qPrintable(finalMessage) << std::endl; - } else { - std::cerr << qPrintable(finalMessage) << std::endl; - } - if (g_logError) { - return; - } - if (!g_logFile) { - g_logFile = std::make_unique(g_file_path); - if (!g_logFile->open(QFile::WriteOnly | QFile::Text | QFile::Append)) { - std::cerr << "Can't open file to write: " << qPrintable(g_logFile->errorString()) << std::endl; - g_logFile.reset(); - g_logError = true; + if (context.file && !message.isEmpty()) { + std::string strFileTmp = context.file; + const char* ptr = strrchr(strFileTmp.c_str(), '/'); + if (nullptr != ptr) { + char fn[512] = {0}; + sprintf(fn, "%s", ptr + 1); + strFileTmp = fn; + } + const char* ptrTmp = strrchr(strFileTmp.c_str(), '\\'); + if (nullptr != ptrTmp) { + char fn[512] = {0}; + sprintf(fn, "%s", ptrTmp + 1); + strFileTmp = fn; + } + QString levelName; + switch (type) { + case QtDebugMsg: + levelName = QStringLiteral("Debug"); + break; + case QtInfoMsg: + levelName = QStringLiteral("Info"); + break; + case QtWarningMsg: + levelName = QStringLiteral("Warning"); + break; + case QtCriticalMsg: + levelName = QStringLiteral("Critical"); + break; + case QtFatalMsg: + levelName = QStringLiteral("Fatal"); + break; + } + const QString finalMessage = QString::fromStdString("[%1] <%2> [ %3:%4 ] %5").arg( + QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss.zzz"), + levelName, + QString::fromStdString(strFileTmp), + QString::number(context.line), + message + ); + if ((type == QtInfoMsg) || (type == QtDebugMsg)) { + std::cout << qPrintable(finalMessage) << std::endl; + } else { + std::cerr << qPrintable(finalMessage) << std::endl; + } + if (g_logError) { return; } + if (!g_logFile) { + g_logFile = std::make_unique(g_file_path); + if (!g_logFile->open(QFile::WriteOnly | QFile::Text | QFile::Append)) { + std::cerr << "Can't open file to write: " << qPrintable(g_logFile->errorString()) << std::endl; + g_logFile.reset(); + g_logError = true; + return; + } + } + if (!g_logStream) { + g_logStream = std::make_unique(); + g_logStream->setDevice(g_logFile.get()); + } + (*g_logStream) << finalMessage << QT_ENDL; + g_logStream->flush(); } - if (!g_logStream) { - g_logStream = std::make_unique(); - g_logStream->setDevice(g_logFile.get()); - } - (*g_logStream) << finalMessage << QT_ENDL; } void Log::setup(const QString &app) @@ -63,17 +108,28 @@ void Log::setup(const QString &app) } once = true; g_app = app; - const QString logFileName = QString("debug-%1.log").arg(g_app); - const QString logDirPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation); + const QString logFileName = QString("%1-%2.log").arg(g_app,QString::number(QDateTime::currentMSecsSinceEpoch())); + const QString logDirPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)+"/log"; const QDir logDir(logDirPath); if(!logDir.exists()){ logDir.mkpath(logDirPath); } g_file_path = logDir.filePath(logFileName); - qSetMessagePattern(QString( - "[%{time yyyy/MM/dd hh:mm:ss.zzz}] <%{if-info}INFO%{endif}%{if-debug}DEBUG" - "%{endif}%{if-warning}WARNING%{endif}%{if-critical}CRITICAL%{endif}%{if-fatal}" - "FATAL%{endif}> %{if-category}%{category}: %{endif}%{message}")); qInstallMessageHandler(myMessageHandler); - qDebug()<<"Application log file path->"<"<init(argv); + Log::setup("example"); #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); @@ -33,11 +38,6 @@ int main(int argc, char *argv[]) QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); #endif #endif - qputenv("QT_QUICK_CONTROLS_STYLE","Basic"); - QGuiApplication::setOrganizationName("ZhuZiChu"); - QGuiApplication::setOrganizationDomain("https://zhuzichu520.github.io"); - QGuiApplication::setApplicationName("FluentUI"); - SettingsHelper::getInstance()->init(argv); if(SettingsHelper::getInstance()->getRender()=="software"){ #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) QQuickWindow::setGraphicsApi(QSGRendererInterface::Software); @@ -55,7 +55,6 @@ int main(int argc, char *argv[]) #ifdef FLUENTUI_BUILD_STATIC_LIB FluentUI::getInstance()->registerTypes(&engine); #endif - qDebug()<("example", 1, 0, "CircularReveal"); qmlRegisterType("example", 1, 0, "FileWatcher"); qmlRegisterType("example", 1, 0, "FpsItem");