-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDLogger.m
337 lines (281 loc) · 8.85 KB
/
SDLogger.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2016 Sysdata Digital
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "SDLogger.h"
#import "DDLog.h"
#ifdef COCOALUMBERJACK_AVAILABLE
#import "SDDDFormatter.h"
#endif
#define kGenericModuleName @"SDLogger.Generic"
@interface SDLoggerUtils : NSObject
#ifdef COCOALUMBERJACK_AVAILABLE
+ (DDLogLevel)ddLogLevelFromSDLogLevel:(SDLogLevel)level;
+ (DDLogFlag)ddLogFlagFromSDLogLevel:(SDLogLevel)level;
#endif
@end
@implementation SDLoggerUtils
#ifdef COCOALUMBERJACK_AVAILABLE
/**
* utility method to convert SDLogLevel into corresponding DDLogLevel
*/
+ (DDLogLevel)ddLogLevelFromSDLogLevel:(SDLogLevel)level
{
switch (level)
{
case SDLogLevelVerbose:
{
return DDLogLevelVerbose;
}
case SDLogLevelInfo:
{
return DDLogLevelInfo;
}
case SDLogLevelWarning:
{
return DDLogLevelWarning;
}
case SDLogLevelError:
{
return DDLogLevelError;
}
}
}
/**
* utility to convert SDLogLevel into corresponding DDLogFlag
*/
+ (DDLogFlag)ddLogFlagFromSDLogLevel:(SDLogLevel)level
{
switch (level)
{
case SDLogLevelVerbose:
{
return DDLogFlagVerbose;
}
case SDLogLevelInfo:
{
return DDLogFlagInfo;
}
case SDLogLevelWarning:
{
return DDLogFlagWarning;
}
case SDLogLevelError:
{
return DDLogFlagError;
}
}
}
#endif
@end
@interface SDLogModuleSetting ()
/**
* keys: SDLogLevel wrapped in NSNumber; values: BOOL wrapped in NSNumber
*/
@property (nonatomic, strong) NSMutableDictionary *asyncModeByLevel;
/**
* value used by logger DDTTYLogger differentiate colors to use
*/
@property (nonatomic, assign) NSInteger context;
@end
@implementation SDLogModuleSetting
- (instancetype) initWithModuleName:(NSString*)name context:(NSInteger)context
{
self = [super init];
if (self)
{
_moduleName = name;
_context = context;
_asyncModeByLevel = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@NO, @(SDLogLevelError), @YES, @(SDLogLevelWarning), @YES, @(SDLogLevelVerbose), nil];
#if DEBUG
_logLevel = SDLogLevelVerbose;
#else
_logLevel = SDLogLevelError;
#endif
}
return self;
}
- (void)setUseAsyncLog:(BOOL)useAsync forLogLevel:(SDLogLevel)logLevel
{
[self.asyncModeByLevel setObject:@(useAsync) forKey:@(logLevel)];
}
- (BOOL)useAsyncLogForLogLevel:(SDLogLevel)logLevel
{
return [self.asyncModeByLevel[@(logLevel)] boolValue];
}
@end
@interface SDLogger ()
{
/**
* structure to keep module informations.
*
* keys: module names; values: SDLogModuleSetting
*/
NSMutableDictionary <NSString*, SDLogModuleSetting*> *settingsByModule;
}
@end
@implementation SDLogger
+ (instancetype) sharedLogger
{
static dispatch_once_t pred;
static id sharedLoggerInstance_ = nil;
dispatch_once(&pred, ^{
sharedLoggerInstance_ = [[self alloc] init];
});
return sharedLoggerInstance_;
}
- (instancetype)init
{
self = [super init];
if (self)
{
settingsByModule = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)setup
{
[self setupWithLoggers:nil];
}
- (void) setupWithLoggers:(NSArray*)loggers
{
#ifdef COCOALUMBERJACK_AVAILABLE
// default setup
if (loggers.count == 0)
{
// Formatter
SDDDFormatter *formatter = [[SDDDFormatter alloc] init];
[[DDTTYLogger sharedInstance] setLogFormatter:formatter];
// Logger to log into file
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* logsDirectory = [paths.firstObject stringByAppendingPathComponent:@"Logs"];
DDLogFileManagerDefault* logFileManagerDefault = [[DDLogFileManagerDefault alloc]initWithLogsDirectory:logsDirectory];
DDFileLogger* fileLogger = [[DDFileLogger alloc] initWithLogFileManager:logFileManagerDefault];
fileLogger.rollingFrequency = 60 * 60 * 24;// 24 hour rolling
fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
[fileLogger setLogFormatter:formatter];
loggers = @[[DDASLLogger sharedInstance], [DDTTYLogger sharedInstance], fileLogger];
}
for (id<DDLogger> logger in loggers)
{
[DDLog addLogger:logger];
}
#endif
// default levels for generic logger.
// levels should be set after DDTTYLogger creation to use XcodeColors.
#if DEBUG
self.genericLogLevel = SDLogLevelVerbose;
#else
self.genericLogLevel = SDLogLevelError;
#endif
}
- (void)setGenericLogLevel:(SDLogLevel)genericLogLevel
{
_genericLogLevel = genericLogLevel;
[self setLogLevel:genericLogLevel forModuleWithName:kGenericModuleName];
}
- (void)setLogLevel:(SDLogLevel)level forModuleWithName:(NSString *)module
{
SDLogModuleSetting *setting = settingsByModule[module];
if (!setting)
{
NSInteger context = [settingsByModule.allKeys count]+1;
setting = [[SDLogModuleSetting alloc] initWithModuleName:module context:context];
settingsByModule[module] = setting;
}
setting.logLevel = level;
}
- (SDLogLevel)logLevelForModuleWithName:(NSString *)module
{
SDLogModuleSetting *setting = settingsByModule[module];
if (setting)
{
return setting.logLevel;
}
return self.genericLogLevel;
}
- (SDLogModuleSetting *)moduleWithName:(NSString *)module
{
return settingsByModule[module];
}
#pragma mark - Log
- (void) logWithLevel:(SDLogLevel)level module:(NSString *)module file:(NSString *)file function:(NSString*)function line:(NSUInteger)line format:(NSString *)format, ...
{
// Log with CocoaLumberjack
va_list args;
if (format)
{
va_start(args, format);
[self logWithLevel:level
module:module
file:file
function:function
line:line
format:format
arguments:args];
va_end(args);
}
}
- (void) logWithLevel:(SDLogLevel)level module:(NSString *)module file:(NSString *)file function:(NSString*)function line:(NSUInteger)line format:(NSString *)format arguments:(va_list)arguments;
{
SDLogModuleSetting *setting = nil;
SDLogLevel filterLevel = self.genericLogLevel;
// In absence of module, fallback on generic module
if (module.length == 0)
{
module = kGenericModuleName;
}
setting = settingsByModule[module];
if (!setting)
{
setting = settingsByModule[kGenericModuleName];
}
filterLevel = setting.logLevel;
// log is synchronous if corresponding settings enable it or if is anble the global flag.
BOOL syncLog = (![setting useAsyncLogForLogLevel:level] || self.forceSyncLogs);
// log levels more verbose have lower values, so if log level of module is grater than log level of message this be missed.
if (level < filterLevel)
{
return;
}
// Log with CocoaLumberjack
if (format)
{
#ifdef COCOALUMBERJACK_AVAILABLE
[DDLog log:!syncLog
level:[SDLoggerUtils ddLogLevelFromSDLogLevel:level]
flag:[SDLoggerUtils ddLogFlagFromSDLogLevel:level]
context:setting.context
file:[file cStringUsingEncoding:NSUTF8StringEncoding]
function:[function cStringUsingEncoding:NSUTF8StringEncoding]
line:line
tag:nil
format:format
args:arguments];
#else
if([self.delegate respondsToSelector:@selector(logger:didReceiveLogWithLevel:syncMode:module:file:function:line:format:arguments:)])
{
[self.delegate logger:self didReceiveLogWithLevel:level syncMode:syncLog module:module file:file function:function line:line format:format arguments:arguments];
}
else
{
NSString *message = [[NSString alloc] initWithFormat:format arguments:arguments];
NSLog(@"%@",message);
}
#endif
}
}
- (void)logWithLevel:(SDLogLevel)level module:(NSString *)module file:(NSString *)file function:(NSString *)function line:(NSUInteger)line message:(NSString *)message
{
[self logWithLevel:level module:module file:file function:function line:line format:message, nil];
}
@end