From 5c44ee722614075e6813844f41ea9ed285d748f8 Mon Sep 17 00:00:00 2001 From: momen Date: Mon, 22 Sep 2025 15:48:40 +0800 Subject: [PATCH] =?UTF-8?q?chore:=E6=B7=BB=E5=8A=A0=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.cpp | 13 ++++- src/pages/settingspage.cpp | 35 ++++++++++++- src/pages/settingspage.h | 3 ++ src/utils/utils.cpp | 104 +++++++++++++++++++++++++++++++++++++ src/utils/utils.h | 7 +++ 5 files changed, 160 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 776a3e7..fe3bc33 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,7 +108,10 @@ int main(int argc, char *argv[]) { // 崩溃处理 signal(SIGSEGV, crashHandler); // 注册SIGSEGV处理函数 - + + // 初始化日志系统 + Utils::initLogger(); + Utils::writeLog("INFO", "Application starting..."); // // Get build time // static const QDate buildDate = QLocale(QLocale::English).toDate(QString(__DATE__).replace(" ", " 0"), "MMM dd yyyy"); @@ -117,6 +120,7 @@ int main(int argc, char *argv[]) //在cmakelist.txt中设置 buildDateTime QString buildDateTime = QString("%1-%2").arg(QString(BUILD_DATE)).arg(QString(BUILD_TIME)); + Utils::writeLog("INFO", QString("Build datetime: %1").arg(buildDateTime)); // NOTE: 提前设置组织名称和应用名称,避免配置文件位置错误 DApplication::setOrganizationName("spark-union"); @@ -130,9 +134,11 @@ int main(int argc, char *argv[]) DataCollectorAndUploader uploader; QObject::connect(&uploader, &DataCollectorAndUploader::uploadSuccessful, [](){ qDebug() << "Data uploaded successfully"; + Utils::writeLog("INFO", "Data uploaded successfully"); }); QObject::connect(&uploader, &DataCollectorAndUploader::uploadFailed, [](QString error){ qDebug() << "Upload failed with error: " << error; + Utils::writeLog("ERROR", QString("Upload failed with error: %1").arg(error)); }); uploader.collectAndUploadData(); @@ -202,5 +208,10 @@ int main(int argc, char *argv[]) } w.show(); + // 在程序结束前关闭日志文件 - 修复变量名并移到return前 + QObject::connect(&a, &QApplication::aboutToQuit, []() { + Utils::writeLog("INFO", "Application shutting down"); + }); + return a.exec(); } \ No newline at end of file diff --git a/src/pages/settingspage.cpp b/src/pages/settingspage.cpp index 467cab5..df5e37d 100644 --- a/src/pages/settingspage.cpp +++ b/src/pages/settingspage.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #define TMP_PATH "/tmp/spark-store" #define DEFAULT_SERVER_URL "https://cdn-d.spark-app.store/" @@ -22,6 +23,9 @@ SettingsPage::SettingsPage(QWidget *parent) configCanSave = false; initConfig(); + + // 连接导出日志按钮的点击信号 + connect(ui->pushButton_exportLog, &QPushButton::clicked, this, &SettingsPage::on_pushButton_exportLog_clicked); } void SettingsPage::setTheme(bool dark) @@ -32,7 +36,7 @@ void SettingsPage::setTheme(bool dark) } else { - // 亮色模式 + // ���色模式 this->setStyleSheet("#frame{background-color: #ffffff;border-radius:14px;border:1px solid rgb(229,229,229);}"); } } @@ -274,4 +278,33 @@ void SettingsPage::on_checkBox_disableSandbox_clicked(bool checked) QSettings config(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/config.ini", QSettings::IniFormat); config.setValue("webengine/noSandbox", checked); config.sync(); +} + +// 添加导出日志按钮的点击事件处理函数 +void SettingsPage::on_pushButton_exportLog_clicked() +{ + auto future = QtConcurrent::run([=]() { + // 禁用按钮防止重复点击 + QMetaObject::invokeMethod(ui->pushButton_exportLog, "setEnabled", Qt::QueuedConnection, Q_ARG(bool, false)); + + QString targetPath = QString::fromUtf8(TMP_PATH); + bool success = Utils::exportLogs(targetPath); + + // 显示导出结果通知 + QString message; + if (success) { + message = tr("Logs exported successfully to: %1").arg(targetPath); + Utils::writeLog("INFO", "User exported logs via settings page"); + } else { + message = tr("Failed to export logs"); + Utils::writeLog("ERROR", "User failed to export logs via settings page"); + } + + // 在主线程显示消息框 + QMetaObject::invokeMethod(this, [message, this]() { + QMessageBox::information(this, tr("Export Logs"), message); + // 重新启用按钮 + ui->pushButton_exportLog->setEnabled(true); + }, Qt::QueuedConnection); + }); } \ No newline at end of file diff --git a/src/pages/settingspage.h b/src/pages/settingspage.h index 7778d16..c2b14fe 100644 --- a/src/pages/settingspage.h +++ b/src/pages/settingspage.h @@ -33,6 +33,9 @@ private slots: void on_checkBox_clicked(bool checked); void on_checkBox_disableSandbox_clicked(bool checked); + + // 添加导出日志按钮的槽函数声明 + void on_pushButton_exportLog_clicked(); public: static bool needUncompatibleNotification; diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index c00d0a0..bf0d8ad 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -10,6 +10,10 @@ #include #include #include +#include +#include +#include +#include #define UOSDeveloperModeFile "/var/lib/deepin/developer-mode/enabled" @@ -275,4 +279,104 @@ bool Utils::shouldDisableWebEngineSandbox() // 如果配置存在且值为true,则返回true;否则返回false qDebug()<<"shaxiang"<open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { + qWarning() << "Failed to open log file:" << logFilePath; + delete logFile; + logFile = nullptr; + return; + } + + // 写入日志头信息 + writeLog("INFO", "Logger initialized"); + writeLog("INFO", QString("Application started at %1").arg( + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))); +} + +// 写入日志 +void Utils::writeLog(const QString &level, const QString &message) +{ + if (!logFile || !logFile->isOpen()) { + return; + } + + QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"); + QString logEntry = QString("[%1] [%2] %3\n").arg(timestamp).arg(level).arg(message); + + QTextStream out(logFile); + out << logEntry; + logFile->flush(); + + // 同时输出到控制台,便于调试 + if (level == "ERROR") { + qCritical() << logEntry.trimmed(); + } else if (level == "WARNING") { + qWarning() << logEntry.trimmed(); + } else { + qDebug() << logEntry.trimmed(); + } +} + +// 导出日志 +bool Utils::exportLogs(const QString &targetPath) +{ + // 确保目标目录存在 + QDir dir; + if (!dir.exists(targetPath)) { + if (!dir.mkpath(targetPath)) { + writeLog("ERROR", QString("Failed to create target directory: %1").arg(targetPath)); + return false; + } + } + + // 关闭当前日志文件,便于复制 + if (logFile && logFile->isOpen()) { + logFile->close(); + } + + // 复制日志文件到目标位置 + QString targetLogPath = targetPath + QString("/spark-store_log_export_%1.log").arg( + QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss")); + + bool success = QFile::copy(logFilePath, targetLogPath); + + if (success) { + writeLog("INFO", QString("Logs exported to: %1").arg(targetLogPath)); + } else { + writeLog("ERROR", QString("Failed to export logs to: %1").arg(targetLogPath)); + } + + // 重新打开日志文件继续记录 + if (logFile && !logFile->isOpen()) { + logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text); + } + + return success; +} + +// 获取日志文件路径 +QString Utils::getLogFilePath() +{ + return logFilePath; } \ No newline at end of file diff --git a/src/utils/utils.h b/src/utils/utils.h index 6082448..6eda97c 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -3,6 +3,7 @@ #include #include +#include class Utils { @@ -19,6 +20,12 @@ public: static void checkUOSDeveloperMode(); static QJsonObject parseFeatureJsonFile(); static bool shouldDisableWebEngineSandbox(); // 新增函数声明 + + // 日志相关函数 + static void initLogger(); // 初始化日志系统 + static void writeLog(const QString &level, const QString &message); // 写入日志 + static bool exportLogs(const QString &targetPath = "/tmp/spark-store"); // 导出日志 + static QString getLogFilePath(); // 获取日志文件路径 }; #endif // UTILS_H \ No newline at end of file