This repository was archived by the owner on Feb 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMDCLog.m
More file actions
97 lines (81 loc) · 3.41 KB
/
Copy pathMDCLog.m
File metadata and controls
97 lines (81 loc) · 3.41 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
//
// MDCLog.m
// DeveloperMenu
//
// Created by Matthew Cheetham on 02/02/2014.
// Copyright (c) 2014 Matthew Cheetham. All rights reserved.
//
#import "MDCLog.h"
@implementation MDCLog
+ (MDCLog *)logWithContent:(NSString *)logContent content:(MDCLogLevel)logLevel
{
MDCLog *log = [MDCLog new];
log.logLevel = logLevel;
log.logContent = logContent;
log.logTime = [NSDate date];
//This is called to ensure that we see the logs in our console
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
asl_add_log_file(NULL, STDERR_FILENO);
});
//Print log
asl_log(NULL, NULL, logLevel, "%s", [logContent UTF8String]);
return log;
}
- (NSString *)logContentWithLevelPrefix
{
return [NSString stringWithFormat:@"<%@>: %@", [self stringForLogLevel:self.logLevel], self.logContent];
}
- (NSAttributedString *)attributedLogContentWithlevelPrefix
{
NSString *logContentString = [self logContentWithLevelPrefix];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:logContentString];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:[logContentString rangeOfString:@"<Debug>"]];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:[logContentString rangeOfString:@"<Information>"]];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:[logContentString rangeOfString:@"<Notice>"]];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:224.0f/255.0f green:221.0f/255.0f blue:29.0f/255.0f alpha:1.0] range:[logContentString rangeOfString:@"<Warning>"]];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:[logContentString rangeOfString:@"<Error>"]];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[logContentString rangeOfString:@"<Critical>"]];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:181.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0] range:[logContentString rangeOfString:@"<Alert>"]];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:[logContentString rangeOfString:@"<Emergency>"]];
return attributedString;
}
- (NSString *)stringForLogLevel:(MDCLogLevel)level
{
switch (level) {
case MDCLogLevelAlert:
return @"Alert";
break;
case MDCLogLevelCrit:
return @"Critical";
break;
case MDCLogLevelDebug:
return @"Debug";
break;
case MDCLogLevelEmerg:
return @"Emergency";
break;
case MDCLogLevelErr:
return @"Error";
break;
case MDCLogLevelInfo:
return @"Information";
break;
case MDCLogLevelNotice:
return @"Notice";
break;
case MDCLogLevelWarning:
return @"Warning";
break;
default:
return @"Unknown";
break;
}
}
- (NSString *)friendlyTimeAndDate
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
return [formatter stringFromDate:self.logTime];
}
@end