forked from getsentry/sentry-cocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentryCrashIntegrationSessionHandler.m
More file actions
89 lines (75 loc) · 2.88 KB
/
SentryCrashIntegrationSessionHandler.m
File metadata and controls
89 lines (75 loc) · 2.88 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
#import "SentryCrashIntegrationSessionHandler.h"
#import "SentryClient+Private.h"
#import "SentryHub.h"
#import "SentryInternalDefines.h"
#import "SentryLogC.h"
#import "SentrySDK+Private.h"
#import "SentrySwift.h"
#import "SentryWatchdogTerminationLogic.h"
@interface SentryCrashIntegrationSessionHandler ()
@property (nonatomic, strong) SentryCrashWrapper *crashWrapper;
#if SENTRY_HAS_UIKIT
@property (nonatomic, strong) SentryWatchdogTerminationLogic *watchdogTerminationLogic;
#endif // SENTRY_HAS_UIKIT
@end
@implementation SentryCrashIntegrationSessionHandler
#if SENTRY_HAS_UIKIT
- (instancetype)initWithCrashWrapper:(SentryCrashWrapper *)crashWrapper
watchdogTerminationLogic:(SentryWatchdogTerminationLogic *)watchdogTerminationLogic
#else
- (instancetype)initWithCrashWrapper:(SentryCrashWrapper *)crashWrapper
#endif // SENTRY_HAS_UIKIT
{
self = [self init];
self.crashWrapper = crashWrapper;
#if SENTRY_HAS_UIKIT
self.watchdogTerminationLogic = watchdogTerminationLogic;
#endif // SENTRY_HAS_UIKIT
return self;
}
- (void)endCurrentSessionIfRequired
{
SentryFileManager *fileManager = [[[SentrySDKInternal currentHub] getClient] fileManager];
if (nil == fileManager) {
SENTRY_LOG_DEBUG(@"File manager is nil. Cannot end current session.");
return;
}
SentrySession *session = [fileManager readCurrentSession];
if (session == nil) {
SENTRY_LOG_DEBUG(@"No current session found to end.");
return;
}
if (self.crashWrapper.crashedLastLaunch
#if SENTRY_HAS_UIKIT
|| [self.watchdogTerminationLogic isWatchdogTermination]
#endif // SENTRY_HAS_UIKIT
) {
NSDate *timeSinceLastCrash = [[SentryDependencyContainer.sharedInstance.dateProvider date]
dateByAddingTimeInterval:-self.crashWrapper.activeDurationSinceLastCrash];
[session endSessionCrashedWithTimestamp:timeSinceLastCrash];
[fileManager storeCrashedSession:session];
[fileManager deleteCurrentSession];
}
#if SENTRY_HAS_UIKIT
else {
// Checking the file existence is way cheaper than reading the file and parsing its contents
// to an SentryEvent.
if (![fileManager appHangEventExists]) {
SENTRY_LOG_DEBUG(@"No app hang event found. Won't end current session.");
return;
}
SentryEvent *appHangEvent = [fileManager readAppHangEvent];
// Just in case the file was deleted between the check and the read.
if (appHangEvent == nil) {
SENTRY_LOG_WARN(
@"App hang event deleted between check and read. Cannot end current session.");
return;
}
[session
endSessionAbnormalWithTimestamp:SENTRY_UNWRAP_NULLABLE(NSDate, appHangEvent).timestamp];
[fileManager storeAbnormalSession:session];
[fileManager deleteCurrentSession];
}
#endif // SENTRY_HAS_UIKIT
}
@end