-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLog.m
70 lines (64 loc) · 3.06 KB
/
Log.m
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Log.m
// Hachidori
//
// Created by 香風智乃 on 3/10/19.
//
#import "Log.h"
@implementation Log
void append(NSString *msg){
NSString *documentsDirectory = [Log retrieveApplicationSupportDirectory:@""];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Hachidori.log"];
NSDate *clearlogdate = [NSUserDefaults.standardUserDefaults valueForKey:@"NextLogClearDate"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]
|| !clearlogdate || (clearlogdate && [clearlogdate timeIntervalSinceNow] <= 0)){
fprintf(stderr,"Creating new log file at %s",[path UTF8String]);
[[NSData data] writeToFile:path atomically:YES];
[NSUserDefaults.standardUserDefaults setValue:[NSDate dateWithTimeIntervalSinceNow:1209600] forKey:@"NextLogClearDate"];
}
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];
[handle truncateFileAtOffset:[handle seekToEndOfFile]];
[handle writeData:[msg dataUsingEncoding:NSUTF8StringEncoding]];
[handle closeFile];
}
void _Log(NSString *prefix, const char *file, int lineNumber, const char *funcName, NSString *format,...) {
va_list ap;
va_start (ap, format);
format = [format stringByAppendingString:@"\n"];
NSDate *date = [NSDate date];
NSString *msg = [[NSString alloc] initWithFormat:[NSString stringWithFormat:@"[%@ %@] %@",[NSDateFormatter localizedStringFromDate: date
dateStyle: NSDateFormatterShortStyle
timeStyle: NSDateFormatterNoStyle], [NSDateFormatter localizedStringFromDate: date
dateStyle: NSDateFormatterNoStyle
timeStyle: NSDateFormatterShortStyle], format] arguments:ap];
va_end (ap);
fprintf(stderr,"%s%50s:%3d - %s",[prefix UTF8String], funcName, lineNumber, [msg UTF8String]);
append(msg);
}
+ (NSString *)retrieveApplicationSupportDirectory:(NSString*)append{
NSFileManager *filemanager = [NSFileManager defaultManager];
NSError *error;
NSString *bundlename = [NSBundle mainBundle].infoDictionary[@"CFBundleName"];
append = [NSString stringWithFormat:@"%@/%@", bundlename, append];
NSURL *path = [filemanager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:true error:&error];
NSString *dir = [NSString stringWithFormat:@"%@/%@",path.path,append];
if (![filemanager fileExistsAtPath:dir isDirectory:nil]) {
NSError *ferror;
bool success = [filemanager createDirectoryAtPath:dir withIntermediateDirectories:true attributes:nil error:&ferror];
if (success && ferror == nil) {
return dir;
}
return @"";
}
return dir;
}
+ (void)openLogFile {
NSString *path = [self retrieveApplicationSupportDirectory:@""];
NSFileManager *filemanger = [NSFileManager defaultManager];
NSString *fullfilenamewithpath = [NSString stringWithFormat:@"%@/%@.log",path, [NSBundle mainBundle].infoDictionary[@"CFBundleName"]];
if (![filemanger fileExistsAtPath:fullfilenamewithpath]) {
return;
}
[NSWorkspace.sharedWorkspace openFile:fullfilenamewithpath];
}
@end