调整日志输出内容

This commit is contained in:
2019-11-02 18:49:45 +08:00
parent 22344d2d41
commit a93fc83c0c
+58 -74
View File
@@ -1,14 +1,10 @@
#import "CrashHandler.h" #import "CrashHandler.h"
//#include <libkern/OSAtomic.h>
#include <execinfo.h> #include <execinfo.h>
NSString * const UncaughtExceptionHandlerSignalExceptionName = @"UncaughtExceptionHandlerSignalExceptionName"; NSString * const UncaughtExceptionHandlerSignalExceptionName = @"UncaughtExceptionHandlerSignalExceptionName";
NSString * const UncaughtExceptionHandlerSignalKey = @"UncaughtExceptionHandlerSignalKey"; NSString * const UncaughtExceptionHandlerSignalKey = @"UncaughtExceptionHandlerSignalKey";
NSString * const UncaughtExceptionHandlerAddressesKey = @"UncaughtExceptionHandlerAddressesKey"; NSString * const UncaughtExceptionHandlerAddressesKey = @"UncaughtExceptionHandlerAddressesKey";
//volatile int32_t UncaughtExceptionCount = 0;
//const int32_t UncaughtExceptionMaximum = 10;
const NSInteger UncaughtExceptionHandlerSkipAddressCount = 4; const NSInteger UncaughtExceptionHandlerSkipAddressCount = 4;
const NSInteger UncaughtExceptionHandlerReportAddressCount = 5; const NSInteger UncaughtExceptionHandlerReportAddressCount = 5;
@@ -17,23 +13,23 @@ const NSInteger UncaughtExceptionHandlerReportAddressCount = 5;
NSDateFormatter *dateFormatter; NSDateFormatter *dateFormatter;
+ (NSArray *)backtrace { + (NSArray *)backtrace {
void* callstack[128]; void* callstack[128];
int frames = backtrace(callstack, 128); int frames = backtrace(callstack, 128);
char **strs = backtrace_symbols(callstack, frames); char **strs = backtrace_symbols(callstack, frames);
int i; int i;
NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames]; NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];
for ( for (
i = UncaughtExceptionHandlerSkipAddressCount; i = UncaughtExceptionHandlerSkipAddressCount;
i < UncaughtExceptionHandlerSkipAddressCount + i < UncaughtExceptionHandlerSkipAddressCount +
UncaughtExceptionHandlerReportAddressCount; UncaughtExceptionHandlerReportAddressCount;
i++) i++)
{ {
[backtrace addObject:[NSString stringWithUTF8String:strs[i]]]; [backtrace addObject:[NSString stringWithUTF8String:strs[i]]];
} }
free(strs); free(strs);
return backtrace; return backtrace;
} }
+ (NSString *)applicationDocumentsDirectory { + (NSString *)applicationDocumentsDirectory {
@@ -51,7 +47,7 @@ NSDateFormatter *dateFormatter;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"错误" message:@"很抱歉,程序出现异常,即将退出." preferredStyle:UIAlertControllerStyleAlert]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"错误" message:@"很抱歉,程序出现异常,即将退出." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UIAlertAction *sure =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
dismissed = YES; dismissed = YES;
}]; }];
[alertController addAction:sure]; [alertController addAction:sure];
@@ -71,10 +67,10 @@ NSDateFormatter *dateFormatter;
CFRunLoopRef runLoop = CFRunLoopGetCurrent(); CFRunLoopRef runLoop = CFRunLoopGetCurrent();
CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop); CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop);
while (!dismissed) { while (!dismissed) {
for (NSString *mode in (__bridge NSArray *)allModes) { for (NSString *mode in (__bridge NSArray *)allModes) {
CFRunLoopRunInMode((CFStringRef)mode, 0.001, false); CFRunLoopRunInMode((CFStringRef)mode, 0.001, false);
} }
} }
CFRelease(allModes); CFRelease(allModes);
@@ -100,75 +96,63 @@ NSDateFormatter *dateFormatter;
} }
- (void)saveLog:(NSException *)exception { - (void)saveLog:(NSException *)exception {
NSLog(@"崩溃日志文件=================================="); // NSLog(@"崩溃日志文件==================================");
NSArray * arr = [exception callStackSymbols]; // 异常的堆栈信息
NSString * reason = [exception reason]; // // 崩溃的原因 可以有崩溃的原因(数组越界,字典nil,调用未知方法...) NSArray *stackArray = [exception callStackSymbols];
NSString * name = [exception name]; // 出现异常的原因
NSString * url = [NSString stringWithFormat:@"exception:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arr componentsJoinedByString:@"\n"]]; NSString *reason = [exception reason];
NSString * path = [[CrashHandler applicationDocumentsDirectory] stringByAppendingPathComponent:[CrashHandler filename]]; // 异常名称
NSLog(@"崩溃日志文件:%@", path); NSString *name = [exception name];
[url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSString *exceptionInfo = [NSString stringWithFormat:@"Exception reason%@\nException name%@\nException stack%@",name, reason, stackArray];
NSLog(@"%@", exceptionInfo);
NSString * path = [[CrashHandler applicationDocumentsDirectory] stringByAppendingPathComponent:[CrashHandler filename]];
NSLog(@"崩溃日志文件:%@", path);
[exceptionInfo writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
} }
@end @end
void HandleException(NSException *exception) { void HandleException(NSException *exception) {
// int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount);
// if (exceptionCount > UncaughtExceptionMaximum) {
// return;
// }
NSArray *callStack = [CrashHandler backtrace];
NSMutableDictionary *userInfo =
[NSMutableDictionary dictionaryWithDictionary:[exception userInfo]];
[userInfo
setObject:callStack
forKey:UncaughtExceptionHandlerAddressesKey];
[[[CrashHandler alloc] init] [[[CrashHandler alloc] init]
performSelectorOnMainThread:@selector(handleException:) performSelectorOnMainThread:@selector(handleException:)
withObject: withObject: exception
[NSException waitUntilDone:YES];
exceptionWithName:[exception name]
reason:[exception reason]
userInfo:userInfo]
waitUntilDone:YES];
} }
void SignalHandler(int signal) { void SignalHandler(int signal) {
// int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount);
// if (exceptionCount > UncaughtExceptionMaximum) {
// return;
// }
NSMutableDictionary *userInfo = NSMutableDictionary *userInfo =
[NSMutableDictionary [NSMutableDictionary
dictionaryWithObject:[NSNumber numberWithInt:signal] dictionaryWithObject:[NSNumber numberWithInt:signal]
forKey:UncaughtExceptionHandlerSignalKey]; forKey:UncaughtExceptionHandlerSignalKey];
NSArray *callStack = [CrashHandler backtrace]; NSArray *callStack = [CrashHandler backtrace];
[userInfo [userInfo
setObject:callStack setObject:callStack
forKey:UncaughtExceptionHandlerAddressesKey]; forKey:UncaughtExceptionHandlerAddressesKey];
[[[CrashHandler alloc] init] [[[CrashHandler alloc] init]
performSelectorOnMainThread:@selector(handleException:) performSelectorOnMainThread:@selector(handleException:)
withObject: withObject:
[NSException [NSException
exceptionWithName:UncaughtExceptionHandlerSignalExceptionName exceptionWithName:UncaughtExceptionHandlerSignalExceptionName
reason: reason: [
[NSString stringWithFormat: NSString stringWithFormat:
NSLocalizedString(@"Signal %d was raised.", nil), NSLocalizedString(@"Signal %d was raised.", nil),
signal] signal
userInfo: ]
[NSDictionary userInfo: [
dictionaryWithObject:[NSNumber numberWithInt:signal] NSDictionary
forKey:UncaughtExceptionHandlerSignalKey]] dictionaryWithObject:[NSNumber numberWithInt:signal]
waitUntilDone:YES]; forKey:UncaughtExceptionHandlerSignalKey
]
]
waitUntilDone:YES
];
} }
void RegistUncauthExceptionHandler(void) { void RegistUncauthExceptionHandler(void) {
NSLog(@"崩溃日志文件============ install ======================"); NSLog(@"崩溃日志文件 ============ regist ======================");
dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"]; [dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
NSSetUncaughtExceptionHandler(&HandleException); NSSetUncaughtExceptionHandler(&HandleException);