Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS]Crash reporting plugin #322

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions iOS/FlipperKit.podspec
Expand Up @@ -155,4 +155,13 @@ Pod::Spec.new do |spec|
ss.source_files = "iOS/Plugins/FlipperKitExamplePlugin/**/*.{h,mm}"
ss.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)\"/Headers/Private/FlipperKit/**" }
end

spec.subspec "FlipperKitCrashReporterPlugin" do |ss|
ss.header_dir = "FlipperKitCrashReporterPlugin"
ss.dependency 'FlipperKit/Core'
ss.compiler_flags = folly_compiler_flags
ss.public_header_files = 'iOS/Plugins/FlipperKitCrashReporterPlugin/FlipperKitCrashReporterPlugin.h'
ss.source_files = "iOS/Plugins/FlipperKitCrashReporterPlugin/**/*.{h,mm}"
ss.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)\"/Headers/Private/FlipperKit/**" }
end
end
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
#if FB_SONARKIT_ENABLED

#import <Foundation/Foundation.h>
#import <FlipperKit/FlipperPlugin.h>

@interface FlipperKitCrashReporterPlugin : NSObject<FlipperPlugin>
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype) sharedInstance;
@end


#endif
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
#if FB_SONARKIT_ENABLED
#import "FlipperKitCrashReporterPlugin.h"
#import <FlipperKit/FlipperConnection.h>

@interface FlipperKitCrashReporterPlugin()
@property (strong, nonatomic) id<FlipperConnection> connection;
- (void) handleException:(NSException *)exception;

@end

void flipperkitUncaughtExceptionHandler(NSException *exception) {
NSLog(@"CRASH: %@", exception);
NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
[[FlipperKitCrashReporterPlugin sharedInstance] handleException:exception];
}

@implementation FlipperKitCrashReporterPlugin

- (instancetype)init {
if (self = [super init]) {
_connection = nil;
}
return self;
}

+ (instancetype)sharedInstance {
static FlipperKitCrashReporterPlugin *sInstance = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sInstance = [FlipperKitCrashReporterPlugin new];
});

return sInstance;
}

- (NSString *)identifier {
return @"CrashReporter";
}

- (void) handleException:(NSException *)exception {
// TODO: Rather than having indirection from c function, somehow pass objective c selectors as a c function pointer to NSSetUncaughtExceptionHandler
[self.connection send:@"crash-report" withParams:@{@"callstack": [exception callStackSymbols]}];

}
- (void)didConnect:(id<FlipperConnection>)connection {
self.connection = connection;
NSSetUncaughtExceptionHandler(&flipperkitUncaughtExceptionHandler);
}

- (void)didDisconnect {
self.connection = nil;
NSSetUncaughtExceptionHandler(nullptr);
}

- (BOOL)runInBackground {
return YES;
}

@end
#endif
11 changes: 10 additions & 1 deletion iOS/Sample/AppDelegate.mm
Expand Up @@ -6,7 +6,7 @@
*
*/
#import "AppDelegate.h"

#import <FlipperKitCrashReporterPlugin/FlipperKitCrashReporterPlugin.h>
#import <FlipperKit/FlipperClient.h>
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
Expand All @@ -27,6 +27,12 @@ @implementation AppDelegate {
UIWindow *_window;
}

//void uncaughtExceptionHandler(NSException *exception) {
// NSLog(@"CRASH: %@", exception);
// NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
// // Internal error reporting
//}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
Expand All @@ -38,6 +44,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
withDescriptorMapper: layoutDescriptorMapper]];

[client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
[client addPlugin:[FlipperKitCrashReporterPlugin sharedInstance]];

[[FlipperClient sharedClient] addPlugin: [[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
[client addPlugin:[FlipperKitExamplePlugin sharedInstance]];
Expand All @@ -54,6 +61,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[_window makeKeyAndVisible];

NSLog(@"Hello from Flipper in an Objc app!");
// NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); // Normal launch stuff

return YES;
}

Expand Down
2 changes: 2 additions & 0 deletions iOS/Sample/NetworkViewController.m
Expand Up @@ -17,6 +17,8 @@ @implementation NetworkViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Network";
NSArray *array = @[];
[array objectAtIndex:4];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is added deliberately to cause a crash. I will remove it once I am done with my final changes.

}

- (IBAction)tappedGithubLitho:(UIButton *)sender {
Expand Down
1 change: 1 addition & 0 deletions iOS/Sample/Podfile
Expand Up @@ -10,6 +10,7 @@ target 'Sample' do
pod 'FlipperKit/SKIOSNetworkPlugin', :path => '../../FlipperKit.podspec'
pod 'FlipperKit/FlipperKitUserDefaultsPlugin', :path => '../../FlipperKit.podspec'
pod 'FlipperKit/FlipperKitExamplePlugin', :path => '../../FlipperKit.podspec'
pod 'FlipperKit/FlipperKitCrashReporterPlugin', :path => '../../FlipperKit.podspec'
pod 'Flipper', :path => '../../Flipper.podspec'
post_install do |installer|

Expand Down