Skip to content

Commit

Permalink
Merge 021d69c into 864c39a
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphofmann committed Sep 26, 2022
2 parents 864c39a + 021d69c commit 03e9d9d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
56 changes: 35 additions & 21 deletions Sources/Sentry/SentryCrashReportSink.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import "SentryAttachment.h"
#import "SentryClient.h"
#import "SentryCrash.h"
#include "SentryCrashMonitor_AppState.h"
#import "SentryCrashReportConverter.h"
#import "SentryDefines.h"
#import "SentryEvent.h"
Expand Down Expand Up @@ -50,29 +51,42 @@ - (void)filterReports:(NSArray *)reports
onCompletion:(SentryCrashReportFilterCompletion)onCompletion
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
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:.");

float value = sentrycrashstate_currentState()->durationFromCrashStateInitToLastCrash;
if (value != 0 && value <= 5.0) {
SENTRY_LOG_DEBUG(@"Startup crash: detected.");
[self sendReports:reports onCompletion:onCompletion];

[SentrySDK flush:5.0];
SENTRY_LOG_DEBUG(@"Startup crash: Finished flushing.");

} else {
dispatch_async(queue, ^{ [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, TRUE, nil);
}
});
}
if (onCompletion) {
onCompletion(sentReports, TRUE, nil);
}
}

@end
1 change: 1 addition & 0 deletions Sources/Sentry/SentryCrashWrapper.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "SentryCrashWrapper.h"
#import "SentryCrash.h"
#import "SentryCrashMonitor_AppState.h"
#include "SentryCrashMonitor_AppState.h"
#import "SentryCrashMonitor_System.h"
#import "SentryHook.h"
#import <Foundation/Foundation.h>
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/SentryFileManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ - (nullable instancetype)initWithOptions:(SentryOptions *)options
= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)
.firstObject;

SENTRY_LOG_DEBUG(@"SentryFileManager.cachePath: %@", cachePath);

self.sentryPath = [cachePath stringByAppendingPathComponent:@"io.sentry"];
self.sentryPath =
[self.sentryPath stringByAppendingPathComponent:[options.parsedDsn getHash]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

#define kKeyFormatVersion "version"
#define kKeyCrashedLastLaunch "crashedLastLaunch"
#define kKeyDurationFromCrashStateInitToLastCrash "durationFromCrashStateInitToLastCrash"
#define kKeyActiveDurationSinceLastCrash "activeDurationSinceLastCrash"
#define kKeyBackgroundDurationSinceLastCrash "backgroundDurationSinceLastCrash"
#define kKeyLaunchesSinceLastCrash "launchesSinceLastCrash"
Expand All @@ -65,6 +66,8 @@ static const char *g_stateFilePath;
/** Current state. */
static SentryCrash_AppState g_state;

static double g_crashstate_initialize_time;

static volatile bool g_isEnabled = false;

// ============================================================================
Expand Down Expand Up @@ -96,6 +99,10 @@ onFloatingPointElement(const char *const name, const double value, void *const u
return SentryCrashJSON_ERROR_INVALID_DATA;
}

if (strcmp(name, kKeyDurationFromCrashStateInitToLastCrash) == 0) {
state->durationFromCrashStateInitToLastCrash = value;
}

if (strcmp(name, kKeyActiveDurationSinceLastCrash) == 0) {
state->activeDurationSinceLastCrash = value;
}
Expand Down Expand Up @@ -277,6 +284,18 @@ saveState(const char *const path)
!= SentryCrashJSON_OK) {
goto done;
}

// Follow the pattern of crashedLastLaunch to only store the calculated value if the app
// crashed.
double durationFromCrashStateInitToLastCrash = 0;
if (g_state.crashedThisLaunch) {
durationFromCrashStateInitToLastCrash = timeSince(g_crashstate_initialize_time);
}
if ((result = sentrycrashjson_addFloatingPointElement(&JSONContext,
kKeyDurationFromCrashStateInitToLastCrash, durationFromCrashStateInitToLastCrash))
!= SentryCrashJSON_OK) {
goto done;
}
if ((result = sentrycrashjson_addFloatingPointElement(
&JSONContext, kKeyActiveDurationSinceLastCrash, g_state.activeDurationSinceLastCrash))
!= SentryCrashJSON_OK) {
Expand Down Expand Up @@ -315,6 +334,7 @@ saveState(const char *const path)
void
sentrycrashstate_initialize(const char *const stateFilePath)
{
g_crashstate_initialize_time = getCurentTime();
g_stateFilePath = strdup(stateFilePath);
memset(&g_state, 0, sizeof(g_state));
loadState(g_stateFilePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ typedef struct {
/** If true, the application crashed on the previous launch. */
bool crashedLastLaunch;

/** Total time in seconds from the crash state init to the last crash. Only contains a value
* bigger than zero if crashedLastLaunch is true. */
double durationFromCrashStateInitToLastCrash;

// Live data

/** If true, the application crashed on this launch. */
Expand Down

0 comments on commit 03e9d9d

Please sign in to comment.