forked from LogentriesCommunity/le_ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LELog.m
197 lines (154 loc) · 5.62 KB
/
LELog.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
//
// LELog.m
// lelib
//
// Created by Petr on 25/11/13.
// Copyright (c) 2013,2014 Logentries. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "LELog.h"
#import "LEBackgroundThread.h"
#import "LogFiles.h"
#import "lelib.h"
extern LEBackgroundThread* backgroundThread;
extern dispatch_queue_t le_write_queue;
extern char* le_token;
@implementation LELog
- (id)init
{
self = [super init];
if (le_init()) return nil;
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationWillEnterForegroundNotification object:nil];
le_poke();
return self;
}
- (void)dealloc
{
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)log:(NSObject*)object
{
NSString* text = nil;
if ([object respondsToSelector:@selector(leDescription)]) {
id<LELoggableObject> leLoggableObject = (id<LELoggableObject>)object;
text = [leLoggableObject leDescription];
} else if ([object isKindOfClass:[NSString class]]) {
text = (NSString*)object;
} else {
text = [object description];
}
text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@"\u2028"];
LE_DEBUG(@"%@", text);
le_write_string(text);
le_poke();
}
+ (void)log:(NSObject *)object{
[[self sharedInstance] log:object];
}
+ (LELog*)sharedInstance
{
static dispatch_once_t once;
static LELog* sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [LELog new];
});
return sharedInstance;
}
+(LELog*)sessionWithToken:(NSString*)token{
LELog * leLog = [self sharedInstance];
[leLog setToken:token];
return leLog;
}
- (void)setToken:(NSString *)token
{
le_set_token([token cStringUsingEncoding:NSUTF8StringEncoding]);
}
- (void)setDebugLogs:(BOOL)debugLogs
{
le_set_debug_logs(debugLogs);
}
- (void)setSystemLogs:(BOOL)debugLogs
{
le_set_system_logs(debugLogs);
}
- (NSString*)token
{
__block NSString* r = nil;
dispatch_sync(le_write_queue, ^{
if (!le_token || le_token[0]) {
r = nil;
} else {
r = [NSString stringWithUTF8String:le_token];
}
});
return r;
}
- (void)notificationReceived:(NSNotification*)notification
{
if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
if (self.logApplicationLifecycleNotifications) {
[self log:notification.name];
}
le_poke();
return;
}
if ([notification.name isEqualToString:UIApplicationDidBecomeActiveNotification]) {
[self log:notification.name];
return;
}
if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
[self log:notification.name];
return;
}
if ([notification.name isEqualToString:UIApplicationDidFinishLaunchingNotification]) {
[self log:notification.name];
return;
}
if ([notification.name isEqualToString:UIApplicationWillTerminateNotification]) {
[self log:notification.name];
return;
}
if ([notification.name isEqualToString:UIApplicationDidEnterBackgroundNotification]) {
[self log:notification.name];
return;
}
if ([notification.name isEqualToString:UIApplicationDidReceiveMemoryWarningNotification]) {
[self log:notification.name];
return;
}
}
- (void)registerForNotifications
{
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationDidBecomeActiveNotification object:nil];
[center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationWillResignActiveNotification object:nil];
[center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationDidFinishLaunchingNotification object:nil];
[center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationWillTerminateNotification object:nil];
[center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[center addObserver:self selector:@selector(notificationReceived:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
- (void)unregisterFromNotifications
{
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[center removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[center removeObserver:self name:UIApplicationDidFinishLaunchingNotification object:nil];
[center removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
[center removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
[center removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
- (void)setLogApplicationLifecycleNotifications:(BOOL)logApplicationLifecycleNotifications
{
@synchronized(self) {
if (logApplicationLifecycleNotifications == _logApplicationLifecycleNotifications) return;
_logApplicationLifecycleNotifications = logApplicationLifecycleNotifications;
if (logApplicationLifecycleNotifications) {
[self registerForNotifications];
} else {
[self unregisterFromNotifications];
}
}
}
@end