Skip to content

Commit

Permalink
Merge pull request kstenerud#331 from GLinnik21/master
Browse files Browse the repository at this point in the history
fix activeDurationSinceLaunch
  • Loading branch information
bamx23 authored May 22, 2019
2 parents 412a325 + 50db78e commit 06a986e
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 57 deletions.
116 changes: 68 additions & 48 deletions Source/KSCrash/Recording/KSCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ @implementation KSCrash
#pragma mark - Lifecycle -
// ============================================================================

+ (void)load
{
[[self class] classDidBecomeLoaded];
}

+ (void)initialize
{
if (self == [KSCrash class]) {
[[self class] subscribeToNotifications];
}
}

+ (instancetype) sharedInstance
{
static KSCrash *sharedInstance = nil;
Expand Down Expand Up @@ -311,49 +323,6 @@ - (BOOL) install
return false;
}

#if KSCRASH_HAS_UIAPPLICATION
NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter];
[nCenter addObserver:self
selector:@selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationWillResignActive)
name:UIApplicationWillResignActiveNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationDidEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationWillTerminate)
name:UIApplicationWillTerminateNotification
object:nil];
#endif
#if KSCRASH_HAS_NSEXTENSION
NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter];
[nCenter addObserver:self
selector:@selector(applicationDidBecomeActive)
name:NSExtensionHostDidBecomeActiveNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationWillResignActive)
name:NSExtensionHostWillResignActiveNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationDidEnterBackground)
name:NSExtensionHostDidEnterBackgroundNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationWillEnterForeground)
name:NSExtensionHostWillEnterForegroundNotification
object:nil];
#endif

return true;
}

Expand Down Expand Up @@ -590,27 +559,78 @@ - (NSMutableData*) nullTerminated:(NSData*) data
#pragma mark - Notifications -
// ============================================================================

- (void) applicationDidBecomeActive
+ (void) subscribeToNotifications
{
#if KSCRASH_HAS_UIAPPLICATION
NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter];
[nCenter addObserver:self
selector:@selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationWillResignActive)
name:UIApplicationWillResignActiveNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationDidEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationWillTerminate)
name:UIApplicationWillTerminateNotification
object:nil];
#endif
#if KSCRASH_HAS_NSEXTENSION
NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter];
[nCenter addObserver:self
selector:@selector(applicationDidBecomeActive)
name:NSExtensionHostDidBecomeActiveNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationWillResignActive)
name:NSExtensionHostWillResignActiveNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationDidEnterBackground)
name:NSExtensionHostDidEnterBackgroundNotification
object:nil];
[nCenter addObserver:self
selector:@selector(applicationWillEnterForeground)
name:NSExtensionHostWillEnterForegroundNotification
object:nil];
#endif
}

+ (void) classDidBecomeLoaded
{
kscrash_notifyObjCLoad();
}

+ (void) applicationDidBecomeActive
{
kscrash_notifyAppActive(true);
}

- (void) applicationWillResignActive
+ (void) applicationWillResignActive
{
kscrash_notifyAppActive(false);
}

- (void) applicationDidEnterBackground
+ (void) applicationDidEnterBackground
{
kscrash_notifyAppInForeground(false);
}

- (void) applicationWillEnterForeground
+ (void) applicationWillEnterForeground
{
kscrash_notifyAppInForeground(true);
}

- (void) applicationWillTerminate
+ (void) applicationWillTerminate
{
kscrash_notifyAppTerminate();
}
Expand Down
60 changes: 56 additions & 4 deletions Source/KSCrash/Recording/KSCrashC.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
#include <stdlib.h>
#include <string.h>

typedef enum
{
KSApplicationStateNone,
KSApplicationStateDidBecomeActive,
KSApplicationStateWillResignActiveActive,
KSApplicationStateDidEnterBackground,
KSApplicationStateWillEnterForeground,
KSApplicationStateWillTerminate
} KSApplicationState;

