From 947103c45dbd6aa240bbc6134a5e1d62b8a40f7b Mon Sep 17 00:00:00 2001 From: yexin <1072390773@qq.com> Date: Thu, 16 Jul 2020 12:36:46 +0800 Subject: [PATCH] =?UTF-8?q?feat(log):=20=E4=BB=BB=E5=8A=A1=E6=A0=8F?= =?UTF-8?q?=E5=90=84=E6=A8=A1=E5=9D=97=E8=AE=B0=E5=BD=95=E5=B4=A9=E6=BA=83?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=92=8C=E6=AC=A1=E6=95=B0=20=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=A0=8F=E5=90=84=E6=A8=A1=E5=9D=97=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E5=B4=A9=E6=BA=83=E6=97=B6=E9=97=B4=E5=92=8C=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Log: 任务栏各模块记录崩溃时间和次数 Task: https://pms.uniontech.com/zentao/task-view-28507.html --- frame/main.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/frame/main.cpp b/frame/main.cpp index 4838bd0ee..524230139 100644 --- a/frame/main.cpp +++ b/frame/main.cpp @@ -26,7 +26,7 @@ #include #include - +#include #include #include #include @@ -34,8 +34,14 @@ #include #include "dbus/dbusdockadaptors.h" +#include #include +#include +#include +#include +#include +#include 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);