iOS 文件操作,记录log日志。
2016-11-10 00:00
399 查看
iOS 文件操作,记录log日志。
先把需求发一下:
上报策略
这里只介绍怎么对文件进行操作。
因为全局任何时刻都有可能会写入日志,并且有可能会存储好几个用户的日志。所以我用了单例写,并且有个属性记录下了当前操作的文件路径。那么先来写个单例。
1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
创建文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
文件写入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
计算文件的大小
1
2
3
4
5
6
1
2
3
4
5
6
删除文件
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
创建文件并写入一些信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
自定义一个线程队列,比较方便调试 。
最后是写好的文件。
先把需求发一下:
• 客户端以日志的形式,记录用户的基本信息、使用环境、使用行为等数据。
上报策略
• 程序进入后台模式时,立即上传缓存的日志文件 • 按照时间,以每小时的频率进行数据上报 • 日志文件小于30KB时,不进行上报 • 上报成功后,清除已上传成功的日志文件 • 特殊上报策略——客户端启动触发的事件,实时上报
这里只介绍怎么对文件进行操作。
因为全局任何时刻都有可能会写入日志,并且有可能会存储好几个用户的日志。所以我用了单例写,并且有个属性记录下了当前操作的文件路径。那么先来写个单例。
@property (nonatomic,strong)NSString * filePath;//当前操作的文件的路径 @property (nonatomic,strong)dispatch_queue_t concurrentWriteFileQueue;//自己创建的一个线程 - (instancetype) shareInstance{ static dispatch_once_t onceToken; static UserInfoCollection *UIC = nil; //确保创建单例只被执行一次。 dispatch_once(&onceToken, ^{ UIC = [UserInfoCollection new]; //自己建立一个线程 UIC.concurrentWriteFileQueue = dispatch_queue_create("人家是写文件的线程自定义队列", DISPATCH_QUEUE_CONCURRENT); }); return UIC; }
1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
创建文件。
(void)createFileWithUID:(NSString*)UID{ // 文件存储的路径, NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentDirectory = [paths objectAtIndex:0]; NSLog(@"%@",documentDirectory); NSFileManager *manager = [NSFileManager defaultManager]; // 路径下的所有文件名 NSArray *filePathArr = [manager contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/",documentDirectory] error:nil]; NSLog(@"arr = %@",filePathArr); // 用UID 匹配文件 如果已经有了某个用户的日志,那么就返回这个用户的文件路径,不创建新的。 for (NSString * file in filePathArr) { if ([file rangeOfString:[NSString stringWithFormat:@"%@_",UID]].location != NSNotFound) { // 已经有了文件, //UIC.filePath 当前操作的文件的路径 UIC.filePath = [NSString stringWithFormat:@"%@/%@",documentDirectory,file]; return; } } //首次创建文件名 NSString *testPath = [NSString stringWithFormat:@"%@/%@_%@_%@%@",documentDirectory,UID,@"设备号",@"时间戳",@".txt"]; BOOL ifFileExist = [manager fileExistsAtPath:testPath]; BOOL success = NO; if (ifFileExist) { NSLog(@"文件已经存在"); UIC.filePath = testPath; }else{ NSLog(@"文件不存在"); success = [manager createFileAtPath:testPath contents:nil attributes:nil]; } if (!success) { NSLog(@"创建文件不成功"); UIC.filePath = nil; // 错误判断+++ }else{ NSLog(@"创建文件成功"); UIC.filePath = testPath; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
文件写入
- (void)writeMessageToFileWithJsonStr:(NSDictionary*)dic{ if (!UIC.filePath) { NSLog(@"文件路径错误,写不进去"); return; } NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:UIC.filePath]; // 字典转JSON NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil]; NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSString *jsonStr = [json stringByAppendingString:@",\n"]; // 在文件的末尾添加内容。如果想在开始写 [file seekToFileOffset:0]; [file seekToEndOfFile]; NSData *strData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding]; [file writeData:strData ]; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
计算文件的大小
//文件大小 - (float)fileSize{ NSFileManager *manager = [NSFileManager defaultManager]; return [[manager attributesOfItemAtPath:UIC.filePath error:nil] fileSize]/(1024.0);//这里返回的单位是KB }
1
2
3
4
5
6
1
2
3
4
5
6
删除文件
- (void)deleteUserInfoFile{ dispatch_barrier_async(UIC.concurrentWriteFileQueue, ^{ NSFileManager *manager = [NSFileManager defaultManager]; BOOL deleSuccess = [manager removeItemAtPath:UIC.filePath error:nil]; if (deleSuccess) { NSLog(@"删除文件成功"); }else{ NSLog(@"删除文件不成功"); } }); }
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
创建文件并写入一些信息
- (void)userInfoWithUID:(NSString*)UID{ //添加写操作到自定义队列,当稍后执行时,这将是队列中唯一执行的条目。这个Block永远不会同时和其他Block一起在队列中执行 dispatch_barrier_async(UIC.concurrentWriteFileQueue, ^{ [UIC createFileWithUID:UID]; NSDictionary *dic = @{ @"u":@{ @"uid":UID,// @"device":@"设备号", @"brand":@"设备信息", @"os":@"设备操作系统信息", @"tag":@"客户端发布渠道", @"clienttype":@"iPhone", @"version":@"x.x.xxxx" } }; [self writeMessageToFileWithJsonStr:dic]; }); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
自定义一个线程队列,比较方便调试 。
最后是写好的文件。
相关文章推荐
- iOS 文件操作,记录log日志。
- DB2 事务日志已满 增加事务日志文件;操作不记录日志
- dbms_errlog让DML操作记录日志
- 自己写的Log日志记录类,支持文件和数据库,自动建立Log表格
- iOS 记录系统运行日志到指定文件目录
- ios 将Log日志重定向输出到文件中保存
- 删除MySQL log bin 日志操作记录
- winForm 读取,操作文件,并记录操作日志
- iOS 将崩溃Log日志重定向输出到文件中保存
- 如何清除Oracle控制文件中的无用记录,例如v$archived_log中的deleted归档日志记录
- php文件操作实现自定义日志记录
- C# 自动查找文件内容(正则使用、获取目录下所有文件、多线程、日志记录,文件操作)
- iOS将Log日志重定向输出到文件中保存
- 【小菜日志】用LINQ的DataContext.Log 属性来记录LINQ TO语句操作日志
- 如何记录文件服务器上的文件操作日志
- Linux下使用crontab来执行定时任务计划----执行每晚12点多执行移动log日志文件操作
- 关于delphi的log的class已经完成,一如既往的open 他delphi ,日志class,临界区,文件操作
- iOS 将崩溃Log日志重定向输出到文件中保存
- iOS 将Log日志输出到文件中保存
- ios 将Log日志重定向输出到文件中保存