"Code note" Saves the log log to a file

Source: Internet
Author: User

Code:

#import "AppDelegate.h" #import "RootViewController.h" @implementation appdelegate-(BOOL) application: (uiapplication *) Application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions{Self.window = [[UIWindow alloc]    Initwithframe:[[uiscreen Mainscreen] bounds];        Override point for customization after application launch.    Rootviewcontroller *rootvc=[[rootviewcontroller Alloc]init];    Uinavigationcontroller *nav=[[uinavigationcontroller ALLOC]INITWITHROOTVIEWCONTROLLER:ROOTVC];            Self.window.rootviewcontroller=nav;    Save log log to file #ifdef Bh_debug [self redirectnslogtodocumentfolder];    #else #endif self.window.backgroundColor = [Uicolor Whitecolor];    [Self.window makekeyandvisible]; return YES;}    -(void) redirectnslogtodocumentfolder{//If the Xcode is already connected, do not output to file if (Isatty (Stdout_fileno)) {return;    } uidevice *device = [Uidevice currentdevice]; If the emulator is not saved to the file if ([[Device model] hassuffix:@ "Simulator"]) {return; }//Save the NSLog print information to the log folder under the document directory Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, N    Suserdomainmask, YES);        NSString *logdirectory = [[Paths objectatindex:0] stringbyappendingpathcomponent:@ "Log"];    Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];    BOOL 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 a new log file after each startup nsstring *datestr = [formatter stringfromdate:[nsdate date]];        NSString *logfilepath = [logdirectory stringbyappendingformat:@ "/%@.log", datestr]; Enter log into file Freopen ([LogFilePath cstringusingencoding:nsasciistringencoding], "A +", stdout);        Freopen ([LogFilePath cstringusingencoding:nsasciistringencoding], "A +", stderr); Uncaught objective-c Exception Log nssetuncaughtexceptionhandler (&uncaughtexceptionhandler);}    void Uncaughtexceptionhandler (nsexception* exception) {nsstring* name = [exception name];    nsstring* reason = [exception reason];    nsarray* symbols = [exception callstacksymbols];    When an exception occurs, the call stack nsmutablestring* strsymbols = [[Nsmutablestring alloc] init];        The string for which the call stack is spelled as an 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 = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSU    Serdomainmask, YES);        NSString *logdirectory = [[Paths objectatindex:0] stringbyappendingpathcomponent:@ "Log"];    Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; if (![ FileManager Fileexistsatpath:logdirectory]) {[FileManager CREAtedirectoryatpath:logdirectory withintermediatedirectories:yes Attributes:nil Error:nil];    } nsstring *logfilepath = [logdirectory stringbyappendingpathcomponent:@ "UncaughtException.log"];    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    [Formatter Setlocale:[[nslocale alloc] initwithlocaleidentifier:@ "ZH_CN"];    [Formatter setdateformat:@ "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\r\n ", datestr, name, Reason, Strsymbols]; Write the error log to the file if (![ FileManager Fileexistsatpath:logfilepath]) {[Crashstring writetofile:logfilepath atomically:yes encoding:NSUTF8Str    Ingencoding Error:nil];        }else{nsfilehandle *outfile = [Nsfilehandle Filehandleforwritingatpath:logfilepath];        [OutFile Seektoendoffile]; [OutFile WRITedata:[crashstring Datausingencoding:nsutf8stringencoding]];    [OutFile CloseFile]; }//Send error log to mailbox//NSString *URLSTR = [NSString stringwithformat:@ "mailto://[email protected]?subject=    Bug report &body= Thank you for your cooperation!<br><br><br> error details: <br>%@ ", crashstring];    Nsurl *url = [Nsurl urlwithstring:[urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; [[UIApplication sharedapplication] openurl:url];}

"Code note" Saves the log log to a file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.