mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-02 15:45:21 +00:00
feat(log): 任务栏各模块记录崩溃时间和次数
任务栏各模块记录崩溃时间和次数 Log: 任务栏各模块记录崩溃时间和次数 Task: https://pms.uniontech.com/zentao/task-view-28507.html
This commit is contained in:
parent
87298061c9
commit
947103c45d
@ -26,7 +26,7 @@
|
||||
|
||||
#include <QAccessible>
|
||||
#include <QDir>
|
||||
|
||||
#include <QStandardPaths>
|
||||
#include <DApplication>
|
||||
#include <DLog>
|
||||
#include <DDBusSender>
|
||||
@ -34,8 +34,14 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include "dbus/dbusdockadaptors.h"
|
||||
#include <string>
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <execinfo.h>
|
||||
#include <sys/stat.h>
|
||||
#include <signal.h>
|
||||
|
||||
DWIDGET_USE_NAMESPACE
|
||||
#ifdef DCORE_NAMESPACE
|
||||
@ -64,12 +70,100 @@ void RegisterDdeSession()
|
||||
qDebug() << Q_FUNC_INFO << r.value();
|
||||
}
|
||||
}
|
||||
const int MAX_STACK_FRAMES = 128;
|
||||
|
||||
using namespace std;
|
||||
|
||||
void sig_crash(int sig)
|
||||
{
|
||||
FILE *fd;
|
||||
struct stat buf;
|
||||
char path[100];
|
||||
memset(path, 0, 100);
|
||||
//崩溃日志路径
|
||||
QString strPath = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)[0] + "/dde-collapse.log";
|
||||
memcpy(path, strPath.toStdString().data(), strPath.length());
|
||||
qDebug() << path;
|
||||
|
||||
stat(path, &buf);
|
||||
if (buf.st_size > 10 * 1024 * 1024) {
|
||||
// 超过10兆则清空内容
|
||||
fd = fopen(path, "w");
|
||||
} else {
|
||||
fd = fopen(path, "at");
|
||||
}
|
||||
|
||||
if (nullptr == fd) {
|
||||
exit(0);
|
||||
}
|
||||
//捕获异常,打印崩溃日志到配置文件中
|
||||
try {
|
||||
char szLine[512] = {0};
|
||||
time_t t = time(nullptr);
|
||||
tm *now = localtime(&t);
|
||||
QString log = "#####" + qApp->applicationName() + "#####\n[%04d-%02d-%02d %02d:%02d:%02d][crash signal number:%d]\n";
|
||||
int nLen1 = sprintf(szLine, log.toStdString().c_str(),
|
||||
now->tm_year + 1900,
|
||||
now->tm_mon + 1,
|
||||
now->tm_mday,
|
||||
now->tm_hour,
|
||||
now->tm_min,
|
||||
now->tm_sec,
|
||||
sig);
|
||||
fwrite(szLine, 1, strlen(szLine), fd);
|
||||
|
||||
#ifdef __linux
|
||||
void *array[MAX_STACK_FRAMES];
|
||||
size_t size = 0;
|
||||
char **strings = nullptr;
|
||||
size_t i, j;
|
||||
signal(sig, SIG_DFL);
|
||||
size = backtrace(array, MAX_STACK_FRAMES);
|
||||
strings = (char **)backtrace_symbols(array, size);
|
||||
for (i = 0; i < size; ++i) {
|
||||
char szLine[512] = {0};
|
||||
sprintf(szLine, "%d %s\n", i, strings[i]);
|
||||
fwrite(szLine, 1, strlen(szLine), fd);
|
||||
|
||||
std::string symbol(strings[i]);
|
||||
|
||||
size_t pos1 = symbol.find_first_of("[");
|
||||
size_t pos2 = symbol.find_last_of("]");
|
||||
std::string address = symbol.substr(pos1 + 1, pos2 - pos1 - 1);
|
||||
char cmd[128] = {0};
|
||||
sprintf(cmd, "addr2line -C -f -e dde-dock %s", address.c_str()); // 打印当前进程的id和地址
|
||||
FILE *fPipe = popen(cmd, "r");
|
||||
if (fPipe != nullptr) {
|
||||
char buff[1024];
|
||||
memset(buff, 0, sizeof(buff));
|
||||
char *ret = fgets(buff, sizeof(buff), fPipe);
|
||||
pclose(fPipe);
|
||||
fwrite(ret, 1, strlen(ret), fd);
|
||||
}
|
||||
}
|
||||
free(strings);
|
||||
#endif // __linux
|
||||
} catch (...) {
|
||||
//
|
||||
}
|
||||
fflush(fd);
|
||||
fclose(fd);
|
||||
fd = nullptr;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
DGuiApplicationHelper::setUseInactiveColorGroup(false);
|
||||
DApplication::loadDXcbPlugin();
|
||||
DApplication app(argc, argv);
|
||||
//崩溃信号
|
||||
signal(SIGTERM, sig_crash);
|
||||
signal(SIGSEGV, sig_crash);
|
||||
signal(SIGILL, sig_crash);
|
||||
signal(SIGINT, sig_crash);
|
||||
signal(SIGABRT, sig_crash);
|
||||
signal(SIGFPE, sig_crash);
|
||||
|
||||
// 锁定物理内存,用于国测测试[会显著增加内存占用]
|
||||
// qDebug() << "lock memory result:" << mlockall(MCL_CURRENT | MCL_FUTURE);
|
||||
|
Loading…
x
Reference in New Issue
Block a user