[Code Note] saves log to file and code note log
Code:
# Import "AppDelegate. h "# import" RootViewController. h "@ implementation AppDelegate-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]; // Override point for customization after application launch. rootViewController * rootVC = [[RootViewController alloc] init]; UINavigat IonController * nav = [[UINavigationController alloc] initWithRootViewController: rootVC]; self. window. rootViewController = nav; // Save the log to the file # ifdef BH_DEBUG [self redirectNSLogToDocumentFolder]; # else # endif self. window. backgroundColor = [UIColor whiteColor]; [self. window makeKeyAndVisible]; return YES;}-(void) redirectNSLogToDocumentFolder {// if Xcode debugging has been connected, it will not be output to the file if (isatty (STDOUT_FILENO) {return ;} UIDevice * device = [UIDevice currentDevice]; // if ([device model] hasSuffix: @ "Simulator"]) {return ;} // Save the NSlog print information to the NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) in the Log folder under the Document directory; NSString * logDirectory = [[paths objectAtIndex: 0] stringByAppendingPathComponent: @ "Log"]; NSFileManager * fileManager = [NSFileManager defaultManager]; BO OL fileExists = [fileManager fileExistsAtPath: logDirectory]; if (! FileExists) {[fileManager createDirectoryAtPath: logDirectory withIntermediateDirectories: YES attributes: nil error: nil];} NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier: @ "zh_CN"]; [formatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; // save NSString * dateStr = [formatter stringFromDate: [NSDate date]; NSString in a new log file after each startup * LogFilePath = [logDirectory stringByAppendingFormat: @ "/% @. log ", dateStr]; // input log to the file freopen ([logFilePath cStringUsingEncoding: Empty]," a + ", stdout); freopen ([logFilePath cStringUsingEncoding: Empty], "a +", stderr); // uncaptured Objective-C exception log handler (& UncaughtExceptionHandler);} void UncaughtExceptionHandler (NSException * exception) {NSString * name = [Exception name]; NSString * reason = [exception reason]; NSArray * symbols = [exception callStackSymbols]; // call stack NSMutableString * strSymbols = [[NSMutableString alloc] init] When an exception occurs; // splice the call stack into the string of the output log for (NSString * item in symbols) {[strSymbols appendString: item]; [strSymbols appendString: @ "\ r \ n"];} // Save the crash Log to the Log folder under the Document directory. NSArray * paths = NSSearchPathForDirectoriesInDomai Ns (NSDocumentDirectory, NSUserDomainMask, YES); NSString * logDirectory = [[paths objectAtIndex: 0] stringByAppendingPathComponent: @ "Log"]; NSFileManager * fileManager = [NSFileManager defaultManager]; if (! [FileManager fileExistsAtPath: logDirectory]) {[fileManager createDirectoryAtPath: logDirectory failed: YES attributes: nil error: nil];} NSString * logFilePath = [logDirectory failed: @ "UncaughtException. log "]; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier: @" zh_CN "]; [formatter s EtDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; NSString * dateStr = [formatter stringFromDate: [NSDate date]; NSString * crashString = [NSString stringWithFormat: @ "<-% @-> [Uncaught Exception] \ r \ nName: % @, Reason: % @ \ r \ n [Fe Symbols Start] \ r \ n % @ [Fe Symbols End] \ r \ n ", dateStr, name, reason, strSymbols]; // write the error log to the file if (! [FileManager fileExistsAtPath: logFilePath]) {[crashString writeToFile: logFilePath atomically: YES encoding: handle error: nil];} else {NSFileHandle * outFile = [NSFileHandle handle: logFilePath]; [outFile seekToEndOfFile]; [outFile writeData: [crashString dataUsingEncoding: NSUTF8StringEncoding]; [outFile closeFile];} // send the error log to the mailbox // NSString * urlStr = [NSString string WithFormat: @ "mailto: // test@163.com? Subject = bug report & body = Thank you for your cooperation! <Br> error details: <br> % @ ", crashString]; // NSURL * url = [NSURL URLWithString: [urlStr failed: NSUTF8StringEncoding]; // [[UIApplication sharedApplication] openURL: url];}