IPhone development tips-log saving tutorial

Source: Internet
Author: User

IPhone DevelopmentTipsLogsSaving the tutorial is the content to be introduced in this article. Most of the debugging programs are read.LogsHere I will summarize how to add and save in the iphone program.Logs.

When developing a program in Objective-C, there is a special log operation class NSLog, which will output the specified stderr to the standard error output ). We can use it in Xcode's log output window or output it to a specific file.

The following is the log macro that I often use in the program. It is managed by the DEBUG switch, that is, the log output is only allowed in the DEBUG mode:

 
 
  1. #ifdef DEBUG  
  2. #  define LOG(fmt, ...) do {                                            \  
  3.         NSString* file = [[NSString alloc] initWithFormat:@"%s", __FILE__]; \  
  4.         NSLog((@"%@(%d) " fmt), [file lastPathComponent], __LINE__, ##__VA_ARGS__); \  
  5.         [file release];                                                 \  
  6.     } while(0)  
  7. #  define LOG_METHOD NSLog(@"%s", __func__)  
  8. #  define LOG_CMETHOD NSLog(@"%@/%@", NSStringFromClass([self class]), NSStringFromSelector(_cmd))  
  9. #  define COUNT(p) NSLog(@"%s(%d): count = %d\n", __func__, __LINE__, [p retainCount]);  
  10. #  define LOG_TRACE(x) do {printf x; putchar('\n'); fflush(stdout);} while (0)  
  11. #else  
  12. #  define LOG(...)  
  13. #  define LOG_METHOD  
  14. #  define LOG_CMETHOD  
  15. #  define COUNT(p)  
  16. #  define LOG_TRACE(x)  
  17. #endif 

As you can see, in addition to the standard user-defined output, I also added a lot of useful information, such as the location of the source program file, row number, class name, function name, and so on. You can add or delete specific applications during development.

During a real machine test, you can use freopen to save the standard error output to the specified file, so that you can analyze the log file after the problem occurs.

 
 
  1. -(Void) redirectNSLogToDocumentFolder {
  2. NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
  3. NSString * documentsDirectory = [paths objectAtIndex: 0];
  4. NSString * fileName = [NSString stringWithFormat: @ "% @. log", [NSDate date];
  5. NSString * logFilePath = [documentsDirectory stringByAppendingPathComponent: fileName];
  6. Freopen ([logFilePath cStringUsingEncoding: NSASCIIStringEncoding], "a +", stderr );
  7. }
  8.  
  9. -(Void) applicationDidFinishLaunching :( UIApplication *) application {
  10. // Logs are saved during real machine testing.
  11. If ([CDeviceInfo getModelType]! = SIMULATOR ){
  12. [Self redirectNSLogToDocumentFolder];
  13. }
  14. .....
  15. }

Summary:IPhone DevelopmentTipsLogsAfter saving the content of the tutorial, I hope you can learn this article to help you!

Related Article

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.