iOS redirect log log output to a file to save

Source: Internet
Author: User
Tags log log

This article reprinted to http://dingran.iteye.com/blog/1773573

For the real machine, the log can not be saved, difficult to analyze the problem. Therefore, it is necessary to save the log to the application's Docunment directory and set it as a shared file so that the analysis can be removed.

The first is the log output, which is divided into C printf and the standard nslog output, printf prints to standard output (sedout), and NSLog to standard error (STDERR), and we need both to have them print the logs to a file.

Example:

Freopen ("Xx.log", "A +", stdout);

Freopen ("Xx.log", "A +", stderr);

Specific practices:

Save NSLog Printing information to a file in the document directory

-(void) Redirectnslogtodocumentfolder

{

Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

NSString *documentdirectory = [Paths objectatindex:0];

NSString *filename = [NSString stringwithformat:@ "Dr.log"];//Note not nsdata!

NSString *logfilepath = [Documentdirectory stringbyappendingpathcomponent:filename];

Delete the existing file first

Nsfilemanager *defaultmanager = [Nsfilemanager Defaultmanager];

[Defaultmanager Removeitematpath:logfilepath Error:nil];

Enter log into file

Freopen ([LogFilePath cstringusingencoding:nsasciistringencoding], "A +", stdout);

Freopen ([LogFilePath cstringusingencoding:nsasciistringencoding], "A +", stderr);

}

This function is to be in-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions

, this function is implemented in APPDELEGATE.M.

/*******************************************************************************/

When the computer is connected to the Mac debugging the comments off, otherwise log will only be entered into the file, but not from the Xcode monitor to see.

If it's a real machine, save it to the Drm.log file in the document directory.

Uidevice *device = [Uidevice currentdevice];

if (![ [Device model] isequaltostring:@ "IPad Simulator"]) {

Start saving log files

[Self redirectnslogtodocumentfolder];

}

/*******************************************************************************/

Finally, configure the shared folder:

Add the Uifilesharingenabled key to the application's Info.plist file and set the key value to Yes. Place the files you want to share in the documents directory of your application. Once the device is plugged into the user's computer, ITunes 9.1 displays a file sharing area in the Apps tab of the selected device. The user can then add files to the directory or move the files to the desktop computer. If the application supports file sharing, the application should be able to recognize and respond appropriately when the file is added to the documents directory. For example, an application can display the contents of a new file on the interface. Do not present a list of directories to the user and ask them what they want to do with the file.

That is, once the device is connected to the computer, you can view the shared folder of the specified application via itune and copy the files to your computer.

Reference connection:

1.http://hi.baidu.com/ios_zb_com/item/ea528f4a8b1100d8c1a59217

2.http://www.cnblogs.com/thankforyou/archive/2012/09/12/2681739.html

iOS redirect log log output to a file to save

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.