|
|
@@ -176,4 +176,43 @@
|
|
|
|
|
|
return success;
|
|
|
}
|
|
|
+
|
|
|
+//写日志
|
|
|
++ (void)writeLogsWithMsg:(NSString *)msg
|
|
|
+{
|
|
|
+ if (![[NSFileManager defaultManager] fileExistsAtPath:kSHPath_logs]) {
|
|
|
+
|
|
|
+ [[NSFileManager defaultManager] createDirectoryAtPath:kSHPath_logs withIntermediateDirectories:YES attributes:nil error:nil];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSString *logFilePath = [kSHPath_logs stringByAppendingPathComponent:@"app.log"];
|
|
|
+
|
|
|
+ // 创建或打开文件
|
|
|
+ NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
+ if (![fileManager fileExistsAtPath:logFilePath]) {
|
|
|
+ // 如果文件不存在,则创建它
|
|
|
+ NSError *error;
|
|
|
+ if (![[NSFileManager defaultManager] createFileAtPath:logFilePath contents:nil attributes:nil]) {
|
|
|
+ NSLog(@"Unable to create file: %@", logFilePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 准备写入文件
|
|
|
+ long long endOfFile = 0;
|
|
|
+
|
|
|
+ NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:logFilePath]; // 创建文件句柄
|
|
|
+ if (fileHandle) {
|
|
|
+ endOfFile = [fileHandle seekToEndOfFile];
|
|
|
+ if (endOfFile >= 0) {
|
|
|
+ NSString *allMsg = [[NSString alloc] initWithFormat:@"%@ %@\n",[NSDate date],msg];
|
|
|
+ NSData *fileData =[allMsg dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
+ [fileHandle writeData:fileData];
|
|
|
+ }
|
|
|
+
|
|
|
+ endOfFile = [fileHandle seekToEndOfFile];
|
|
|
+ // 关闭文件句柄
|
|
|
+ [fileHandle closeFile];
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
@end
|