開發(fā)過程中,我們會在項目中添加很多日志以便調(diào)試,打包后這些日志并不需要,我們可以將這些日志屏蔽掉。
一、添加宏定義
#ifdef DEBUG
#define DLog(s,...) NSLog(@"%s LINE:%d < %@ >",__FUNCTION__, __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__]);
#define DMethod() NSLog(@"%s", __func__);
#else
#define DLog(...);
#define DMethod();
#endif
在使用時直接使用Dlog就可以在release模式去掉日志
二、使用開關(guān)日志輸出
添加日志輸出管理方法
1. 首先在.h中添加方法
#pragma mark -- 日志方法
// 設(shè)置日志輸出狀態(tài)
+ (void)setLogEnable:(BOOL)enable;
// 獲取日志輸出狀態(tài)
+ (BOOL)getLogEnable;
// 日志輸出方法
+ (void)customLogWithFunction:(const char *)function lineNumber:(int)lineNumber formatString:(NSString *)formatString;
2. 在.m文件中,設(shè)置靜態(tài)變量來存儲日志輸出狀態(tài)值
// 默認值為NO
static BOOL kLogEnable = NO;
3. 實現(xiàn)類方法
#pragma mark -- 日志方法
// 設(shè)置日志輸出狀態(tài)
+ (void)setLogEnable:(BOOL)enable {
kLogEnable = enable;
}
// 獲取日志輸出狀態(tài)
+ (BOOL)getLogEnable {
return kLogEnable;
}
// 日志輸出方法
+ (void)customLogWithFunction:(const char *)function lineNumber:(int)lineNumber formatString:(NSString *)formatString {
if ([self getLogEnable]) {
// 開啟了Log
NSLog(@"%s[%d]%@", function, lineNumber, formatString);
}
}
4. 添加宏定義
#define DLog(format,...) [STATUtils customLogWithFunction:__FUNCTION__ lineNumber:__LINE__ formatString:[NSString stringWithFormat:format, ##__VA_ARGS__]]
使用宏定義打印就可以自己控制日志輸出
5. 在使用時,實現(xiàn)控制方法
[Utils setLogEnable:YES];