Refactoring.

This commit is contained in:
Roman Telezhynskyi 2024-03-25 13:59:17 +02:00
parent 035e702a04
commit 7383d78775
3 changed files with 25 additions and 25 deletions

View File

@ -43,6 +43,9 @@
#define MIN(x, y) (((x) < (y)) ? (x) : (y)) // NOLINT #define MIN(x, y) (((x) < (y)) ? (x) : (y)) // NOLINT
#endif #endif
#include <string>
#include <vector>
#include <client/crash_report_database.h> #include <client/crash_report_database.h>
#include <client/crashpad_client.h> #include <client/crashpad_client.h>
#include <client/settings.h> #include <client/settings.h>
@ -58,7 +61,6 @@
#include <vcsRepoState.h> #include <vcsRepoState.h>
using namespace base;
using namespace crashpad; using namespace crashpad;
namespace namespace
@ -168,7 +170,7 @@ auto InitializeCrashpad(const QString &appName) -> bool
#endif #endif
// Helper class for cross-platform file systems // Helper class for cross-platform file systems
VCrashPaths crashpadPaths(exeDir); VCrashPaths const crashpadPaths(exeDir);
auto MakeDir = [](const QString &path) auto MakeDir = [](const QString &path)
{ {
@ -182,7 +184,7 @@ auto InitializeCrashpad(const QString &appName) -> bool
// Directory where reports will be saved. Important! Must be writable or crashpad_handler will crash. // Directory where reports will be saved. Important! Must be writable or crashpad_handler will crash.
QString const reportsPath = VCrashPaths::GetReportsPath(); QString const reportsPath = VCrashPaths::GetReportsPath();
MakeDir(reportsPath); MakeDir(reportsPath);
FilePath const reportsDir(VCrashPaths::GetPlatformString(reportsPath)); base::FilePath const reportsDir(VCrashPaths::GetPlatformString(reportsPath));
// Initialize crashpad database // Initialize crashpad database
std::unique_ptr<CrashReportDatabase> database = CrashReportDatabase::Initialize(reportsDir); std::unique_ptr<CrashReportDatabase> database = CrashReportDatabase::Initialize(reportsDir);
@ -200,20 +202,20 @@ auto InitializeCrashpad(const QString &appName) -> bool
settings->SetUploadsEnabled(true); settings->SetUploadsEnabled(true);
// Attachments to be uploaded alongside the crash - default bundle size limit is 20MB // Attachments to be uploaded alongside the crash - default bundle size limit is 20MB
std::vector<FilePath> attachments; std::vector<base::FilePath> attachments;
FilePath const attachment(VCrashPaths::GetPlatformString(VCrashPaths::GetAttachmentPath(appName))); base::FilePath const attachment(VCrashPaths::GetPlatformString(VCrashPaths::GetAttachmentPath(appName)));
#if defined(Q_OS_WINDOWS) || defined(Q_OS_LINUX) #if defined(Q_OS_WINDOWS) || defined(Q_OS_LINUX)
// Crashpad hasn't implemented attachments on OS X yet // Crashpad hasn't implemented attachments on OS X yet
attachments.push_back(attachment); attachments.push_back(attachment);
#endif #endif
// Ensure that crashpad_handler is shipped with your application // Ensure that crashpad_handler is shipped with your application
FilePath const handler(VCrashPaths::GetPlatformString(crashpadPaths.GetHandlerPath())); base::FilePath const handler(VCrashPaths::GetPlatformString(crashpadPaths.GetHandlerPath()));
// Directory where metrics will be saved. Important! Must be writable or crashpad_handler will crash. // Directory where metrics will be saved. Important! Must be writable or crashpad_handler will crash.
QString const metricsPath = VCrashPaths::GetMetricsPath(); QString const metricsPath = VCrashPaths::GetMetricsPath();
MakeDir(metricsPath); MakeDir(metricsPath);
FilePath const metricsDir(VCrashPaths::GetPlatformString(metricsPath)); base::FilePath const metricsDir(VCrashPaths::GetPlatformString(metricsPath));
QString const dbName = QStringLiteral("valentina"); QString const dbName = QStringLiteral("valentina");
// Configure url with your BugSplat database // Configure url with your BugSplat database
@ -221,7 +223,7 @@ auto InitializeCrashpad(const QString &appName) -> bool
// Metadata that will be posted to BugSplat // Metadata that will be posted to BugSplat
QMap<std::string, std::string> annotations; QMap<std::string, std::string> annotations;
annotations["format"] = "minidump"; // Required: Crashpad setting to save crash as a minidump annotations["format"] = "minidump"; // Required: Crashpad setting to save crash as a
annotations["database"] = dbName.toStdString(); // Required: BugSplat database annotations["database"] = dbName.toStdString(); // Required: BugSplat database
annotations["product"] = appName.toStdString(); // Required: BugSplat appName annotations["product"] = appName.toStdString(); // Required: BugSplat appName
annotations["version"] = AppCrashVersion().toStdString(); // Required: BugSplat appVersion annotations["version"] = AppCrashVersion().toStdString(); // Required: BugSplat appVersion
@ -245,7 +247,7 @@ auto InitializeCrashpad(const QString &appName) -> bool
arguments.emplace_back("--no-rate-limit"); arguments.emplace_back("--no-rate-limit");
// Start crash handler // Start crash handler
auto *client = new CrashpadClient(); CrashpadClient client;
return client->StartHandler(handler, reportsDir, metricsDir, url.toStdString(), "", annotations.toStdMap(), return client.StartHandler(handler, reportsDir, metricsDir, url.toStdString(), "", annotations.toStdMap(),
arguments, true, true, attachments); arguments, true, true, attachments);
} }

