Skip to content

Commit

Permalink
KSCrashState buffered output
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdowell committed Jan 13, 2022
1 parent 93f1de8 commit 099c5a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Bugsnag/KSCrash/Source/KSCrash/Recording/BSG_KSCrashState.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "BSG_KSCrashState.h"

#include "BSG_KSFileUtils.h"
#include "BSG_KSFile.h"
#include "BSG_KSJSONCodec.h"
#include "BSG_KSJSONCodecObjC.h"
#include "BSG_KSMach.h"
Expand Down Expand Up @@ -154,8 +154,7 @@ int bsg_kscrashstate_i_onEndData(__unused void *const userData) {
*/
int bsg_kscrashstate_i_addJSONData(const char *const data, const size_t length,
void *const userData) {
const int fd = *((int *)userData);
const bool success = bsg_ksfuwriteBytesToFD(fd, data, (ssize_t)length);
bool success = BSG_KSFileWrite(userData, data, length);
return success ? BSG_KSJSON_OK : BSG_KSJSON_ERROR_CANNOT_ADD_DATA;
}

Expand Down Expand Up @@ -227,9 +226,13 @@ bool bsg_kscrashstate_i_saveState(const BSG_KSCrash_State *const state,
return false;
}

BSG_KSFile file;
char buffer[256];
BSG_KSFileInit(&file, fd, buffer, sizeof(buffer) / sizeof(*buffer));

BSG_KSJSONEncodeContext JSONContext;
bsg_ksjsonbeginEncode(&JSONContext, false, bsg_kscrashstate_i_addJSONData,
&fd);
&file);

int result;
if ((result = bsg_ksjsonbeginObject(&JSONContext, NULL)) != BSG_KSJSON_OK) {
Expand Down Expand Up @@ -269,6 +272,7 @@ bool bsg_kscrashstate_i_saveState(const BSG_KSCrash_State *const state,
result = bsg_ksjsonendEncode(&JSONContext);

done:
BSG_KSFileFlush(&file);
close(fd);

if (result != BSG_KSJSON_OK) {
Expand Down
16 changes: 16 additions & 0 deletions Tests/KSCrashTests/KSCrashState_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,20 @@ - (void)testBGFGCrash
XCTAssertTrue(context.crashedLastLaunch, @"");
}

- (void)testPersistence
{
NSString *file = [self.tempPath stringByAppendingPathComponent:@"state.json"];

BSG_KSCrash_State state = {0};
bsg_kscrashstate_init([file fileSystemRepresentation], &state);

id json = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:file] options:0 error:nil];
XCTAssertEqualObjects([json objectForKey:@"crashedLastLaunch"], @NO);

bsg_kscrashstate_notifyAppCrash();

json = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:file] options:0 error:nil];
XCTAssertEqualObjects([json objectForKey:@"crashedLastLaunch"], @YES);
}

@end

0 comments on commit 099c5a7

Please sign in to comment.