打印日志
在真机,或真机没连接到电脑时,可以把日志信息输出到文件
1.设置日志文件路径
- - (NSString*)logFilePath {
- //非越狱版路径
- #ifndef CRACK
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- //越狱版路径
- #else
- NSString *documentsDirectory = @"/var/root/Media/xiaodao/";
- #endif
- NSString *logDir = [documentsDirectory stringByAppendingPathComponent:@"logs"];
- BOOL isDir = YES;
- //如果logs文件夹存不存在,则创建
- if([[NSFileManager defaultManager] fileExistsAtPath:logDir isDirectory:&isDir] == NO)
- {
- [[NSFileManager defaultManager] createDirectoryAtPath:logDir withIntermediateDirectories:YES attributes:nil error:nil];
- }
- //以当天的时间为文件名,文件名后缀为.log
- NSString *fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]];
- NSString *logPath = [logDir stringByAppendingPathComponent:fileName];
- return logPath;
- }
2.开始打印日志
1 - (void)startLog { 2 freopen([[self logFilePath] cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); 3 }
freopen重定向打印
stderr为标准出错信息
3.关闭打印日志
1 - (void)finishLog { 2 fflush(stderr); 3 dup2(dup(STDERR_FILENO), STDERR_FILENO); 4 close(dup(STDERR_FILENO)); 5 }
4.清空日志文件
1 - (BOOL)deleteLogFile { 2 BOOL success = [[NSFileManager defaultManager] removeItemAtPath:[self logFilePath] error:nil]; 3 return success; 4 }