Skip to content

Commit

Permalink
refactor: Move objc categories into functions (#377)
Browse files Browse the repository at this point in the history
Reduces duplication in behavior between the categories and helper function classes
Removes all category use, fixing a development build issue for bugsnag-unity in
iOS simulators
  • Loading branch information
paulz authored and kattrali committed Jul 3, 2019
1 parent 3fba34e commit 9586f7c
Show file tree
Hide file tree
Showing 18 changed files with 247 additions and 690 deletions.
37 changes: 13 additions & 24 deletions OSX/Bugsnag.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions Source/BugsnagCollections.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ void BSGDictInsertIfNotNil(NSMutableDictionary *dict, id object,
* @param object an object or nil
*/
void BSGArrayInsertIfNotNil(NSMutableArray *array, id object);

/**
* Merge values from source dictionary with destination
*
* @param source a dictionary
* @param destination a dictionary or nil
*/
NSDictionary *BSGDictMerge(NSDictionary *source, NSDictionary *destination);
23 changes: 22 additions & 1 deletion Source/BugsnagCollections.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,25 @@ void BSGArrayInsertIfNotNil(NSMutableArray *array, id object) {
if (object) {
[array addObject:object];
}
}
}

NSDictionary *BSGDictMerge(NSDictionary *source, NSDictionary *destination) {
if ([destination count] == 0) {
return source;
}
if ([source count] == 0) {
return destination;
}

NSMutableDictionary *dict = [destination mutableCopy];
for (id key in [source allKeys]) {
id srcEntry = source[key];
id dstEntry = destination[key];
if ([dstEntry isKindOfClass:[NSDictionary class]] &&
[srcEntry isKindOfClass:[NSDictionary class]]) {
srcEntry = BSGDictMerge(srcEntry, dstEntry);
}
dict[key] = srcEntry;
}
return dict;
}
3 changes: 1 addition & 2 deletions Source/BugsnagCrashReport.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#import "BugsnagHandledState.h"
#import "BugsnagLogger.h"
#import "BugsnagKeys.h"
#import "NSDictionary+BSG_Merge.h"
#import "BugsnagKSCrashSysInfoParser.h"
#import "BugsnagSession.h"
#import "BSG_RFC3339DateTool.h"
Expand Down Expand Up @@ -522,7 +521,7 @@ - (NSDictionary *)toJson {
BSGDictSetSafeObject(event, @YES, BSGKeyIncomplete);
}

NSDictionary *device = [self.device bsg_mergedInto:self.deviceState];
NSDictionary *device = BSGDictMerge(self.device, self.deviceState);
BSGDictSetSafeObject(event, device, BSGKeyDevice);

NSMutableDictionary *appObj = [NSMutableDictionary new];
Expand Down
18 changes: 6 additions & 12 deletions Source/KSCrash/Source/KSCrash/Recording/BSG_KSCrashReportStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@

#import "BSG_KSCrashDoctor.h"
#import "BSG_KSCrashReportFields.h"
#import "BSG_KSSafeCollections.h"
#import "BSG_RFC3339DateTool.h"
#import "NSDictionary+BSG_Merge.h"
#import "BSG_KSLogger.h"
#import "BugsnagCollections.h"

static NSString *const kCrashReportSuffix = @"-CrashReport-";
#define BSG_kRecrashReportSuffix @"-RecrashReport-"
Expand Down Expand Up @@ -99,8 +98,7 @@ - (NSDictionary *)fileWithId:(NSString *)fileId {
NSMutableDictionary *fileContents = [NSMutableDictionary new];
NSMutableDictionary *recrashReport =
[self readFile:[self pathToRecrashReportWithID:fileId] error:nil];
[fileContents bsg_ksc_setObjectIfNotNil:recrashReport
forKey:@BSG_KSCrashField_RecrashReport];
BSGDictInsertIfNotNil(fileContents, recrashReport, @BSG_KSCrashField_RecrashReport);
return fileContents;
}
}
Expand All @@ -116,8 +114,7 @@ - (NSMutableDictionary *)fixupCrashReport:(NSDictionary *)report {
NSMutableDictionary *mutableReport = [report mutableCopy];
NSMutableDictionary *mutableInfo =
[report[@BSG_KSCrashField_Report] mutableCopy];
[mutableReport bsg_ksc_setObjectIfNotNil:mutableInfo
forKey:@BSG_KSCrashField_Report];
BSGDictInsertIfNotNil(mutableReport, mutableInfo, @BSG_KSCrashField_Report);

// Timestamp gets stored as a unix timestamp. Convert it to rfc3339.
[self convertTimestamp:@BSG_KSCrashField_Timestamp inReport:mutableInfo];
Expand All @@ -132,11 +129,9 @@ - (NSMutableDictionary *)fixupCrashReport:(NSDictionary *)report {

NSMutableDictionary *crashReport =
[report[@BSG_KSCrashField_Crash] mutableCopy];
[mutableReport bsg_ksc_setObjectIfNotNil:crashReport
forKey:@BSG_KSCrashField_Crash];
BSGDictInsertIfNotNil(mutableReport, crashReport, @BSG_KSCrashField_Crash);
BSG_KSCrashDoctor *doctor = [BSG_KSCrashDoctor doctor];
[crashReport bsg_ksc_setObjectIfNotNil:[doctor diagnoseCrash:report]
forKey:@BSG_KSCrashField_Diagnosis];
BSGDictInsertIfNotNil(crashReport, [doctor diagnoseCrash:report], @BSG_KSCrashField_Diagnosis);

return mutableReport;
}
Expand All @@ -160,8 +155,7 @@ - (void)mergeDictWithKey:(NSString *)srcKey
return;
}

