From 5d4cfa52864c0d3181b4bc002a96866add9c6484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=AD=90=E6=A5=9A=5Czhuzi?= Date: Mon, 4 Dec 2023 21:18:19 +0800 Subject: [PATCH] fix bug --- example/src/helper/Log.cpp | 94 ++++++++++++++++++++++++++++++++++++++ example/src/helper/Log.h | 32 +++++++++++++ example/src/main.cpp | 2 + framelesshelper | 2 +- src/FluApp.cpp | 2 +- 5 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 example/src/helper/Log.cpp create mode 100644 example/src/helper/Log.h diff --git a/example/src/helper/Log.cpp b/example/src/helper/Log.cpp new file mode 100644 index 00000000..fd4f396d --- /dev/null +++ b/example/src/helper/Log.cpp @@ -0,0 +1,94 @@ +/* + * MIT License + * + * Copyright (C) 2021-2023 by wangwenx190 (Yuhang Zhao) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "log.h" +#include +#include +#include +#include +#include + +#ifndef QT_ENDL +# if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) +# define QT_ENDL Qt::endl +# else +# define QT_ENDL endl +# endif +#endif + +static QString g_app = {}; +static bool g_logError = false; + +static std::unique_ptr g_logFile = nullptr; +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 << qUtf8Printable(finalMessage) << std::endl; + } else { + std::cerr << qUtf8Printable(finalMessage) << std::endl; + } + if (g_logError) { + return; + } + if (!g_logFile) { + g_logFile = std::make_unique(); + g_logFile->setFileName(FRAMELESSHELPER_STRING_LITERAL("debug-%1.log").arg(g_app)); + if (!g_logFile->open(QFile::WriteOnly | QFile::Text | QFile::Append)) { + std::cerr << "Can't open file to write: " << qUtf8Printable(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; +} + +void Log::setup(const QString &app) +{ + Q_ASSERT(!app.isEmpty()); + if (app.isEmpty()) { + return; + } + static bool once = false; + if (once) { + return; + } + once = true; + g_app = app; + qSetMessagePattern(FRAMELESSHELPER_STRING_LITERAL( + "[%{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); +} diff --git a/example/src/helper/Log.h b/example/src/helper/Log.h new file mode 100644 index 00000000..f8730ede --- /dev/null +++ b/example/src/helper/Log.h @@ -0,0 +1,32 @@ +/* + * MIT License + * + * Copyright (C) 2021-2023 by wangwenx190 (Yuhang Zhao) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#pragma once + +#include + +namespace Log +{ + void setup(const QString &app); +} // namespace Log diff --git a/example/src/main.cpp b/example/src/main.cpp index 8cf77c63..b6c964c5 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -9,6 +9,7 @@ #include #include #include "AppInfo.h" +#include "helper/Log.h" #include "src/component/CircularReveal.h" #include "src/component/FileWatcher.h" #include "src/component/FpsItem.h" @@ -23,6 +24,7 @@ Q_IMPORT_QML_PLUGIN(FluentUIPlugin) int main(int argc, char *argv[]) { + Log::setup("example"); QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy); #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); diff --git a/framelesshelper b/framelesshelper index 49c72fb4..27fcd913 160000 --- a/framelesshelper +++ b/framelesshelper @@ -1 +1 @@ -Subproject commit 49c72fb4f85421f770a6a14657ba73a9912ee833 +Subproject commit 27fcd913b4b21b51d5cca307e47e93d1ae75e1bb diff --git a/src/FluApp.cpp b/src/FluApp.cpp index 74c85b09..9137ff5e 100644 --- a/src/FluApp.cpp +++ b/src/FluApp.cpp @@ -24,7 +24,7 @@ FluApp::~FluApp(){ void FluApp::init(QObject *application){ this->_application = application; - FramelessHelper::Quick::initialize(); + FramelessHelperQuickInitialize(); FramelessConfig::instance()->set(Global::Option::DisableLazyInitializationForMicaMaterial); FramelessConfig::instance()->set(Global::Option::CenterWindowBeforeShow); QQmlEngine *engine = qmlEngine(_application);