-
Notifications
You must be signed in to change notification settings - Fork 3
/
MCCFeatureFormatter.m
67 lines (53 loc) · 2.03 KB
/
MCCFeatureFormatter.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
//
// MCCFeatureFormatter.m
// Tealeaves
//
// Created by Scott Little on 30/5/14.
// Copyright (c) 2014 Little Known Software. All rights reserved.
//
#import "MCCFeatureFormatter.h"
#define ERROR_TYPE @"<Err>"
#define WARN_TYPE @"<Warn>"
#define INFO_TYPE @"<Info>"
#define DEBUG_TYPE @"<Debug>"
#define VERBOSE_TYPE @""
@implementation MCC_PREFIXED_NAME(FeatureFormatter)
- (instancetype)init {
self = [super init];
if (self) {
self.dateFormatter = [NSDateFormatter new];
[self.dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; // 10.4+ style
[self.dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss.SSS"];
}
return self;
}
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
NSString *featureName = @"";
NSString *logLevelString = VERBOSE_TYPE;
// If the context is a feature formatting and there is a feature set, determine what to add
if (self.featureMappings && (logMessage->logContext & MCCFeatureFormattingContext) && (logMessage->logFlag != 0)) {
if (self.featureMappings[@(logMessage->logFlag)]) {
featureName = [NSString stringWithFormat:@" (%@)", self.featureMappings[@(logMessage->logFlag)]];
}
}
// Only put a logLevelString, if this is not a feature context
if (!(logMessage->logContext & MCCFeatureFormattingContext)) {
// Check for the level below is not there to set value
if (logMessage->logFlag & LOG_FLAG_DEBUG) {
logLevelString = DEBUG_TYPE;
}
else if (logMessage->logFlag & LOG_FLAG_ERROR) {
logLevelString = ERROR_TYPE;
}
else if (logMessage->logFlag & LOG_FLAG_INFO) {
logLevelString = INFO_TYPE;
}
else if (logMessage->logFlag & LOG_FLAG_WARN) {
logLevelString = WARN_TYPE;
}
}
NSString *fileName = [[NSString stringWithUTF8String:logMessage->file] lastPathComponent];
NSString *dateAndTime = [self.dateFormatter stringFromDate:(logMessage->timestamp)];
return [NSString stringWithFormat:@"%@ %@ [%@:%d %s]%@ | %@", dateAndTime, logLevelString, fileName, logMessage->lineNumber, logMessage->function, featureName, logMessage->logMsg];
}
@end