-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathraven.cpp
More file actions
365 lines (319 loc) · 10 KB
/
raven.cpp
File metadata and controls
365 lines (319 loc) · 10 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "raven.h"
#include <QHostInfo>
#include <QDebug>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QSysInfo>
#include <ctime>
#ifndef Q_OS_WIN
#include <dlfcn.h>
#ifndef Q_OS_ANDROID
#include <execinfo.h>
#endif
#endif
#define RAVEN_CLIENT_NAME QString("QRaven")
#define RAVEN_CLIENT_VERSION QString("0.1")
const static QNetworkRequest::Attribute RavenUuidAttribute
= static_cast<QNetworkRequest::Attribute>(QNetworkRequest::User + 19);
Raven::Raven(const QString& dsn, QObject* parent)
: QObject(parent)
, m_initialized(false)
, m_networkAccessManager(new QNetworkAccessManager(this))
{
m_eventTemplate["server_name"] = QHostInfo::localHostName();
m_eventTemplate["logger"] = RAVEN_CLIENT_NAME;
m_eventTemplate["platform"] = "c";
m_eventTemplate["release"] = QCoreApplication::applicationVersion();
m_tagsTemplate["os_type"] = QSysInfo::productType();
m_tagsTemplate["os_version"] = QSysInfo::productVersion();
parseDsn(dsn);
connect(m_networkAccessManager, &QNetworkAccessManager::finished, this,
&Raven::requestFinished);
connect(m_networkAccessManager, &QNetworkAccessManager::sslErrors, this,
&Raven::sslErrors);
connect(
this, &Raven::capture, this, &Raven::_capture, Qt::QueuedConnection);
connect(this, &Raven::sendAllPending, this, &Raven::_sendAllPending,
Qt::QueuedConnection);
}
Raven::~Raven()
{
QEventLoop loop;
connect(this, &Raven::eventSent, &loop, &QEventLoop::quit);
while (!m_pendingRequest.isEmpty()) {
loop.exec(QEventLoop::ExcludeUserInputEvents);
}
}
void Raven::parseDsn(const QString& dsn)
{
if (dsn.isEmpty()) {
qWarning() << "DSN is empty, client disabled";
return;
}
QUrl url(dsn);
if (!url.isValid()) {
qWarning() << "DSN is not valid, client disabled";
return;
}
m_protocol = url.scheme();
m_publicKey = url.userName();
m_secretKey = url.password();
m_host = url.host();
m_path = url.path();
m_projectId = url.fileName();
int i = m_path.lastIndexOf('/');
if (i >= 0)
m_path = m_path.left(i);
int port = url.port(80);
if (port != 80)
m_host.append(":").append(QString::number(port));
qDebug() << "Raven client is ready";
m_initialized = true;
}
QString levelString(Raven::RavenLevel level)
{
switch (level) {
case Raven::Fatal:
return "fatal";
case Raven::Error:
return "error";
case Raven::Warning:
return "warning";
case Raven::Info:
return "info";
case Raven::Debug:
return "debug";
}
return "debug";
}
RavenMessage Raven::operator()(Raven::RavenLevel level, QString culprit)
{
RavenMessage event;
event.m_body = QJsonObject(m_eventTemplate);
event.m_tags = QJsonObject(m_tagsTemplate);
event.m_body["event_id"]
= QUuid::createUuid().toString().remove('{').remove('}').remove('-');
event.m_body["level"] = levelString(level);
event.m_body["timestamp"]
= QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
event.m_body["culprit"] = culprit;
event.m_instance = this;
return event;
}
Raven& Raven::operator<<(const RavenTag& tag)
{
m_tagsTemplate[tag.first] = tag.second;
return *this;
}
bool Raven::isInitialized() const { return m_initialized; }
static const QList<QString> _requiredAttributes
= { "event_id", "message", "timestamp", "level", "logger", "platform",
/* "sdk", "device" */ };
void Raven::_capture(const RavenMessage& message)
{
if (!isInitialized())
return;
QJsonObject body = QJsonObject(message.m_body);
body["tags"] = message.m_tags;
for (const auto& attributeName : _requiredAttributes) {
Q_ASSERT(body.contains(attributeName));
}
send(body);
}
void Raven::send(QJsonObject& message)
{
QString clientInfo
= QString("%1/%2").arg(RAVEN_CLIENT_NAME, RAVEN_CLIENT_VERSION);
QString authInfo = QString("Sentry sentry_version=7,"
"sentry_client=%1,"
"sentry_timestamp=%2,"
"sentry_key=%3,"
"sentry_secret=%4")
.arg(clientInfo, QString::number(time(NULL)),
m_publicKey, m_secretKey);
QString url = QString("%1://%2%3/api/%4/store/")
.arg(m_protocol, m_host, m_path, m_projectId);
QString uuid = message["event_id"].toString();
qDebug() << "url=" << url << ",uuid=" << uuid;
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setHeader(QNetworkRequest::UserAgentHeader, clientInfo);
request.setAttribute(RavenUuidAttribute, uuid);
request.setRawHeader("X-Sentry-Auth", authInfo.toUtf8());
QByteArray body = QJsonDocument(message).toJson(QJsonDocument::Indented);
qDebug() << body;
{
QMutexLocker lk(&m_pendingMutex);
m_pendingRequest[uuid] = body;
}
m_networkAccessManager->post(request, body);
}
void Raven::requestFinished(QNetworkReply* reply)
{
QUrl redirectUrl
= reply->attribute(QNetworkRequest::RedirectionTargetAttribute)
.toUrl();
QString uuid = reply->request().attribute(RavenUuidAttribute).toString();
QByteArray body = m_pendingRequest[uuid];
if (!redirectUrl.isEmpty() && redirectUrl != reply->url()) {
QNetworkRequest request = reply->request();
request.setUrl(redirectUrl);
m_networkAccessManager->post(request, body);
}
else {
if (reply->error() == QNetworkReply::NoError) {
qDebug() << "Event sent " << reply->readAll();
}
else {
qDebug() << "Failed to send message to sentry: " << reply->error();
qDebug() << "Sentry answer: " << reply->readAll();
save(uuid, body);
}
{
QMutexLocker lk(&m_pendingMutex);
m_pendingRequest.remove(uuid);
}
emit eventSent(uuid);
}
reply->deleteLater();
}
void Raven::sslErrors(QNetworkReply* reply, const QList<QSslError>& errors)
{
Q_UNUSED(reply)
Q_UNUSED(errors)
reply->ignoreSslErrors();
}
void Raven::save(const QString& uuid, QByteArray& message)
{
QString messageDir = QStandardPaths::writableLocation(
QStandardPaths::AppLocalDataLocation);
messageDir = QDir::cleanPath(messageDir + QDir::separator() + "messages");
QString messageFile
= QDir::cleanPath(messageDir + QDir::separator() + uuid);
QFile file(messageFile);
if (file.open(QIODevice::WriteOnly)) {
file.write(message);
}
else {
qWarning() << "Could not save message, it will be discarded";
}
}
RavenMessage& Raven::send(RavenMessage& message)
{
message.m_instance->capture(message);
return message;
}
RavenTag Raven::tag(const QString& name, const QString& value)
{
return RavenTag(name, value);
}
void Raven::_sendAllPending()
{
QString messageDir = QStandardPaths::writableLocation(
QStandardPaths::AppLocalDataLocation);
messageDir = QDir::cleanPath(messageDir + QDir::separator() + "messages");
QDirIterator it(messageDir, QDir::Files);
while (it.hasNext()) {
auto messageFile = it.next();
QFile file(messageFile);
if (file.open(QIODevice::ReadOnly)) {
QByteArray body = file.readAll();
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(body, &error);
if (error.error == QJsonParseError::NoError) {
QJsonObject object = doc.object();
send(object);
}
else {
qDebug() << "Could not parse message " << messageFile << ": "
<< error.errorString();
}
}
else {
qDebug() << "Could not open file " << messageFile;
}
}
}
RavenMessage& RavenMessage::operator<<(const QString& message)
{
m_body["message"] = message;
return *this;
}
#ifndef Q_OS_WIN
#include <cxxabi.h>
using namespace __cxxabiv1;
#endif
QString util_demangle(std::string to_demangle)
{
#ifdef Q_OS_WIN
return QString::fromStdString(to_demangle);
#else
int status = 0;
char* buff
= __cxxabiv1::__cxa_demangle(to_demangle.c_str(), NULL, NULL, &status);
QString demangled(buff);
free(buff);
if (demangled.isEmpty()) {
return QString::fromStdString(to_demangle);
}
return demangled;
#endif
}
RavenMessage& RavenMessage::operator<<(const std::exception& exc)
{
m_body["message"] = exc.what();
#ifndef Q_OS_WIN
QJsonArray frameList;
#ifndef Q_OS_ANDROID
void* callstack[128];
int i, frames = backtrace(callstack, 128);
QString moduleName;
for (i = 0; i < frames; ++i) {
QJsonObject frame;
Dl_info dlinfo;
if (dladdr(callstack[i], &dlinfo) == 0)
continue;
if (i == 0) {
moduleName = dlinfo.dli_fname;
continue;
}
frame["function"] = util_demangle(dlinfo.dli_sname);
frame["module"] = dlinfo.dli_fname;
frame["in_app"] = false;
frameList.push_back(frame);
}
#else
QString moduleName = "<UNKNOWN>";
#endif
QJsonObject frameHash;
frameHash["frames"] = frameList;
QJsonObject _exc;
_exc["type"] = util_demangle(__cxa_current_exception_type()->name());
_exc["stacktrace"] = frameHash;
_exc["module"] = moduleName;
_exc["value"] = exc.what();
QJsonArray values;
values.push_back(_exc);
QJsonObject exceptionReport;
exceptionReport["values"] = values;
m_body["exception"] = values;
#endif
return *this;
}
RavenMessage& RavenMessage::operator<<(const RavenTag& tag)
{
m_tags[tag.first] = tag.second;
return *this;
}
RavenMessage& RavenMessage::operator<<(RavenMessage& (*pf)(RavenMessage&))
{
return (*pf)(*this);
}
QString Raven::locationInfo(const char* file, const char* func, int line)
{
return QString("%1 in %2 at %3")
.arg(file)
.arg(func)
.arg(QString::number(line));
}