mirror of
https://gitee.com/spark-store-project/spark-store
synced 2025-12-14 12:52:04 +08:00
chore:添加导出日志功能
This commit is contained in:
13
src/main.cpp
13
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();
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <QSettings>
|
||||
#include <QtConcurrent>
|
||||
#include <QDebug>
|
||||
#include <QMessageBox>
|
||||
|
||||
#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
|
||||
{
|
||||
// 亮色模式
|
||||
// <EFBFBD><EFBFBD><EFBFBD>色模式
|
||||
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);
|
||||
});
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -10,6 +10,10 @@
|
||||
#include <QFile>
|
||||
#include <QUuid>
|
||||
#include <QJsonDocument>
|
||||
#include <QDateTime>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
#define UOSDeveloperModeFile "/var/lib/deepin/developer-mode/enabled"
|
||||
|
||||
@@ -275,4 +279,104 @@ bool Utils::shouldDisableWebEngineSandbox()
|
||||
// 如果配置存在且值为true,则返回true;否则返回false
|
||||
qDebug()<<"shaxiang"<<config.value("webengine/noSandbox", false).toBool();
|
||||
return config.value("webengine/noSandbox", false).toBool();
|
||||
}
|
||||
|
||||
// 日志相关静态变量
|
||||
static QFile *logFile = nullptr;
|
||||
static QString logFilePath;
|
||||
|
||||
// 初始化日志系统
|
||||
void Utils::initLogger()
|
||||
{
|
||||
// 确保日志目录存在
|
||||
QString logDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||
QDir dir;
|
||||
if (!dir.exists(logDir)) {
|
||||
dir.mkpath(logDir);
|
||||
}
|
||||
|
||||
// 设置日志文件路径
|
||||
QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss");
|
||||
logFilePath = logDir + QString("/spark-store_%1.log").arg(timestamp);
|
||||
|
||||
// 打开日志文件
|
||||
logFile = new QFile(logFilePath);
|
||||
if (!logFile->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;
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QJsonObject>
|
||||
#include <QString>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user