#import//DmLog-----------------------------#define DmLog_TYPE_1 1 //日志打印 1:开/0:关#define __DmLOGWITHFUNCTION(s, ...) \NSLog(@"%s : %@",__FUNCTION__,[NSString stringWithFormat:(s), ##__VA_ARGS__])#if DmLog_TYPE_1#define DmLog_METHOD NSLog(@"DmLog_METHOD:%@:%@-->",[[self class] description],NSStringFromSelector(_cmd))#define DmLog(...) __DmLOGWITHFUNCTION(__VA_ARGS__)#define DmLog_2(DmLog_STR_2) DmLog_METHOD,DmLog_STR_2#else#define DmLog_METHOD#define DmLog(...)#define DmLog_2(DmLog_STR_2)#endif#define SystemLogCreate(a) [SystemLog systemLogWithContent:[NSString stringWithFormat:@"%@",a]] //将内容写入系统日志中@interface SystemLog : NSObject{ NSDate *_date; NSString *_filePath; NSString *_content;}- (id)initWithContent:(NSString *)content;+ (SystemLog *)systemLogWithContent:(NSString *)content;@end
#import "SystemLog.h"#import "CachesManger.h"#define SYSTEMLOG(a,b) [NSString stringWithFormat:@"----------------------------------------------------\n%@\n\n%@\n\n",a,b]@implementation SystemLog- (id)initWithContent:(NSString *)content{ if (self == [super init]) { [self createSystemLogFile]; [self nowDate]; _content = SYSTEMLOG(_date, content); [self saveSystemLogToTxtWithContent:_content]; } return self;}+ (SystemLog *)systemLogWithContent:(NSString *)content{ return [[self alloc] initWithContent:content];}/** * 创建systemLog.txt文件 */- (void)createSystemLogFile{// NSString *homePath = NSHomeDirectory();// NSString *systemLogPath = [homePath stringByAppendingPathComponent:@"Documents/DearMob/SystemLog"]; NSString *cachePath = [CachesManger getCachesFilePath]; NSString *filePath = [cachePath stringByAppendingPathComponent:@"systemLog.txt"]; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isExists = [fileManager fileExistsAtPath:filePath]; if (!isExists){ [fileManager createFileAtPath:filePath contents:nil attributes:nil]; }// NSFileManager *fileManager = [NSFileManager defaultManager];// BOOL isExists = [fileManager fileExistsAtPath:filePath];// if (!isExists) {// [fileManager createDirectoryAtPath:systemLogPath withIntermediateDirectories:YES attributes:nil error:nil];// [fileManager createFileAtPath:filePath contents:nil attributes:nil];// } _filePath = filePath;}/** * 获取当前时间 */- (void)nowDate{ NSDate *date = [NSDate date]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate: date]; NSDate *localeDate = [date dateByAddingTimeInterval: interval]; _date = localeDate;}/** * 保存内容至系统日志中 * * @param content 需要添加的系统日志内容 */- (void)saveSystemLogToTxtWithContent:(NSString *)content{ NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:_filePath]; [fileHandle seekToEndOfFile]; NSData *resultData = [content dataUsingEncoding:NSUTF8StringEncoding]; [fileHandle writeData:resultData]; [fileHandle closeFile];}@end