[report bsg_ksc_setObjectIfNotNil:[srcDict bsg_mergedInto:dstDict]
forKey:dstKey];
BSGDictInsertIfNotNil(report, BSGDictMerge(srcDict, dstDict), dstKey);
[report removeObjectForKey:srcKey];
}

Expand Down
104 changes: 35 additions & 69 deletions Source/KSCrash/Source/KSCrash/Recording/BSG_KSSystemInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
#import "BSG_KSDynamicLinker.h"
#import "BSG_KSJSONCodecObjC.h"
#import "BSG_KSMach.h"
#import "BSG_KSSafeCollections.h"
#import "BSG_KSSysCtl.h"
#import "BSG_KSSystemCapabilities.h"
#import "BugsnagKeys.h"
#import "BugsnagCollections.h"
#import "BSG_KSLogger.h"

#import <CommonCrypto/CommonDigest.h>
Expand Down Expand Up @@ -363,17 +363,13 @@ + (NSDictionary *)systemInfo {
NSDictionary *infoDict = [mainBundle infoDictionary];
const struct mach_header *header = _dyld_get_image_header(0);
#ifdef __clang_version__
[sysInfo bsg_ksc_safeSetObject:@__clang_version__
forKey:@BSG_KSSystemField_ClangVersion];
BSGDictSetSafeObject(sysInfo, @__clang_version__, @BSG_KSSystemField_ClangVersion);
#endif
#if BSG_KSCRASH_HAS_UIDEVICE
[sysInfo bsg_ksc_safeSetObject:[UIDevice currentDevice].systemName
forKey:@BSG_KSSystemField_SystemName];
[sysInfo bsg_ksc_safeSetObject:[UIDevice currentDevice].systemVersion
forKey:@BSG_KSSystemField_SystemVersion];
BSGDictSetSafeObject(sysInfo, [UIDevice currentDevice].systemName, @BSG_KSSystemField_SystemName);
BSGDictSetSafeObject(sysInfo, [UIDevice currentDevice].systemVersion, @BSG_KSSystemField_SystemVersion);
#else
[sysInfo bsg_ksc_safeSetObject:@"Mac OS"
forKey:@BSG_KSSystemField_SystemName];
BSGDictSetSafeObject(sysInfo, @"Mac OS", @BSG_KSSystemField_SystemName);
NSOperatingSystemVersion version =
[NSProcessInfo processInfo].operatingSystemVersion;
NSString *systemVersion;
Expand All @@ -386,79 +382,49 @@ + (NSDictionary *)systemInfo {
stringWithFormat:@"%ld.%ld.%ld", version.majorVersion,
version.minorVersion, version.patchVersion];
}
[sysInfo bsg_ksc_safeSetObject:systemVersion
forKey:@BSG_KSSystemField_SystemVersion];
BSGDictSetSafeObject(sysInfo, systemVersion, @BSG_KSSystemField_SystemVersion);
#endif
if ([self isSimulatorBuild]) {
NSString *model = [NSProcessInfo processInfo]
.environment[BSGKeySimulatorModelId];
[sysInfo bsg_ksc_safeSetObject:model forKey:@BSG_KSSystemField_Machine];
[sysInfo bsg_ksc_safeSetObject:@"simulator"
forKey:@BSG_KSSystemField_Model];
BSGDictSetSafeObject(sysInfo, model, @BSG_KSSystemField_Machine);
BSGDictSetSafeObject(sysInfo, @"simulator", @BSG_KSSystemField_Model);
} else {
#if BSG_KSCRASH_HOST_OSX
// MacOS has the machine in the model field, and no model
[sysInfo bsg_ksc_safeSetObject:[self stringSysctl:BSGKeyHwModel]
forKey:@BSG_KSSystemField_Machine];
BSGDictSetSafeObject(sysInfo, [self stringSysctl:BSGKeyHwModel], @BSG_KSSystemField_Machine);
#else
[sysInfo bsg_ksc_safeSetObject:[self stringSysctl:BSGKeyHwMachine]
forKey:@BSG_KSSystemField_Machine];
[sysInfo bsg_ksc_safeSetObject:[self stringSysctl:BSGKeyHwModel]
forKey:@BSG_KSSystemField_Model];
BSGDictSetSafeObject(sysInfo, [self stringSysctl:BSGKeyHwMachine], @BSG_KSSystemField_Machine);
BSGDictSetSafeObject(sysInfo, [self stringSysctl:BSGKeyHwModel], @BSG_KSSystemField_Model);
#endif
}
[sysInfo bsg_ksc_safeSetObject:[self stringSysctl:@"kern.version"]
forKey:@BSG_KSSystemField_KernelVersion];
[sysInfo bsg_ksc_safeSetObject:[self osBuildVersion]
forKey:@BSG_KSSystemField_OSVersion];
[sysInfo bsg_ksc_safeSetObject:@([self isJailbroken])
forKey:@BSG_KSSystemField_Jailbroken];
[sysInfo bsg_ksc_safeSetObject:[self dateSysctl:@"kern.boottime"]
forKey:@BSG_KSSystemField_BootTime];
[sysInfo bsg_ksc_safeSetObject:[NSDate date]
forKey:@BSG_KSSystemField_AppStartTime];
[sysInfo bsg_ksc_safeSetObject:[self executablePath]
forKey:@BSG_KSSystemField_ExecutablePath];
[sysInfo bsg_ksc_safeSetObject:infoDict[BSGKeyExecutableName]
forKey:@BSG_KSSystemField_Executable];
[sysInfo bsg_ksc_safeSetObject:infoDict[@"CFBundleIdentifier"]
forKey:@BSG_KSSystemField_BundleID];
[sysInfo bsg_ksc_safeSetObject:infoDict[@"CFBundleName"]
forKey:@BSG_KSSystemField_BundleName];
[sysInfo bsg_ksc_safeSetObject:infoDict[@"CFBundleVersion"]
forKey:@BSG_KSSystemField_BundleVersion];
[sysInfo
bsg_ksc_safeSetObject:infoDict[@"CFBundleShortVersionString"]
forKey:@BSG_KSSystemField_BundleShortVersion];
[sysInfo bsg_ksc_safeSetObject:[self appUUID]
forKey:@BSG_KSSystemField_AppUUID];
[sysInfo bsg_ksc_safeSetObject:[self currentCPUArch]
forKey:@BSG_KSSystemField_CPUArch];
[sysInfo bsg_ksc_safeSetObject:[self int32Sysctl:@BSGKeyHwCputype]
forKey:@BSG_KSSystemField_CPUType];
[sysInfo bsg_ksc_safeSetObject:[self int32Sysctl:@BSGKeyHwCpusubtype]
forKey:@BSG_KSSystemField_CPUSubType];
[sysInfo bsg_ksc_safeSetObject:@(header->cputype)
forKey:@BSG_KSSystemField_BinaryCPUType];
[sysInfo bsg_ksc_safeSetObject:@(header->cpusubtype)
forKey:@BSG_KSSystemField_BinaryCPUSubType];
[sysInfo bsg_ksc_safeSetObject:[[NSTimeZone localTimeZone] abbreviation]
forKey:@BSG_KSSystemField_TimeZone];
[sysInfo bsg_ksc_safeSetObject:[NSProcessInfo processInfo].processName
forKey:@BSG_KSSystemField_ProcessName];
[sysInfo bsg_ksc_safeSetObject:@([NSProcessInfo processInfo]
.processIdentifier)
forKey:@BSG_KSSystemField_ProcessID];
[sysInfo bsg_ksc_safeSetObject:@(getppid())
forKey:@BSG_KSSystemField_ParentProcessID];
[sysInfo bsg_ksc_safeSetObject:[self deviceAndAppHash]
forKey:@BSG_KSSystemField_DeviceAppHash];
[sysInfo bsg_ksc_safeSetObject:[BSG_KSSystemInfo buildType]
forKey:@BSG_KSSystemField_BuildType];
BSGDictSetSafeObject(sysInfo, [self stringSysctl:@"kern.version"], @BSG_KSSystemField_KernelVersion);
BSGDictSetSafeObject(sysInfo, [self osBuildVersion], @BSG_KSSystemField_OSVersion);
BSGDictSetSafeObject(sysInfo, @([self isJailbroken]), @BSG_KSSystemField_Jailbroken);
BSGDictSetSafeObject(sysInfo, [self dateSysctl:@"kern.boottime"], @BSG_KSSystemField_BootTime);
BSGDictSetSafeObject(sysInfo, [NSDate date], @BSG_KSSystemField_AppStartTime);
BSGDictSetSafeObject(sysInfo, [self executablePath], @BSG_KSSystemField_ExecutablePath);
BSGDictSetSafeObject(sysInfo, infoDict[BSGKeyExecutableName], @BSG_KSSystemField_Executable);
BSGDictSetSafeObject(sysInfo, infoDict[@"CFBundleIdentifier"], @BSG_KSSystemField_BundleID);
BSGDictSetSafeObject(sysInfo, infoDict[@"CFBundleName"], @BSG_KSSystemField_BundleName);
BSGDictSetSafeObject(sysInfo, infoDict[@"CFBundleVersion"], @BSG_KSSystemField_BundleVersion);
BSGDictSetSafeObject(sysInfo, infoDict[@"CFBundleShortVersionString"], @BSG_KSSystemField_BundleShortVersion);
BSGDictSetSafeObject(sysInfo, [self appUUID], @BSG_KSSystemField_AppUUID);
BSGDictSetSafeObject(sysInfo, [self currentCPUArch], @BSG_KSSystemField_CPUArch);
BSGDictSetSafeObject(sysInfo, [self int32Sysctl:@BSGKeyHwCputype], @BSG_KSSystemField_CPUType);
BSGDictSetSafeObject(sysInfo, [self int32Sysctl:@BSGKeyHwCpusubtype], @BSG_KSSystemField_CPUSubType);
BSGDictSetSafeObject(sysInfo, @(header->cputype), @BSG_KSSystemField_BinaryCPUType);
BSGDictSetSafeObject(sysInfo, @(header->cpusubtype), @BSG_KSSystemField_BinaryCPUSubType);
BSGDictSetSafeObject(sysInfo, [[NSTimeZone localTimeZone] abbreviation], @BSG_KSSystemField_TimeZone);
BSGDictSetSafeObject(sysInfo, [NSProcessInfo processInfo].processName, @BSG_KSSystemField_ProcessName);
BSGDictSetSafeObject(sysInfo, @([NSProcessInfo processInfo].processIdentifier), @BSG_KSSystemField_ProcessID);
BSGDictSetSafeObject(sysInfo, @(getppid()), @BSG_KSSystemField_ParentProcessID);
BSGDictSetSafeObject(sysInfo, [self deviceAndAppHash], @BSG_KSSystemField_DeviceAppHash);
BSGDictSetSafeObject(sysInfo, [BSG_KSSystemInfo buildType], @BSG_KSSystemField_BuildType);

NSDictionary *memory =
@{@BSG_KSSystemField_Size: [self int64Sysctl:@"hw.memsize"]};
[sysInfo bsg_ksc_safeSetObject:memory forKey:@BSG_KSSystemField_Memory];
BSGDictSetSafeObject(sysInfo, memory, @BSG_KSSystemField_Memory);

return sysInfo;
}
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 9586f7c

Please sign in to comment.