/**
* MARK:把NSLog日志写入到文件中
*/
+(void)redirectNSlogToDocumentFolder{
return;
{
//模拟器不保存日志
if(TARGET_IPHONE_SIMULATOR){
return ;
}
//放入到子线程中
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSFileManager *defaultManager = [NSFileManager defaultManager];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyyMMdd"];
NSString* currentDateString = [format stringFromDate:[NSDate date]];
NSString* currentLogName = nil;
//日志文件夹
NSString* managerDir = [docPath stringByAppendingPathComponent:managerDirectory];
NSString* appLogDir = [managerDir stringByAppendingPathComponent:AppLogDirectory];
if(![defaultManager fileExistsAtPath:appLogDir]){
[defaultManager createDirectoryAtPath:appLogDir withIntermediateDirectories:YES attributes:nil error:nil];
}
NSUserDefaults *foo = [NSUserDefaults standardUserDefaults];
NSString *lName = [foo stringForKey:kCurrentLogName];
BOOL isAllowable = [self isAllowableFilePath:lName];
if (isAllowable) {
NSString* logName = lName;
//设置当前写入日志的名称
currentLogName = [logName copy];
//写入日志的文件路径
NSString *logFilePath = nil;
if(!logName || logName.length == 0){
//第一次创建日志文件
currentLogName = [NSString stringWithFormat:@"%@_1.txt",currentDateString];
}else{
//判断日志名称是否为当天的日志,如果不是则新建一个
if(![logName hasPrefix:currentDateString]){
currentLogName = [NSString stringWithFormat:@"%@_1.txt",currentDateString];
}
}
//日志写入文件路径
logFilePath = [NSString stringWithFormat:@"%@",[appLogDir stringByAppendingPathComponent:currentLogName]];
//判断文件大小 M
float fileSize = (float)[self fileSizeAtPath:logFilePath]/(1024.0*1024.0);
if (fileSize > eachAppLogMaxSize)
{
//创建新文件
NSString* newLogName = currentLogName;
//去掉.txt
newLogName = [newLogName stringByReplacingOccurrencesOfString:@".txt" withString:@""];
NSRange range = [newLogName rangeOfString:[NSString stringWithFormat:@"%@_",currentDateString]];
NSString* snString = [newLogName substringFromIndex:range.location + range.length];
currentLogName = [NSString stringWithFormat:@"%@_%ld.txt",currentDateString,[snString integerValue] + 1];
}
//日志写入文件路径
NSString *s = [currentLogName copy];
logFilePath = [appLogDir stringByAppendingPathComponent:s];
if (![defaultManager fileExistsAtPath:logFilePath]) {
//如果日志不存在,则创建文件
BOOL isAllowable = [self isAllowableFilePath:logFilePath];
if (isAllowable) {
BOOL isCreate = [defaultManager createFileAtPath:logFilePath contents:nil attributes:nil];
if (isCreate) {
NSLog(@"--成功创建日志文件--");
}
}
}
//设置默认的日志名称
[[NSUserDefaults standardUserDefaults] setObject:currentLogName forKey:kCurrentLogName];
//判断所有的文件是否大于10M,如果大于则删除最开始的文件
NSArray* array = [self queryAppLogList];
NSMutableArray* removeLogArray = [NSMutableArray arrayWithCapacity:10];
float allSize = 0.0;
if(array && array.count > 0){
for (NSUInteger i = 0; i < array.count; i++) {
APPLogModel* model = array[i];
allSize += model.fileSize / (1024.0*1024.0);
if (allSize > allAppLogMaxSize) {
[removeLogArray addObject:model];
}
}
}
//删除大于10M的文件
if (removeLogArray && removeLogArray.count > 0) {
for (APPLogModel* model in removeLogArray) {
[defaultManager removeItemAtPath:model.filePath error:nil];
}
}
// 将log输入到文件
BOOL isAllowable = [self isAllowableFilePath:logFilePath];
if (isAllowable) {
const char *bufTemp = [logFilePath cStringUsingEncoding:NSASCIIStringEncoding];
if (bufTemp) {
freopen(bufTemp, "a+", stdout);
freopen(bufTemp, "a+", stderr);
}
}
}
});
}
}
/**
* MARK:查询app日志列表
*
* @return
*/
+(NSArray*)queryAppLogList{
NSError* error;
NSFileManager* fileManager = [NSFileManager defaultManager];
//日志文件夹
NSString* managerDir = [docPath stringByAppendingPathComponent:managerDirectory];
NSString* appLogDir = [managerDir stringByAppendingPathComponent:AppLogDirectory];
NSArray* fileNames = [fileManager contentsOfDirectoryAtPath:appLogDir error:&error];
NSMutableArray* array = [NSMutableArray arrayWithCapacity:10];
if (fileNames && fileNames.count) {
APPLogModel* model = nil;
for (NSString* fName in fileNames) {
NSString* fSubffix = [fName pathExtension];
NSString* name = [fName stringByDeletingPathExtension];
if (name && name.length > 0 && fSubffix && fSubffix.length > 0) {
model = [[APPLogModel alloc]init];
NSString* filePath = [appLogDir stringByAppendingPathComponent:fName];
model.name = name;
model.subffix = fSubffix;
model.fileName = fName;
model.filePath = filePath;
model.canDelete = NO;
model.fileSize = [self fileSizeAtPath:filePath];
//获取最后的更新时间
model.updateDateStr = [self updateDateOfFilePath:filePath];
[array addObject:model];
}
}
}
//按照日期倒序
if(array && array.count > 0){
[array sortUsingComparator:^NSComparisonResult(APPLogModel* _Nonnull obj1, APPLogModel* _Nonnull obj2) {
return [obj2.name compare:obj1.name];
}];
}
return [array copy];
}
/**
* MARK:文件路径过滤方法
* @param fp 文件路径
*/
+ (BOOL)isAllowableFilePath:(NSString *)fp{
if (!fp) {
return YES;
}
if ([fp rangeOfString:@".."].location != NSNotFound || [fp rangeOfString:@"./"].location != NSNotFound) {
return NO;
}
return YES;
}
/**
* MARK:判断文件大小
*
* @param filePath 文件路径
*
* @return
*/
+ (long long) fileSizeAtPath:(NSString*) filePath
{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
/**
* MARK:查询文件的最后更新时间(yyyy-MM-dd HH:mm:ss)
*
* @return
*/
+(NSString*)updateDateOfFilePath:(NSString*)filePath{
NSString* updateDateStr = @"";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePath error:&error];
if (fileAttributes != nil) {
NSDate *fileModDate = [fileAttributes objectForKey:NSFileModificationDate];
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyyMMdd"];
updateDateStr = [formatter stringFromDate:fileModDate];
}
return updateDateStr;
}
/**
* MARK:返回所有请求的字典数组
*
* @return
*/
+(NSMutableArray *)returnRequst_Arr{
NSMutableArray * request_Arr = [[NSMutableArray alloc]init];
NSString* path = [[NSBundle mainBundle]pathForResource:@"icleanPower_Request_List" ofType:@"plist"];
NSArray* array = [NSArray arrayWithContentsOfFile:path];
for (NSUInteger i = 0; i < array.count; i++) {
NSDictionary* dic = array[i];
[request_Arr addObject:dic];
}
return request_Arr;
}
/**
* MARK:具体输出Log的格式
*/
+ (void) LogDetailedFormatWithString:(NSString *)logString andData:(id)data{
NSString * finalString = logString;
#if DEBUG //DEBUG模式下如果有data数据源也一并记录
// if (data) {
// finalString = [NSString stringWithFormat:@"%@ 详细数据:%@",logString,data];
// }
#endif
NSLog(@"%@",finalString);
}