// ============================================================================
#pragma mark - Globals -
Expand All @@ -64,7 +73,7 @@ static char g_consoleLogPath[KSFU_MAX_PATH_LENGTH];
static KSCrashMonitorType g_monitoring = KSCrashMonitorTypeProductionSafeMinimal;
static char g_lastCrashReportFilePath[KSFU_MAX_PATH_LENGTH];
static KSReportWrittenCallback g_reportWrittenCallback;

static KSApplicationState g_lastApplicationState = KSApplicationStateNone;

// ============================================================================
#pragma mark - Utility -
Expand All @@ -83,6 +92,25 @@ static void printPreviousLog(const char* filePath)
}
}

static void notifyOfBeforeInstallationState(void)
{
KSLOG_DEBUG("Notifying of pre-installation state");
switch (g_lastApplicationState)
{
case KSApplicationStateDidBecomeActive:
return kscrash_notifyAppActive(true);
case KSApplicationStateWillResignActiveActive:
return kscrash_notifyAppActive(false);
case KSApplicationStateDidEnterBackground:
return kscrash_notifyAppInForeground(false);
case KSApplicationStateWillEnterForeground:
return kscrash_notifyAppInForeground(true);
case KSApplicationStateWillTerminate:
return kscrash_notifyAppTerminate();
default:
return;
}
}

// ============================================================================
#pragma mark - Callbacks -
Expand Down Expand Up @@ -157,6 +185,9 @@ KSCrashMonitorType kscrash_install(const char* appName, const char* const instal
KSCrashMonitorType monitors = kscrash_setMonitoring(g_monitoring);

KSLOG_DEBUG("Installation complete.");

notifyOfBeforeInstallationState();

return monitors;
}

Expand Down Expand Up @@ -246,19 +277,40 @@ void kscrash_reportUserException(const char* name,
}
}

void kscrash_notifyObjCLoad(void)
{
kscrashstate_notifyObjCLoad();
}

void kscrash_notifyAppActive(bool isActive)
{
kscrashstate_notifyAppActive(isActive);
if (g_installed)
{
kscrashstate_notifyAppActive(isActive);
}
g_lastApplicationState = isActive
? KSApplicationStateDidBecomeActive
: KSApplicationStateWillResignActiveActive;
}

void kscrash_notifyAppInForeground(bool isInForeground)
{
kscrashstate_notifyAppInForeground(isInForeground);
if (g_installed)
{
kscrashstate_notifyAppInForeground(isInForeground);
}
g_lastApplicationState = isInForeground
? KSApplicationStateWillEnterForeground
: KSApplicationStateDidEnterBackground;
}

void kscrash_notifyAppTerminate(void)
{
kscrashstate_notifyAppTerminate();
if (g_installed)
{
kscrashstate_notifyAppTerminate();
}
g_lastApplicationState = KSApplicationStateWillTerminate;
}

