
- Removed YuMi/Library/ (138 MB, not tracked) - Removed YuMi/Resources/ (23 MB, not tracked) - Removed old version assets (566 files, not tracked) - Excluded Pods/, xcuserdata/ and other build artifacts - Clean repository optimized for company server deployment
207 lines
7.2 KiB
Objective-C
207 lines
7.2 KiB
Objective-C
//
|
||
// FileOptionModel.m
|
||
// GWTestSDK
|
||
//
|
||
// Created by xiaowen.chen on 16/8/29.
|
||
// Copyright © 2016年 xw.com. All rights reserved.
|
||
//
|
||
|
||
#import "BSFileOptionModel.h"
|
||
#import "BSRecordModel.h"
|
||
#import <objc/runtime.h>// 导入运行时文件
|
||
|
||
#define BS_DocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
|
||
|
||
#define BS_LogFilePath [NSString stringWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],@"GWLogFile"]
|
||
|
||
#define BS_LogZipFilePath [NSString stringWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],@"GWLogFileZip"]
|
||
|
||
@implementation BSFileOptionModel
|
||
|
||
|
||
|
||
|
||
|
||
+(void)writeToFile_RecordModel:(NSArray *)logArray stringForMatBlock:( NSString*(^)( NSString *forMatString) )forMatblock withCompletion:( NSString*(^)( NSString *logString) )block class:(Class)class1{
|
||
//日志文件夹
|
||
NSString *logFile = BS_LogFilePath;
|
||
if (![[NSFileManager defaultManager] fileExistsAtPath:logFile]) {
|
||
[[NSFileManager defaultManager] createDirectoryAtPath:logFile withIntermediateDirectories:YES attributes:nil error:nil];
|
||
} else {
|
||
|
||
NSLog(@"FileDir is exists.");
|
||
}
|
||
|
||
|
||
//日志txt文件
|
||
NSString *logTxtPath = [NSString stringWithFormat:@"%@/%@.txt", logFile,[self currentTime]];
|
||
if (![[NSFileManager defaultManager] fileExistsAtPath:logTxtPath]) {
|
||
[[NSFileManager defaultManager] createFileAtPath:logTxtPath contents:nil attributes:nil];
|
||
}
|
||
// NSLog(@"logTxtPath = %@",logTxtPath);
|
||
//写入日志头
|
||
NSError *error;
|
||
NSString *logHead = [NSString stringWithFormat:@"日志协议类型:1,写入时间:%@\n",[self currentTime2]];
|
||
BOOL flag = [logHead writeToFile:logTxtPath atomically:YES encoding:NSUTF8StringEncoding error:&error];//一般error都设置为nil,保证写入成功
|
||
if (flag) {
|
||
NSLog(@"写入文件头成功");
|
||
}
|
||
else{
|
||
NSLog(@"写入文件头失败");
|
||
}
|
||
|
||
|
||
|
||
@autoreleasepool {
|
||
NSString *str = @"类型编码:[coding] 类型:ddd 描述:[describe] 关键字:[keyword] 结果:[result] 操作编码:[optionCoding]\n";
|
||
if (forMatblock) {
|
||
str = forMatblock(str);
|
||
}
|
||
id log;
|
||
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:logTxtPath];
|
||
for (int i = 0 ; i<logArray.count; i++) {
|
||
if ([logArray[i] isKindOfClass:class1]) {
|
||
log = logArray[i];
|
||
[fileHandle seekToEndOfFile];
|
||
|
||
for (NSString *tempProperties in [self getProperties:class1]) {
|
||
NSString *tempValue = [log valueForKey:tempProperties];
|
||
if ([tempValue isKindOfClass:[NSNumber class]]) {
|
||
NSNumber *tempNumber = (NSNumber *)tempValue;
|
||
tempValue = [NSString stringWithFormat:@"%@",tempNumber];
|
||
str = [str stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"[%@]",tempProperties] withString:tempValue];
|
||
|
||
}else{
|
||
str = [str stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"[%@]",tempProperties] withString:tempValue];
|
||
|
||
}
|
||
|
||
}
|
||
|
||
if(block){
|
||
str = block(str);
|
||
}
|
||
NSLog(@"写入:%@",str);
|
||
|
||
NSData* stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
|
||
[fileHandle writeData:stringData];
|
||
}
|
||
}
|
||
[fileHandle closeFile];
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
/*
|
||
* 压缩当天产生的日志文件zip文件:
|
||
* 1:如果当天没有日志文件,返回空;
|
||
* 2:如果没有实现压缩zip的方法,返回日志的位置路径;
|
||
* 3:如果实现了压缩文件的方法,返回日志压缩后的位置路径.
|
||
*
|
||
*/
|
||
+(NSString *)compression:(void(^)( NSString *logPath))block{
|
||
//压缩
|
||
NSString *logTextPath = [NSString stringWithFormat:@"%@/%@.txt", BS_LogFilePath,[self currentTime]];
|
||
if (![[NSFileManager defaultManager] fileExistsAtPath:logTextPath]) {
|
||
//文件不存在
|
||
return nil;
|
||
}
|
||
|
||
if (![[NSFileManager defaultManager] fileExistsAtPath:BS_LogZipFilePath]) {
|
||
[[NSFileManager defaultManager] createDirectoryAtPath:BS_LogZipFilePath withIntermediateDirectories:YES attributes:nil error:nil];
|
||
}
|
||
NSString *zipPath = [NSString stringWithFormat:@"%@/%@.zip", BS_LogZipFilePath,[self currentTime]];
|
||
|
||
if (block) {
|
||
block(logTextPath);
|
||
}else{
|
||
return logTextPath;
|
||
}
|
||
return zipPath;
|
||
}
|
||
|
||
|
||
+ (instancetype)getProperties:(Class)cls{
|
||
// 获取当前类的所有属性
|
||
unsigned int count;// 记录属性个数
|
||
objc_property_t *properties = class_copyPropertyList(cls, &count);
|
||
// 遍历
|
||
NSMutableArray *mArray = [NSMutableArray array];
|
||
for (int i = 0; i < count; i++) {
|
||
// objc_property_t 属性类型
|
||
objc_property_t property = properties[i];
|
||
// 获取属性的名称 C语言字符串
|
||
const char *cName = property_getName(property);
|
||
// 转换为Objective C 字符串
|
||
NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];
|
||
[mArray addObject:name];
|
||
}
|
||
|
||
return mArray.copy;
|
||
}
|
||
|
||
//获取当前时间
|
||
+(NSString *)currentTime{
|
||
return [self getTimeStr:@"YYYY-MM-dd"];
|
||
}
|
||
|
||
//获取当前时间
|
||
+(NSString *)currentTime2{
|
||
return [self getTimeStr:@"YYYY-MM-dd HH:mm:ss:SSS"];
|
||
}
|
||
|
||
|
||
+(NSString *)getTimeStr:(NSString *)forMet{
|
||
NSDate * currentTime=[NSDate date];
|
||
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
|
||
[dateformatter setDateFormat:forMet];//
|
||
NSString *currentTimeStr=[dateformatter stringFromDate:currentTime];
|
||
return currentTimeStr;
|
||
}
|
||
|
||
|
||
|
||
#pragma mark - 删除的方法
|
||
+(void)deleteFilePathLog{
|
||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||
[self deleteFilePath:BS_LogFilePath fileManager:fileManager];//删除普通文件
|
||
[self deleteFilePath:BS_LogZipFilePath fileManager:fileManager];//删除压缩文件
|
||
}
|
||
|
||
+(void)deleteFilePath:(NSString *)path fileManager:(NSFileManager *)fileManager{
|
||
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
|
||
Boolean b = [fileManager removeItemAtPath:path error:nil];
|
||
if (b) {
|
||
NSLog(@"删除文件成功");
|
||
}else{
|
||
NSLog(@"删除文件失败");
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
-(void)deleteFile{
|
||
NSString *extension = @"m4r";
|
||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||
NSString *documentsDirectory = [paths objectAtIndex:0];
|
||
|
||
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];
|
||
NSEnumerator *e = [contents objectEnumerator];
|
||
NSString *filename;
|
||
while ((filename = [e nextObject])) {
|
||
|
||
if ([[filename pathExtension] isEqualToString:extension]) {
|
||
|
||
[fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
@end
|