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:
- #ifdef DEBUG
- # define LOG(fmt, ...) do { \
- NSString* file = [[NSString alloc] initWithFormat:@"%s", __FILE__]; \
- NSLog((@"%@(%d) " fmt), [file lastPathComponent], __LINE__, ##__VA_ARGS__); \
- [file release]; \
- } while(0)
- # define LOG_METHOD NSLog(@"%s", __func__)
- # define LOG_CMETHOD NSLog(@"%@/%@", NSStringFromClass([self class]), NSStringFromSelector(_cmd))
- # define COUNT(p) NSLog(@"%s(%d): count = %d\n", __func__, __LINE__, [p retainCount]);
- # define LOG_TRACE(x) do {printf x; putchar('\n'); fflush(stdout);} while (0)
- #else
- # define LOG(...)
- # define LOG_METHOD
- # define LOG_CMETHOD
- # define COUNT(p)
- # define LOG_TRACE(x)
- #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.
- -(Void) redirectNSLogToDocumentFolder {
- NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
- NSString * documentsDirectory = [paths objectAtIndex: 0];
- NSString * fileName = [NSString stringWithFormat: @ "% @. log", [NSDate date];
- NSString * logFilePath = [documentsDirectory stringByAppendingPathComponent: fileName];
- Freopen ([logFilePath cStringUsingEncoding: NSASCIIStringEncoding], "a +", stderr );
- }
-
- -(Void) applicationDidFinishLaunching :( UIApplication *) application {
- // Logs are saved during real machine testing.
- If ([CDeviceInfo getModelType]! = SIMULATOR ){
- [Self redirectNSLogToDocumentFolder];
- }
- .....
- }
Summary:IPhone DevelopmentTipsLogsAfter saving the content of the tutorial, I hope you can learn this article to help you!