This commit is contained in:
朱子楚\zhuzi 2023-12-04 21:18:19 +08:00
parent ee23554cb4
commit 5d4cfa5286
5 changed files with 130 additions and 2 deletions

View File

@ -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 <QtCore/qdebug.h>
#include <QtCore/qfile.h>
#include <QtCore/qtextstream.h>
#include <iostream>
#include <framelesshelpercore_global.h>
#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<QFile> g_logFile = nullptr;
static std::unique_ptr<QTextStream> 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<QFile>();
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<QTextStream>();
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);
}

32
example/src/helper/Log.h Normal file
View File

@ -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 <QtCore/qstring.h>
namespace Log
{
void setup(const QString &app);
} // namespace Log

View File

@ -9,6 +9,7 @@
#include <QtQml/qqmlextensionplugin.h>
#include <QLoggingCategory>
#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);

@ -1 +1 @@
Subproject commit 49c72fb4f85421f770a6a14657ba73a9912ee833
Subproject commit 27fcd913b4b21b51d5cca307e47e93d1ae75e1bb

View File

@ -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);