void kscrash_notifyAppCrash(void)
Expand Down
4 changes: 4 additions & 0 deletions Source/KSCrash/Recording/KSCrashC.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ void kscrash_reportUserException(const char* name,

#pragma mark -- Notifications --

/** Notify the crash reporter of KSCrash being added to Objective-C runtime system.
*/
void kscrash_notifyObjCLoad(void);

/** Notify the crash reporter of the application active state.
*
* @param isActive true if the application is active, otherwise false.
Expand Down
26 changes: 21 additions & 5 deletions Source/KSCrash/Recording/Monitors/KSCrashMonitor_AppState.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,15 @@ static void updateAppState(void)

if(g_state.applicationIsActive)
{
KSLOG_TRACE("Updating activeDurationSinceLaunch: %f and activeDurationSinceLastCrash: %f with duration: %f",
g_state.activeDurationSinceLaunch, g_state.activeDurationSinceLastCrash, duration);
g_state.activeDurationSinceLaunch += duration;
g_state.activeDurationSinceLastCrash += duration;
}
else if(!g_state.applicationIsInForeground)
{
KSLOG_TRACE("Updating backgroundDurationSinceLaunch: %f and backgroundDurationSinceLastCrash: %f with duration: %f",
g_state.backgroundDurationSinceLaunch, g_state.backgroundDurationSinceLastCrash, duration);
g_state.backgroundDurationSinceLaunch += duration;
g_state.backgroundDurationSinceLastCrash += duration;
}
Expand All @@ -345,7 +349,6 @@ static void updateAppState(void)
void kscrashstate_initialize(const char* const stateFilePath)
{
g_stateFilePath = strdup(stateFilePath);
memset(&g_state, 0, sizeof(g_state));
loadState(g_stateFilePath);
}

Expand All @@ -369,24 +372,36 @@ bool kscrashstate_reset()
g_state.launchesSinceLastCrash++;
g_state.sessionsSinceLastCrash++;
g_state.applicationIsInForeground = true;

return saveState(g_stateFilePath);
}
return false;
}

void kscrashstate_notifyObjCLoad(void)
{
KSLOG_TRACE("KSCrash has been loaded!");
memset(&g_state, 0, sizeof(g_state));
g_state.applicationIsInForeground = false;
g_state.applicationIsActive = true;
g_state.appStateTransitionTime = getCurentTime();
}

void kscrashstate_notifyAppActive(const bool isActive)
{
if(g_isEnabled)
{
g_state.applicationIsActive = isActive;
if(isActive)
{
KSLOG_TRACE("Updating transition time from: %f to: %f", g_state.appStateTransitionTime, getCurentTime());
g_state.appStateTransitionTime = getCurentTime();
}
else
{
double duration = timeSince(g_state.appStateTransitionTime);
KSLOG_TRACE("Updating activeDurationSinceLaunch: %f and activeDurationSinceLastCrash: %f with duration: %f",
g_state.activeDurationSinceLaunch, g_state.activeDurationSinceLastCrash, duration);
g_state.activeDurationSinceLaunch += duration;
g_state.activeDurationSinceLastCrash += duration;
}
Expand All @@ -403,6 +418,8 @@ void kscrashstate_notifyAppInForeground(const bool isInForeground)
if(isInForeground)
{
double duration = getCurentTime() - g_state.appStateTransitionTime;
KSLOG_TRACE("Updating backgroundDurationSinceLaunch: %f and backgroundDurationSinceLastCrash: %f with duration: %f",
g_state.backgroundDurationSinceLaunch, g_state.backgroundDurationSinceLastCrash, duration);
g_state.backgroundDurationSinceLaunch += duration;
g_state.backgroundDurationSinceLastCrash += duration;
g_state.sessionsSinceLastCrash++;
Expand All @@ -421,15 +438,14 @@ void kscrashstate_notifyAppTerminate(void)
if(g_isEnabled)
{
const char* const stateFilePath = g_stateFilePath;

const double duration = timeSince(g_state.appStateTransitionTime);
g_state.backgroundDurationSinceLastCrash += duration;
updateAppState();
saveState(stateFilePath);
}
}

void kscrashstate_notifyAppCrash(void)
{
KSLOG_TRACE("Trying to update AppState. g_isEnabled: %d", g_isEnabled);
if(g_isEnabled)
{
const char* const stateFilePath = g_stateFilePath;
Expand Down
4 changes: 4 additions & 0 deletions Source/KSCrash/Recording/Monitors/KSCrashMonitor_AppState.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ void kscrashstate_initialize(const char* stateFilePath);
*/
bool kscrashstate_reset(void);

/** Notify the crash reporter of KSCrash being added to Objective-C runtime system.
*/
void kscrashstate_notifyObjCLoad(void);

/** Notify the crash reporter of the application active state.
*
* @param isActive true if the application is active, otherwise false.
Expand Down

0 comments on commit 06a986e

Please sign in to comment.