2013年5月9日 星期四

[iOS] print Log

Because We will need a lot of  information to debug,
so we can use NSLog to print log.

However, these log information we do not want are still being printed out after the release,
we can define like below in project-Prefix.pch:
1
2
3
4
5
6
7
8
//enable NSLogs
#define DEBUG_MODE
#ifdef DEBUG_MODE
    #define DLog(...) NSLog(__VA_ARGS__)
#else
    #define DLog(...) 
#endif
#define ALog(...) NSLog(__VA_ARGS__)
Then use DLog to replace NSLog which one you want control it will be printed or not,
use ALog to replace the one you want always be printed.

When you dont need printed DLog,
 just comment this line #define DEBUG_MODE.




---------------------- By the way below is something about NSLog's usage -----------------------

In order to track bug occurred,
We can add below at each important function:

- (IBAction)showRangeList:(id)sender {
    DLog(@"%s Enter", __func__);
}
When APP executed to this line will print:
-[MainViewController showRangeList:] Enter



And NSLog already provide format,
so we don't need code like this:

NSLog(@"%@",[NSString stringWithFormat:@"%@ %@, %@", 
       errorMsgFormat, 
       error, 
       [error userInfo]]);
just need:

NSLog(@"%@ %@, %@", 
   errorMsgFormat, 
   error, 
   [error userInfo]);
if we only code:

NSLog([NSString stringWithFormat:@"msg:%@",msg]);
Although it's no problem when execute, but there will be a "format not a string literal and no format arguments" warning.

沒有留言:

張貼留言