-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZKLog.m
More file actions
139 lines (120 loc) · 3.56 KB
/
ZKLog.m
File metadata and controls
139 lines (120 loc) · 3.56 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat"
//
// ZKLog.m
// ZipKit
//
// Created by Karl Moskowski on 01/04/09.
//
#import "ZKLog.h"
NSString* const ZKLogLevelKey = @"ZKLogLevel";
NSString* const ZKLogToFileKey = @"ZKLogToFile";
@implementation ZKLog
- (void) logFile:(char*) sourceFile lineNumber:(NSUInteger) lineNumber level:(NSUInteger) level format:(NSString*) format, ... {
if (level >= self.minimumLevel) {
va_list args;
va_start(args, format);
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
NSString *label = [self levelToLabel:level];
NSString *now = [self.dateFormatter stringFromDate:[NSDate date]];
if (label) {
NSString *line = [NSString stringWithFormat:@"%@ [%i] %@ %@ (%@:%u)", now, self.pid, label, message, [[NSString stringWithUTF8String:sourceFile] lastPathComponent], lineNumber];
fprintf(stderr, "%s\n", [line UTF8String]);
fflush(stderr);
}
}
return;
}
- (NSUInteger) minimumLevel {
return minimumLevel;
}
- (void) setMinimumLevel:(NSUInteger) value {
switch (value) {
case ZKLogLevelError:
case ZKLogLevelNotice:
case ZKLogLevelDebug:
case ZKLogLevelAll:
minimumLevel = value;
break;
default:
ZKLogError(@"Invalid logging level: %u. Old value %@ unchanged.", value, [self levelToLabel:self.minimumLevel]);
break;
}
return;
}
- (NSString *) levelToLabel:(NSUInteger) level {
NSString *label = nil;
switch (level) {
case ZKLogLevelError:
label = @"<ERROR->";
break;
case ZKLogLevelNotice:
label = @"<Notice>";
break;
case ZKLogLevelDebug:
label = @"<Debug->";
break;
default:
label = nil;
break;
}
return label;
}
static ZKLog *sharedInstance = nil;
+ (ZKLog *) sharedInstance {
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [self new];
}
}
return sharedInstance;
}
- (id) init {
@synchronized([self class]) {
if (sharedInstance == nil) {
if (self = [super init]) {
sharedInstance = self;
self.pid = [[NSProcessInfo processInfo] processIdentifier];
self.minimumLevel = ZKLogLevelError;
self.dateFormatter = [NSDateFormatter new];
[self.dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:ZKLogToFileKey]) {
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryFolder = [searchPaths objectAtIndex:0];
NSString *logFolder = [libraryFolder stringByAppendingPathComponent:@"Logs"];
[[NSFileManager new] createDirectoryAtPath:logFolder withIntermediateDirectories:YES attributes:nil error:nil];
self.logFilePath = [logFolder stringByAppendingPathComponent:
[[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]
stringByAppendingPathExtension:@"log"]];
freopen([self.logFilePath fileSystemRepresentation], "a+", stderr);
}
}
}
}
return sharedInstance;
}
+ (id) allocWithZone:(NSZone *) zone {
@synchronized(self) {
if (sharedInstance == nil) {
return [super allocWithZone:zone];
}
}
return sharedInstance;
}
+ (void) initialize {
[[NSUserDefaults standardUserDefaults] registerDefaults:
[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:ZKLogToFileKey]];
[super initialize];
}
- (id) copyWithZone:(NSZone *) zone {
return self;
}
- (void) dealloc {
if (self.logFilePointer)
fclose(self.logFilePointer);
}
@synthesize dateFormatter, pid, logFilePath, logFilePointer;
@dynamic minimumLevel;
@end
#pragma clang dignostic pop