http://blog.csdn.net/yadonghaoren/article/details/8831355
1.在打印log信息的時候,我們常常不知道此Log信息來自本代碼的哪行。用NSLog函數替代可以解決這個問題“
做法:
1.1在Prefix.pch中添加如下代碼:
|
extern void LogFunction(NSString
*format, ...); #ifdef
DEBUG #define
__BASENAME__ (strrchr(__FILE__, '/') + 1) #define
NSLogAlt(...) \ LogFunction(@ "%s(%s:%05d)%@" ,
__PRETTY_FUNCTION__, __BASENAME__, __LINE__, [NSString stringWithFormat:__VA_ARGS__]) #else #define
NSLogAlt(...) #endif #define
NSLog(...) NSLogAlt(__VA_ARGS__)
|
void
LogFunction(NSString
*format, ...)
{
va_list
argList;
va_start
(argList,
format);
NSString
*str=[[NSString alloc]initWithFormat:format arguments:argList];
va_end
(argList);
#undef
NSLog
NSLog(@
"%@"
,
str);
#define
NSLog(...) NSLogAlt(__VA_ARGS__)
// [str
release];
}
2.release模式下禁用NSLog
NSLog的運行會佔用時間和設備資源,因此在Release模式下編譯的程序最好不用NSLog,而在Debug模式下將執行NSLog的全部功能
Release模式通常會定義__OPTINIZE__,Debug模式不會。將下面的代碼放在頭文件中你就可以放心的時候NSLog了、
- #ifndef __OPTIMIZE__
- # define NSLog(…) NSLog(__VA_ARGS__)
- #else
- # define NSLog(…) {}
- #endif