From 1d4451c066f9c31aed0d5f5fa02578f42b6a4379 Mon Sep 17 00:00:00 2001 From: Greedysky Date: Sat, 29 Jan 2022 08:37:29 +0800 Subject: [PATCH] refactor: Optimie project global module[564802] --- TTKCommon/ttkglobal.h | 20 +++++++++++------ TTKCommon/ttkprivate.h | 2 +- TTKCommon/ttksingleton.h | 2 +- TTKThirdParty/TTKDumper/minidumper.cpp | 31 +++++++++++++++++++------- TTKThirdParty/TTKDumper/minidumper.h | 7 +++--- TTKThirdParty/TTKDumper/ttkdumper.cpp | 2 +- 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/TTKCommon/ttkglobal.h b/TTKCommon/ttkglobal.h index 39cbbff..fb85941 100644 --- a/TTKCommon/ttkglobal.h +++ b/TTKCommon/ttkglobal.h @@ -136,15 +136,21 @@ Q_DECL_CONSTEXPR typename std::add_const::type &qAsConst(T &t) { return t; } # endif #endif -// -#define TTK_DECLARE_MODULE(Class) \ -public: \ - inline static QString className() \ - { \ - return #Class; \ +// disable copy +#define TTK_DISABLE_COPY(Class) \ +private: \ + Class(const Class &) TTK_DELETE; \ + Class &operator=(const Class &) TTK_DELETE; + +// make class name +#define TTK_DECLARE_MODULE(Class) \ +public: \ + inline static QString className() \ + { \ + return #Class; \ } -// +// marco str cat #ifndef _MSC_VER // gcc version less than 3.4.0 # if __GNUC__ <= 3 && __GNUC_MINOR__ <= 4 diff --git a/TTKCommon/ttkprivate.h b/TTKCommon/ttkprivate.h index 7851c95..ed6a632 100644 --- a/TTKCommon/ttkprivate.h +++ b/TTKCommon/ttkprivate.h @@ -118,7 +118,7 @@ class TTK_MODULE_EXPORT TTKPrivateInterface private: TTKPrivate* m_dptr; - Q_DISABLE_COPY(TTKPrivateInterface) + TTK_DISABLE_COPY(TTKPrivateInterface) }; diff --git a/TTKCommon/ttksingleton.h b/TTKCommon/ttksingleton.h index 90e120a..cf8f806 100644 --- a/TTKCommon/ttksingleton.h +++ b/TTKCommon/ttksingleton.h @@ -44,7 +44,7 @@ class TTK_MODULE_EXPORT TTKSingleton static QMutex m_mutex; static QScopedPointer m_instance; - Q_DISABLE_COPY(TTKSingleton) + TTK_DISABLE_COPY(TTKSingleton) }; diff --git a/TTKThirdParty/TTKDumper/minidumper.cpp b/TTKThirdParty/TTKDumper/minidumper.cpp index efafeb0..cee63e2 100644 --- a/TTKThirdParty/TTKDumper/minidumper.cpp +++ b/TTKThirdParty/TTKDumper/minidumper.cpp @@ -6,16 +6,14 @@ LPCWSTR MiniDumper::m_appName; LPWSTR MiniDumper::m_appVersion; -LPWSTR MiniDumper::m_appBuildNumber; LPWSTR MiniDumper::m_dumpFilePath; #define MAX_DUMP_FILE_NUMBER 9999 -MiniDumper::MiniDumper(LPCWSTR name, LPCWSTR version, LPCWSTR number) +MiniDumper::MiniDumper(LPCWSTR name, LPCWSTR version) { m_appName = name ? wcsdup(name) : wcsdup(L"TTK"); m_appVersion = version ? wcsdup(version) : wcsdup(L"1.0.0.0"); - m_appBuildNumber = number ? wcsdup(number) : wcsdup(L"0000"); m_dumpFilePath = nullptr; ::SetUnhandledExceptionFilter(TopLevelFilter); @@ -92,8 +90,6 @@ LONG MiniDumper::TopLevelFilter(EXCEPTION_POINTERS *info) wcscat(dumpPath, m_appName); wcscat(dumpPath, L"_"); wcscat(dumpPath, m_appVersion); - wcscat(dumpPath, L"_"); - wcscat(dumpPath, m_appBuildNumber); wcscat(dumpPath, fileNumber); wcscat(dumpPath, L".dmp"); @@ -142,15 +138,34 @@ LONG MiniDumper::TopLevelFilter(EXCEPTION_POINTERS *info) #elif defined Q_OS_UNIX #include +#include +#include +#include + +static QString GlobalAppName; -void errorHandler(int type) +static void errorHandler(int id) { - TTK_LOGGER_INFO("Error Type " << type); + TTK_LOGGER_INFO("App error occurred, error code " << id); + + char stamp[50]; + sprintf(stamp, "%ld", time(nullptr)); + const std::string& file_name = GlobalAppName.toStdString() + "." + stamp + ".dmp"; + + const int size = 512; + void* array[size]; + const int fd = open(file_name.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + backtrace_symbols_fd(array, backtrace(array, size), fd); + close(fd); + + raise(id); exit(0); } -MiniDumper::MiniDumper() +MiniDumper::MiniDumper(const QString &name, const QString &version) { + GlobalAppName = name + '_' + version; + signal(SIGPIPE, errorHandler); signal(SIGSEGV, errorHandler); signal(SIGFPE, errorHandler); diff --git a/TTKThirdParty/TTKDumper/minidumper.h b/TTKThirdParty/TTKDumper/minidumper.h index fdd8632..567d732 100644 --- a/TTKThirdParty/TTKDumper/minidumper.h +++ b/TTKThirdParty/TTKDumper/minidumper.h @@ -20,13 +20,13 @@ typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hF class TTK_MODULE_EXPORT MiniDumper { public: - MiniDumper(LPCWSTR name, LPCWSTR version, LPCWSTR number = nullptr); + MiniDumper(LPCWSTR name, LPCWSTR version); private: static LPCWSTR m_appName; static LPWSTR m_appVersion; - static LPWSTR m_appBuildNumber; static LPWSTR m_dumpFilePath; + static LONG WINAPI TopLevelFilter(EXCEPTION_POINTERS *info); }; @@ -38,7 +38,8 @@ class TTK_MODULE_EXPORT MiniDumper class TTK_MODULE_EXPORT MiniDumper { public: - MiniDumper(); + MiniDumper(const QString &name, const QString &version); + }; #endif diff --git a/TTKThirdParty/TTKDumper/ttkdumper.cpp b/TTKThirdParty/TTKDumper/ttkdumper.cpp index 95bc95d..727b270 100644 --- a/TTKThirdParty/TTKDumper/ttkdumper.cpp +++ b/TTKThirdParty/TTKDumper/ttkdumper.cpp @@ -7,7 +7,7 @@ TTKDumper::TTKDumper() #ifdef Q_OS_WIN m_dump = new MiniDumper(L"TTK", OCR_VERSION_WSTR); #elif defined Q_OS_UNIX - m_dump = new MiniDumper(); + m_dump = new MiniDumper("TTK", OCR_VERSION_STR); #endif }