1. 日志生成、分级;
使用AnkyHe编写的SimpleLogger类(见http://ankyhe.iteye.com/blog/365628),完成日志的分级、格式化、输出等工作:
关键代码为一系列日志格式宏,其中AnkyHe的代码中RETURN宏定义如下,在输出日志的同时,完成了函数返回。为了避免该宏的误用,提高易读性,特进行修改去掉return调用。
#define RETURN(returnValue, format, ...) [[SimpleLogger getLogger] retrn:[NSString stringWithFormat:(format), ##__VA_ARGS__] \
inFile:[[NSString stringWithUTF8String:__FILE__] lastPathComponent] \
inLine:__LINE__]; return (returnValue)
修改后的宏定义如下:
#define LOG(level, format, ...) [[SimpleLogger getLogger] log:[NSString stringWithFormat:(format), ##__VA_ARGS__] \
withLevel:level \
inFile:[[NSString stringWithUTF8String:__FILE__] lastPathComponent] \
inLine:__LINE__]
#define ENTER(format, ...) [[SimpleLogger getLogger] enter:[NSString stringWithFormat:(format), ##__VA_ARGS__] \
inFile:[[NSString stringWithUTF8String:__FILE__] lastPathComponent] \
inLine:__LINE__]
#define RETURN(format, ...) [[SimpleLogger getLogger] retrn:[NSString stringWithFormat:(format), ##__VA_ARGS__] \
inFile:[[NSString stringWithUTF8String:__FILE__] lastPathComponent] \
inLine:__LINE__];
#define INFO(format, ...) [[SimpleLogger getLogger] info:[NSString stringWithFormat:(format), ##__VA_ARGS__] \
inFile:[[NSString stringWithUTF8String:__FILE__] lastPathComponent] \
inLine:__LINE__]
#define DEBUG(format, ...) [[SimpleLogger getLogger] debug:[NSString stringWithFormat:(format), ##__VA_ARGS__] \
inFile:[[NSString stringWithUTF8String:__FILE__] lastPathComponent] \
inLine:__LINE__]
#define WARN(format, ...) [[SimpleLogger getLogger] warn:[NSString stringWithFormat:(format), ##__VA_ARGS__] \
inFile:[[NSString stringWithUTF8String:__FILE__] lastPathComponent] \
inLine:__LINE__]
#define ERROR(format, ...) [[SimpleLogger getLogger] error:[NSString stringWithFormat:(format), ##__VA_ARGS__] \
inFile:[[NSString stringWithUTF8String:__FILE__] lastPathComponent] \
inLine:__LINE__]
使用方法
ENTER(@"method name");
INFO(@"The count of array is %d", [array count]);
DEBUG(@"The persons name is %@", person.name);
ERROR(@"Err message");
RETURN(@"%d", rst); //rst就是返回值
LOG(S, @"This log is very detailed with value %d", value);
[[SimpleLogger getLogger] setLogLevelSetting:SLLS_MINOR]; //设置日志级别
2. 日志重定向:
- (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);
}
以上代码将NSLog(原输出指向stderr)输出重定向到应用程序Document目录下(date).log文件中,其中date是文件生成的时间,由于时间中有":",因此在Windows机器上查看日志时,需要重新命名文件。
可以将以上函数实现为SimpleLogger的成员函数
方法出处:http://blog.csdn.net/demigod2009/article/details/6428004
3. 可能的问题;
重定向的触发位置,选择为应用程序的入口:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
在其中调用
[[SimpleLoggergetLogger] redirectNSLogToDocumentFolder];
完成重定向。
至于取消重定向的位置,由于本人的应用程序,在用户点击Home时并没有退出,因此不易确定准确的退出点,考虑到重定向只影响本进程的文件对应表,因此索性没有作专门的取消重定向操作。
应该不会造成问题吧。也请各位高手指点。