-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Doctor an optional filter (#559)
* Move CrashDoctor to filters * Enable doctor in intstallations * Fix format * Update tests * Fix format * Fix import * Remove unused variable * Address code review comments
- Loading branch information
Showing
14 changed files
with
238 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// KSCrashReportFilterDoctor.m | ||
// | ||
// Created by Karl Stenerud on 2024-09-05. | ||
// | ||
// Copyright (c) 2012 Karl Stenerud. All rights reserved. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall remain in place | ||
// in this source code. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
#import "KSCrashReportFilterDoctor.h" | ||
#import "KSCrashDoctor.h" | ||
#import "KSCrashReport.h" | ||
#import "KSCrashReportFields.h" | ||
|
||
// #define KSLogger_LocalLevel TRACE | ||
#import "KSLogger.h" | ||
|
||
@interface KSCrashReportFilterDoctor () | ||
|
||
@end | ||
|
||
@implementation KSCrashReportFilterDoctor | ||
|
||
+ (NSString *)diagnoseCrash:(NSDictionary *)crashReport | ||
{ | ||
return [[KSCrashDoctor new] diagnoseCrash:crashReport]; | ||
} | ||
|
||
- (void)filterReports:(NSArray<id<KSCrashReport>> *)reports onCompletion:(KSCrashReportFilterCompletion)onCompletion | ||
{ | ||
NSMutableArray<id<KSCrashReport>> *filteredReports = [NSMutableArray arrayWithCapacity:[reports count]]; | ||
for (KSCrashReportDictionary *report in reports) { | ||
if ([report isKindOfClass:[KSCrashReportDictionary class]] == NO) { | ||
KSLOG_ERROR(@"Unexpected non-dictionary report: %@", report); | ||
continue; | ||
} | ||
|
||
NSString *diagnose = [[self class] diagnoseCrash:report.value]; | ||
NSMutableDictionary *crashReport = [report.value mutableCopy]; | ||
if (diagnose != nil) { | ||
if (crashReport[KSCrashField_Crash] != nil) { | ||
NSMutableDictionary *crashDict = [crashReport[KSCrashField_Crash] mutableCopy]; | ||
crashDict[KSCrashField_Diagnosis] = diagnose; | ||
crashReport[KSCrashField_Crash] = crashDict; | ||
} | ||
if (crashReport[KSCrashField_RecrashReport][KSCrashField_Crash] != nil) { | ||
NSMutableDictionary *recrashReport = [crashReport[KSCrashField_RecrashReport] mutableCopy]; | ||
NSMutableDictionary *crashDict = [recrashReport[KSCrashField_Crash] mutableCopy]; | ||
crashDict[KSCrashField_Diagnosis] = diagnose; | ||
recrashReport[KSCrashField_Crash] = crashDict; | ||
crashReport[KSCrashField_RecrashReport] = recrashReport; | ||
} | ||
} | ||
|
||
[filteredReports addObject:[KSCrashReportDictionary reportWithValue:crashReport]]; | ||
} | ||
|
||
kscrash_callCompletion(onCompletion, filteredReports, nil); | ||
} | ||
|
||
@end |
46 changes: 46 additions & 0 deletions
46
Sources/KSCrashFilters/include/KSCrashReportFilterDoctor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// KSCrashReportFilterDoctor.h | ||
// | ||
// Created by Karl Stenerud on 2024-09-15. | ||
// | ||
// Copyright (c) 2012 Karl Stenerud. All rights reserved. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall remain in place | ||
// in this source code. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
#import "KSCrashReportFilter.h" | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/** | ||
* Adds an automated diagnostis section to reports. | ||
* | ||
* Input: NSDictionary | ||
* Output: NSDictionary | ||
*/ | ||
NS_SWIFT_NAME(CrashReportFilterDoctor) | ||
@interface KSCrashReportFilterDoctor : NSObject <KSCrashReportFilter> | ||
|
||
+ (NSString *)diagnoseCrash:(NSDictionary *)crashReport; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Tests/KSCrashFiltersTests/KSCrashReportFilterDoctor_Tests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#import <XCTest/XCTest.h> | ||
|
||
#import "KSCrashReport.h" | ||
#import "KSCrashReportFields.h" | ||
#import "KSCrashReportFilterDoctor.h" | ||
#import "KSTestModuleConfig.h" | ||
|
||
@interface KSCrashDoctor_Tests : XCTestCase | ||
@end | ||
|
||
@implementation KSCrashDoctor_Tests | ||
|
||
- (KSCrashReportDictionary *)_crashReportAsJSON:(NSString *)filename | ||
{ | ||
NSURL *url = [KS_TEST_MODULE_BUNDLE URLForResource:filename withExtension:@"json"]; | ||
NSData *data = [NSData dataWithContentsOfURL:url]; | ||
NSDictionary *reportDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; | ||
return [KSCrashReportDictionary reportWithValue:reportDict]; | ||
} | ||
|
||
- (KSCrashReportDictionary *)_filteredReport:(KSCrashReportDictionary *)report | ||
{ | ||
KSCrashReportDictionary *__block result = nil; | ||
KSCrashReportFilterDoctor *filter = [KSCrashReportFilterDoctor new]; | ||
[filter filterReports:@[ report ] | ||
onCompletion:^(NSArray<id<KSCrashReport>> *filteredReports, NSError *error) { | ||
result = filteredReports.firstObject; | ||
XCTAssertNil(error); | ||
}]; | ||
return result; | ||
} | ||
|
||
- (void)testGracefulTermination | ||
{ | ||
KSCrashReportDictionary *report = [self _crashReportAsJSON:@"sigterm"]; | ||
KSCrashReportDictionary *resultReport = [self _filteredReport:report]; | ||
NSString *diagnostic = resultReport.value[KSCrashField_Crash][KSCrashField_Diagnosis]; | ||
XCTAssertEqual(diagnostic, @"The OS request the app be gracefully terminated."); | ||
} | ||
|
||
- (void)testOOM | ||
{ | ||
KSCrashReportDictionary *report = [self _crashReportAsJSON:@"oom"]; | ||
KSCrashReportDictionary *resultReport = [self _filteredReport:report]; | ||
NSString *diagnostic = resultReport.value[KSCrashField_Crash][KSCrashField_Diagnosis]; | ||
XCTAssertEqual(diagnostic, @"The app was terminated due to running out of memory (OOM)."); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// KSTestModuleConfig.h | ||
// | ||
// Created by Nikolay Volosatov on 2024-09-08. | ||
// | ||
// Copyright (c) 2012 Karl Stenerud. All rights reserved. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall remain in place | ||
// in this source code. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
#ifndef KSTestModuleConfig_h | ||
#define KSTestModuleConfig_h | ||
|
||
#import <XCTest/XCTest.h> | ||
|
||
#if !defined(KS_TEST_MODULE_BUNDLE) | ||
#ifdef SWIFTPM_MODULE_BUNDLE | ||
#define KS_TEST_MODULE_BUNDLE SWIFTPM_MODULE_BUNDLE | ||
#else | ||
#define KS_TEST_MODULE_BUNDLE ([NSBundle bundleForClass:[self class]]) | ||
#endif | ||
#endif | ||
|
||
#endif // KSTestModuleConfig_h |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.