| Index | Submit class | Submit snippet | Submission feed | List |

BSCrashNotifier

Language: Objective-C, Author: Karsten
License: Public domain

BSCrashNotifier is a bundle that allows you to be notified when your app is crashing.

use it with:

NSBundle* bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"BSCrashNotifier" ofType:@"bundle"]];
Class crashNotifierClass = [bundle principalClass];
if (crashNotifierClass)
{
[crashNotifierClass onCrashSend:@selector(weCrashed:) to:self];
}
else
{
NSLog(@"couldn't load bundle");
}

and implement the notification method like:
- (void)weCrashed:(int)signalNumber
{
NSLog(@"we crashed: %i",signalNumber);
}

BSCrashNotifier source preview

//
//  BSCrashNotifier.m
//  BSCrashNotifier
//
//  Created by Karsten Kusche on 22.09.07.
//  Copyright 2007 briksoftware.com. All rights reserved.
//

#import "BSCrashNotifier.h"

static id crashNotifier;

void crashHandler(int num)
{
  if (crashNotifier)
  {
    [crashNotifier notify:num];
  }
  // reset the old signal handler
  signal(num,0);
  raise(num);
}  

void registerSignalHander()
{
  signal(SIGBUS,crashHandler);
  signal(SIGSEGV,crashHandler);
}

@implementation BSCrashNotifier

+ (void)onCrashSend:(SEL)selector to:(id)object 
{
  crashNotifier = [[self alloc] initWithSelector: selector target: object];
}

- (id) initWithSelector:(SEL)selector target:(id)object
{
  self = [super init];
  if (self != nil) {
    target = [object retain];
    action = selector;
    registerSignalHander();
  }
  return self;
}

- (void)notify:(int)num
{
  if ([NSStringFromSelector(action) rangeOfString:@":"].length)
  {
    [target performSelector:action withObject:(id)num];
  }
  else
  {
    [target performSelector:action];
  }
}
@end

BSCrashNotifier header preview

//
//  BSCrashNotifier.h
//  BSCrashNotifier
//
//  Created by Karsten Kusche on 22.09.07.
//  Copyright 2007 briksoftware.com. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface BSCrashNotifier : NSObject {
  id target;
  SEL action;
}

+ (void)onCrashSend:(SEL)selector to:(id)object;
- (id) initWithSelector:(SEL)selector target:(id)object;
- (void)notify:(int)num;

@end
Download Archive

Compatible with:


Comments

Michele Balistreri says:

Unfortunately this is very useful :D

Name

Website

Do you hate spammers? (Answer "Yes")