-
Notifications
You must be signed in to change notification settings - Fork 51
/
crashreporter.cpp
196 lines (159 loc) · 4.71 KB
/
crashreporter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <client/crash_report_database.h>
#include <client/settings.h>
#include <client/crashpad_client.h>
#include <client/annotation.h>
#include <fstream>
#include <string>
#include <filesystem>
#include <iostream>
#include "version_info.h"
#if defined __has_attribute
# if __has_attribute (init_priority)
# define INITPRIO_CLASS __attribute__ ((init_priority (5000)))
# endif
#endif
#ifndef INITPRIO_CLASS
# ifdef _MSC_VER
#pragma init_seg(lib)
# endif
#endif
#ifndef INITPRIO_CLASS
# define INITPRIO_CLASS
#endif
class crash_reporter
{
crashpad::CrashpadClient client;
std::unique_ptr<crashpad::CrashReportDatabase> database;
public:
std::string provider = "";
bool autoupload = false;
crash_reporter();
void set_autoupload();
void upload_reject();
void upload_accept();
bool upload_pending();
};
crash_reporter::crash_reporter()
{
#ifdef _WIN32
if (!std::filesystem::exists("crashdumps/crashpad_handler.exe"))
return;
#else
if (!std::filesystem::exists("crashdumps/crashpad_handler"))
return;
#endif
autoupload = std::filesystem::exists("crashdumps/autoupload_enabled.conf");
std::string url, token, prov;
std::ifstream conf("crashdumps/crashpad.conf");
std::string line, param, value;
while (std::getline(conf, line)) {
std::istringstream linestream(line);
if (!std::getline(linestream, param, '='))
continue;
if (!std::getline(linestream, value, '='))
continue;
if (param == "URL")
url = value;
else if (param == "TOKEN")
token = value;
else if (param == "PROVIDER")
prov = value;
}
if (token.empty())
return;
if (url.empty())
return;
if (prov.empty())
return;
#ifdef _WIN32
base::FilePath db(L"crashdumps");
base::FilePath handler(L"crashdumps/crashpad_handler.exe");
#else
base::FilePath db("crashdumps");
base::FilePath handler("crashdumps/crashpad_handler");
#endif
std::map<std::string, std::string> annotations;
annotations["git_hash"] = GIT_HASH;
annotations["src_date"] = SRC_DATE;
annotations["format"] = "minidump";
annotations["token"] = token;
std::vector<std::string> arguments;
arguments.push_back("--no-rate-limit");
database = crashpad::CrashReportDatabase::Initialize(db);
if (database == nullptr || database->GetSettings() == NULL)
return;
std::vector<crashpad::CrashReportDatabase::Report> reports;
database->GetCompletedReports(&reports);
for (auto const &report : reports)
if (report.uploaded)
database->DeleteReport(report.uuid);
database->GetSettings()->SetUploadsEnabled(autoupload);
if (!client.StartHandler(handler, db, db, url, annotations, arguments, true, true))
return;
provider = prov;
}
void crash_reporter::set_autoupload()
{
autoupload = true;
database->GetSettings()->SetUploadsEnabled(true);
std::ofstream flag("crashdumps/autoupload_enabled.conf");
flag << "y" << std::endl;
flag.close();
}
void crash_reporter::upload_accept()
{
std::vector<crashpad::CrashReportDatabase::Report> reports;
database->GetCompletedReports(&reports);
for (auto const &report : reports)
if (!report.uploaded)
database->RequestUpload(report.uuid);
}
void crash_reporter::upload_reject()
{
std::vector<crashpad::CrashReportDatabase::Report> reports;
database->GetCompletedReports(&reports);
for (auto const &report : reports)
if (!report.uploaded)
database->DeleteReport(report.uuid);
}
bool crash_reporter::upload_pending()
{
if (autoupload)
return false;
std::vector<crashpad::CrashReportDatabase::Report> reports;
database->GetCompletedReports(&reports);
for (auto const &report : reports)
if (!report.uploaded && !report.upload_explicitly_requested)
return true;
return false;
}
crash_reporter crash_reporter_inst INITPRIO_CLASS;
const std::string& crashreport_get_provider()
{
return crash_reporter_inst.provider;
}
void crashreport_add_info(const char *name, const std::string &value)
{
char *copy = new char[value.size() + 1];
strcpy(copy, value.c_str());
crashpad::Annotation *annotation = new crashpad::Annotation(crashpad::Annotation::Type::kString, name, copy);
annotation->SetSize(value.size() + 1);
}
void crashreport_set_autoupload()
{
crash_reporter_inst.set_autoupload();
}
bool crashreport_is_pending()
{
if (crash_reporter_inst.provider.empty())
return false;
return crash_reporter_inst.upload_pending();
}
void crashreport_upload_reject()
{
crash_reporter_inst.upload_reject();
}
void crashreport_upload_accept()
{
crash_reporter_inst.upload_accept();
}