mirror of
https://gitee.com/spark-store-project/spark-store
synced 2026-03-25 14:59:44 +08:00
enhance: 完善日志内容
This commit is contained in:
80
src/main.cpp
80
src/main.cpp
@@ -5,6 +5,10 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <execinfo.h>
|
#include <execinfo.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include <DSysInfo>
|
#include <DSysInfo>
|
||||||
#include <DApplicationSettings>
|
#include <DApplicationSettings>
|
||||||
@@ -20,17 +24,82 @@
|
|||||||
DCORE_USE_NAMESPACE
|
DCORE_USE_NAMESPACE
|
||||||
DWIDGET_USE_NAMESPACE
|
DWIDGET_USE_NAMESPACE
|
||||||
|
|
||||||
|
static QString buildDateTime;
|
||||||
|
|
||||||
|
|
||||||
void crashHandler(int sig) {
|
void crashHandler(int sig) {
|
||||||
void *array[50];
|
void *array[50];
|
||||||
size_t size;
|
size_t size = backtrace(array, 50);
|
||||||
|
if (size == 0) {
|
||||||
|
perror("backtrace");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
// 获取所有活动的函数指针
|
time_t t = time(NULL);
|
||||||
size = backtrace(array, 50);
|
struct tm tm = *localtime(&t);
|
||||||
|
char filename[128];
|
||||||
|
snprintf(filename, sizeof(filename), "/tmp/spark_store_crash_log_%04d%02d%02d_%02d%02d%02d.txt",
|
||||||
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||||
|
|
||||||
|
std::ofstream logFile(filename, std::ios::out);
|
||||||
|
if (!logFile.is_open()) {
|
||||||
|
perror("ofstream");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
logFile << "Please send this log to the developer. QQ Group: 872690351\n";
|
||||||
|
logFile << "Build Date and Time: " << buildDateTime.toStdString() << "\n";
|
||||||
|
|
||||||
|
FILE *fp = popen("uname -m", "r");
|
||||||
|
if (fp) {
|
||||||
|
char buffer[256];
|
||||||
|
if (fgets(buffer, sizeof(buffer), fp) != NULL) {
|
||||||
|
// 移除换行符
|
||||||
|
buffer[strcspn(buffer, "\n")] = 0;
|
||||||
|
logFile << "CPU Architecture: " << buffer << "\n";
|
||||||
|
}
|
||||||
|
pclose(fp);
|
||||||
|
} else {
|
||||||
|
logFile << "Failed to gather CPU architecture info.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
FILE *fp2 = popen("lsb_release -a", "r");
|
||||||
|
if (fp2) {
|
||||||
|
char buffer[256];
|
||||||
|
while (fgets(buffer, sizeof(buffer), fp2) != NULL) {
|
||||||
|
logFile << buffer;
|
||||||
|
}
|
||||||
|
pclose(fp2);
|
||||||
|
} else {
|
||||||
|
logFile << "Failed to gather distribution info.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
logFile << "Error: signal " << sig << ":\n";
|
||||||
|
for (size_t i = 0; i < size; i++) {
|
||||||
|
char **strings = backtrace_symbols(&array[i], 1);
|
||||||
|
if (strings != NULL && strings[0] != NULL) {
|
||||||
|
logFile << strings[0] << "\n";
|
||||||
|
} else {
|
||||||
|
logFile << "Failed to get symbol.\n";
|
||||||
|
}
|
||||||
|
free(strings);
|
||||||
|
}
|
||||||
|
|
||||||
|
logFile.close();
|
||||||
|
|
||||||
|
char openCmd[256];
|
||||||
|
snprintf(openCmd, sizeof(openCmd), "xdg-open %s", filename);
|
||||||
|
if (system(openCmd) == -1) {
|
||||||
|
perror("system");
|
||||||
|
}
|
||||||
|
|
||||||
// 打印函数调用堆栈
|
|
||||||
fprintf(stderr, "Error: signal %d:\n", sig);
|
fprintf(stderr, "Error: signal %d:\n", sig);
|
||||||
backtrace_symbols_fd(array, size, STDERR_FILENO);
|
backtrace_symbols_fd(array, size, STDERR_FILENO);
|
||||||
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +113,8 @@ int main(int argc, char *argv[])
|
|||||||
// Get build time
|
// Get build time
|
||||||
static const QDate buildDate = QLocale(QLocale::English).toDate(QString(__DATE__).replace(" ", " 0"), "MMM dd yyyy");
|
static const QDate buildDate = QLocale(QLocale::English).toDate(QString(__DATE__).replace(" ", " 0"), "MMM dd yyyy");
|
||||||
static const QTime buildTime = QTime::fromString(__TIME__, "hh:mm:ss");
|
static const QTime buildTime = QTime::fromString(__TIME__, "hh:mm:ss");
|
||||||
static const QString buildDateTime = buildDate.toString("yyyy.MM.dd") + "-" + buildTime.toString("hh:mm:ss");
|
buildDateTime = buildDate.toString("yyyy.MM.dd") + "-" + buildTime.toString("hh:mm:ss");
|
||||||
|
|
||||||
|
|
||||||
// NOTE: 提前设置组织名称和应用名称,避免配置文件位置错误
|
// NOTE: 提前设置组织名称和应用名称,避免配置文件位置错误
|
||||||
DApplication::setOrganizationName("spark-union");
|
DApplication::setOrganizationName("spark-union");
|
||||||
|
|||||||
Reference in New Issue
Block a user