View File

@ -49,7 +49,7 @@ auto VCrashPaths::GetAttachmentPath(const QString &appName) -> QString
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
auto VCrashPaths::GetHandlerPath() -> QString auto VCrashPaths::GetHandlerPath() const -> QString
{ {
#if defined(Q_OS_WINDOWS) #if defined(Q_OS_WINDOWS)
const QString handler = QStringLiteral("crashpad_handler.exe"); const QString handler = QStringLiteral("crashpad_handler.exe");
@ -76,16 +76,14 @@ auto VCrashPaths::GetMetricsPath() -> QString
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
auto VCrashPaths::GetPlatformString(const QString &string) -> base::FilePath::StringType
{
#if defined(Q_OS_UNIX) #if defined(Q_OS_UNIX)
auto VCrashPaths::GetPlatformString(const QString &string) -> std::string
{
return string.toStdString(); return string.toStdString();
}
#elif defined(Q_OS_WINDOWS) #elif defined(Q_OS_WINDOWS)
auto VCrashPaths::GetPlatformString(const QString &string) -> std::wstring
{
return string.toStdWString();
}
#else #else
return string.toStdWString();
#error GetPlatformString not implemented on this platform #error GetPlatformString not implemented on this platform
#endif #endif
}

View File

@ -30,22 +30,22 @@
#include <QString> #include <QString>
#include <base/files/file_path.h>
class VCrashPaths class VCrashPaths
{ {
public: public:
explicit VCrashPaths(QString exeDir); explicit VCrashPaths(QString exeDir);
auto GetHandlerPath() -> QString; auto GetHandlerPath() const -> QString;
static auto GetAttachmentPath(const QString &appName) -> QString; static auto GetAttachmentPath(const QString &appName) -> QString;
static auto GetReportsPath() -> QString; static auto GetReportsPath() -> QString;
static auto GetMetricsPath() -> QString; static auto GetMetricsPath() -> QString;
#if defined(Q_OS_UNIX) #if defined(Q_OS_UNIX) || defined(Q_OS_WINDOWS)
static auto GetPlatformString(const QString &string) -> std::string; static auto GetPlatformString(const QString &string) -> base::FilePath::StringType;
#elif defined(Q_OS_WINDOWS) #elif
static auto GetPlatformString(const QString &string) -> std::wstring;
#else
#error GetPlatformString not implemented on this platform #error GetPlatformString not implemented on this platform
#endif #endif