系统日子打印记录
#import <Foundation/Foundation.h>//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
?
轉載于:https://www.cnblogs.com/ZhangYuGe/p/4949579.html
總結
- 上一篇: Codeforces Round #33
- 下一篇: 【ACR2015】依那西普按需维持治疗策