-
-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathSentryCrashReportSink.m
106 lines (90 loc) · 3.73 KB
/
SentryCrashReportSink.m
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
#import "SentryCrashReportSink.h"
#import "SentryAttachment.h"
#import "SentryClient.h"
#import "SentryCrash.h"
#include "SentryCrashMonitor_AppState.h"
#import "SentryCrashReportConverter.h"
#import "SentryCrashWrapper.h"
#import "SentryDefines.h"
#import "SentryDispatchQueueWrapper.h"
#import "SentryEvent.h"
#import "SentryException.h"
#import "SentryHub.h"
#import "SentryLog.h"
#import "SentrySDK+Private.h"
#import "SentrySDK.h"
#import "SentryScope+Private.h"
#import "SentryThread.h"
static const NSTimeInterval SENTRY_APP_START_CRASH_DURATION_THRESHOLD = 2.0;
static const NSTimeInterval SENTRY_APP_START_CRASH_FLUSH_DURATION = 5.0;
@interface SentryCrashReportSink ()
@property (nonatomic, strong) SentryInAppLogic *inAppLogic;
@property (nonatomic, strong) SentryCrashWrapper *crashWrapper;
@property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueue;
@end
@implementation SentryCrashReportSink
- (instancetype)initWithInAppLogic:(SentryInAppLogic *)inAppLogic
crashWrapper:(SentryCrashWrapper *)crashWrapper
dispatchQueue:(SentryDispatchQueueWrapper *)dispatchQueue
{
if (self = [super init]) {
self.inAppLogic = inAppLogic;
self.crashWrapper = crashWrapper;
self.dispatchQueue = dispatchQueue;
}
return self;
}
- (void)filterReports:(NSArray *)reports
onCompletion:(SentryCrashReportFilterCompletion)onCompletion
{
NSTimeInterval durationFromCrashStateInitToLastCrash
= self.crashWrapper.durationFromCrashStateInitToLastCrash;
if (durationFromCrashStateInitToLastCrash > 0
&& durationFromCrashStateInitToLastCrash <= SENTRY_APP_START_CRASH_DURATION_THRESHOLD) {
SENTRY_LOG_WARN(@"Startup crash: detected.");
[SentrySDK setDetectedStartUpCrash:YES];
[self sendReports:reports onCompletion:onCompletion];
[SentrySDK flush:SENTRY_APP_START_CRASH_FLUSH_DURATION];
SENTRY_LOG_DEBUG(@"Startup crash: Finished flushing.");
} else {
[self.dispatchQueue
dispatchAsyncWithBlock:^{ [self sendReports:reports onCompletion:onCompletion]; }];
}
}
- (void)sendReports:(NSArray *)reports onCompletion:(SentryCrashReportFilterCompletion)onCompletion
{
NSMutableArray *sentReports = [NSMutableArray new];
for (NSDictionary *report in reports) {
SentryCrashReportConverter *reportConverter =
[[SentryCrashReportConverter alloc] initWithReport:report inAppLogic:self.inAppLogic];
if (nil != [SentrySDK.currentHub getClient]) {
SentryEvent *event = [reportConverter convertReportToEvent];
if (nil != event) {
[self handleConvertedEvent:event report:report sentReports:sentReports];
}
} else {
SENTRY_LOG_ERROR(
@"Crash reports were found but no [SentrySDK.currentHub getClient] is set. "
@"Cannot send crash reports to Sentry. This is probably a misconfiguration, "
@"make sure you set the client with [SentrySDK.currentHub bindClient] before "
@"calling startCrashHandlerWithError:.");
}
}
if (onCompletion) {
onCompletion(sentReports, YES, nil);
}
}
- (void)handleConvertedEvent:(SentryEvent *)event
report:(NSDictionary *)report
sentReports:(NSMutableArray *)sentReports
{
[sentReports addObject:report];
SentryScope *scope = [[SentryScope alloc] initWithScope:SentrySDK.currentHub.scope];
if (report[SENTRYCRASH_REPORT_ATTACHMENTS_ITEM]) {
for (NSString *ssPath in report[SENTRYCRASH_REPORT_ATTACHMENTS_ITEM]) {
[scope addCrashReportAttachmentInPath:ssPath];
}
}
[SentrySDK captureCrashEvent:event withScope:scope];
}
@end