-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogManager.m
99 lines (74 loc) · 1.98 KB
/
LogManager.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
#import "LogManager.h"
#include <asl.h>
@interface ASLLogManager : LogManager
@end
@implementation ASLLogManager {
aslclient _client;
aslmsg _messageTemplate;
}
- (instancetype)init
{
self = [super init];
if (self != nil) {
self->_client = asl_open(NULL, "PreLoginAgents", 0);
assert(self->_client != NULL);
(void) asl_set_filter(self->_client, ASL_FILTER_MASK_UPTO(ASL_LEVEL_INFO));
self->_messageTemplate = asl_new(ASL_TYPE_MSG);
assert(self->_messageTemplate != NULL);
// If you set state in the message template here then it'll be included in all
// messages logged.
}
return self;
}
- (void)dealloc
{
if (self->_messageTemplate != NULL) {
asl_free(self->_messageTemplate);
}
if (self->_client != NULL) {
asl_close(self->_client);
}
}
- (void)logWithFormat:(NSString *)format, ...
{
va_list ap;
NSString * str;
va_start(ap, format);
str = [[NSString alloc] initWithFormat:format arguments:ap];
va_end(ap);
(void) asl_log(self->_client, self->_messageTemplate, ASL_LEVEL_INFO, "%s", [str UTF8String]);
}
@end
@interface NSLogManager : LogManager
- (void)logWithFormat:(NSString *)format, ...;
@end
@implementation NSLogManager
- (void)logWithFormat:(NSString *)format, ...;
{
va_list ap;
va_start(ap, format);
NSLogv(format, ap);
va_end(ap);
}
@end
@implementation LogManager
+ (LogManager *)sharedManager
{
static dispatch_once_t sOnceToken;
static LogManager * sLogManager;
dispatch_once(&sOnceToken, ^{
// Change the following to log via ASL (the default) or NSLog.
if (YES) {
sLogManager = [[ASLLogManager alloc] init];
} else {
sLogManager = [[NSLogManager alloc] init];
}
});
return sLogManager;
}
- (void)logWithFormat:(NSString *)format, ...
{
#pragma unused(format)
assert(NO);
}
@end