culturered_client/Common/CrashHandler.cpp

112 lines
3.4 KiB
C++
Raw Permalink Normal View History

2024-09-07 03:34:44 +00:00
#include "CrashHandler.h"
#include <QCoreApplication>
#include <QMessageBox>
#include <QDir>
#include <QDebug>
#if NDEBUG
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#include "client/windows/handler/exception_handler.h"
#elif defined(__APPLE__) && (defined(__GNUC__) || defined(__xlC__) || defined(__xlc__))
#include "client/mac/handler/exception_handler.h"
#endif
bool FindFileForDelete(const QString& path) {
QDir dir(path);
if (!dir.exists())
return false;
dir.setFilter(QDir::Dirs | QDir::Files);
dir.setSorting(QDir::DirsFirst);
QFileInfoList list = dir.entryInfoList();
int i = 0;
do {
QFileInfo fileInfo = list.at(i);
if (fileInfo.fileName() == "." || fileInfo.fileName() == "..") {
i++;
continue;
}
bool bisDir = fileInfo.isDir();
if (bisDir) {
qDebug() << qPrintable(QString("%1 %2 %3").arg(fileInfo.size(), 10)
.arg(fileInfo.fileName(), 10).arg(fileInfo.path())) ;
FindFileForDelete(fileInfo.filePath());
} else {
QDateTime delDateTime = QDateTime::currentDateTime().addDays(-5);
qint64 nSecs = delDateTime.secsTo(fileInfo.birthTime());
if (nSecs < 0) {
qDebug() << qPrintable(QString("%1 %2 %3").arg(fileInfo.size(), 10)
.arg(fileInfo.fileName(), 10).arg(fileInfo.path())) ;
fileInfo.dir().remove(fileInfo.fileName());
}
}
i++;
} while (i < list.size());
return true;
}
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
static bool OnMinidumpCallback(const wchar_t* dump_path,
const wchar_t* minidump_id,
void* context,
EXCEPTION_POINTERS* exinfo,
MDRawAssertionInfo* assertion,
bool succeeded) {
#else
static bool OnMinidumpCallback(const char* dump_path,
const char* id,
void* context,
bool succeeded) {
#endif
qCritical() << __FUNCTION__ << minidump_id;
const QString title = QObject::tr("error");
const QString content = QObject::tr("the appliaction is crash");
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
// MessageBox(nullptr, content.toStdWString().c_str(), title.toStdWString().c_str(), MB_OK);
#endif
return succeeded;
}
std::unique_ptr<google_breakpad::ExceptionHandler> exceptionHandler;
#endif
bool InstallCrashHandler() {
#if NDEBUG
FindFileForDelete("D:/pcm");
QString appDirPath = QCoreApplication::applicationDirPath() + "/crash";
QDir dir;
if (!dir.exists(appDirPath)) {
bool res = dir.mkpath(appDirPath);
qDebug() << "New mkdir " << appDirPath << " " << res;
}
if (exceptionHandler) {
qWarning() << __FUNCTION__ << "exceptionHandler exits";
return true;
}
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
exceptionHandler.reset(new google_breakpad::ExceptionHandler(
appDirPath.toStdWString(), NULL, OnMinidumpCallback, NULL,
google_breakpad::ExceptionHandler::HANDLER_ALL));
#else
exceptionHandler.reset(new google_breakpad::ExceptionHandler(
appDirPath.toStdString(), NULL, OnMinidumpCallback, NULL,
google_breakpad::ExceptionHandler::HANDLER_ALL));
#endif
return !!exceptionHandler;
#endif
return true;
}
void TestCrash() {
volatile int* a = (int*)(nullptr);
*a = 1;
}