iOS应用程序代码日志实现及文件输出

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时并没有退出,因此不易确定准确的退出点,考虑到重定向只影响本进程的文件对应表,因此索性没有作专门的取消重定向操作。

应该不会造成问题吧。也请各位高手指点。

转载于:https://www.cnblogs.com/wawawawacool/archive/2012/03/27/2417997.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值