iOS crash log collection

Keywords: iOS SDK Swift Mac

iOS crash log collection

Developing an APP user experience may seem very important. Think about how uncomfortable it is that an APP always flies away and bug s keep popping up. Users will definitely uninstall it directly. So in order to develop more robust programs, we have roughly the following methods:

  • Fully consider the differences between system versions
  • CodReview (including more security code in the code)
  • Collect and correct crash logs

Speaking of log collection, it can be said that there are really hundreds of schools of thought contending on the market now. Let's not go into details here. In fact, apple SDK provides the interface for exception capture.

Realize Abnormal Statistics by Yourself
typedef void NSUncaughtExceptionHandler(NSException *exception);

FOUNDATION_EXPORT NSUncaughtExceptionHandler * _Nullable NSGetUncaughtExceptionHandler(void);
FOUNDATION_EXPORT void NSSetUncaughtExceptionHandler(NSUncaughtExceptionHandler * _Nullable);

By defining this NSUncaughtException Handler, we can do a lot of things in case of an exception. First, we define a class HCDCrashLog.

// .h

FOUNDATION_EXPORT void defalutUncaughtExceptionHandler(NSException * exception);

@interface HCDCrashLog : NSObject

+ (void)startWithUncaughtExceptionHandler:(NSUncaughtExceptionHandler *)uncaughtExceptionHandler;

+ (NSUncaughtExceptionHandler *)getUncaughtExceptionHandler;

+ (void)throwException;

@end

//.m
@interface ExceptionInfo : NSObject

@property(readonly) NSString *file;
@property(readonly) int line;

/**
 the code causes crash. eg '[myArr objectAtIndex:outRangeIndex]'
 */
@property(readonly) NSString *code;
@end

@implementation ExceptionInfo

@end


ExceptionInfo *getExceptionInfoFromCallStackSymbols(NSArray* callStackSymbols);

void defalutUncaughtExceptionHandler(NSException *exception) {

    NSArray *callStackSymbols = [NSThread callStackSymbols];
    NSString * reason = [exception reason]; //
    NSString * name = [exception name];
    NSString * info = [NSString stringWithFormat:@"\n========Program throws an exception========\n\nname:%@\n\nreason:\n%@\n\nuserInfo:\n%@\n\ncallStackSymbols:\n%@",name,reason,[exception userInfo],[callStackSymbols componentsJoinedByString:@"\n"]];

#pragma mark - Can you locate the crash location in the form of code?

    ExceptionInfo *exceptionInfo = getExceptionInfoFromCallStackSymbols(callStackSymbols);

    DLog(@"%@\n=================%@",info,exception.userInfo);

}

ExceptionInfo *getExceptionInfoFromCallStackSymbols(NSArray* callStackSymbols)
{
    return nil;

}

@implementation HCDCrashLog

+(void)startWithUncaughtExceptionHandler:(NSUncaughtExceptionHandler *)uncaughtExceptionHandler
{
    NSSetUncaughtExceptionHandler(defalutUncaughtExceptionHandler);
}

+(NSUncaughtExceptionHandler *)getUncaughtExceptionHandler
{
    return NSGetUncaughtExceptionHandler();
}

+(void)throwException
{
    NSException *exception = [NSException exceptionWithName:@"Testing exceptions" reason:@"Artificial throwing of anomalies" userInfo:nil];
    @throw exception;
}

@end

This allows us to send information to the server or save something when an exception occurs, but the difficulty lies in how we locate more detailed information (the method of crash, which line of crash, what code caused the crash, etc.) in the source code. Here If you have friends who can think of how to implement it, you are welcome to point out (as if you can't do it, because the dSYM file is on the computer, we only have call stack information in our code). In addition, according to crash file and dSYM location, see Here ) Now that we can't locate the specific information for the time being, we have to choose some three-party platforms to make statistics (big companies have a lot of bulls, it is estimated that they do statistics by themselves).

Crashlytics crash log statistics

Speaking of tripartite log statistics, I personally think Crashlytics is the best one to use. Don't ask why, let's talk about the convenience of Crashlytics.

Crashlytics is very brain-free and simple to use, and Xiaobai can easily integrate specific steps to see. Here OC and swift projects are clearly identified, and a Mac APP is officially provided. Fabric It's very convenient to help users integrate. When you go online, the crash will be sent to you by email. Yagi is very happy.

It looks like this in the mail.


We have all the information we need, and we can use crash file and dSYM to locate it. Get rid of the bug now! ___________

Besides, I'm curious about how big companies do this. Do they use three parties? If you do it yourself, how do you locate the method from the call stack? Maybe I think the direction is wrong.

Posted by vierme on Fri, 19 Apr 2019 11:00:32 -0700