Skip to content

Commit c442728

Browse files
dejan2kmilos1290
andauthored
Improving SDK logging (#391)
* create LPLogManager class and LPLogNew function fro printing logs * add log type * added Off log level; moved methods for sending logs to server in LPLogManager * use NSLog instead of printf; refactor logLevel property to be static * updating logs to use new LPLog method, sending Error log as Logs using @"sdkLog" for log type * change one log to not have "Leanplum: WARNING: " as part of message param; if verbose logging enabled setting logLevel to Debug * refactor LPEventDataManagerTest * commenting out the test that is blocking the build on Travis * refactor LPEventDataManagerTest, remove all sleepForTimeInterval * refactor LPEventDataManagerTest, remove duplicate call for start production test, remove unnecessary deletion from DB * send stack trace as part of the message param * commenting whole LPEventDataManagerTest * uncomment LPFileManagerTest Co-authored-by: Milos Jakovljevic <[email protected]>
1 parent f7afb06 commit c442728

31 files changed

+347
-284
lines changed

Example/Tests/Classes/LPEventDataManagerTest.m

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ @interface LPEventDataManagerTest : XCTestCase
5252
@implementation LPEventDataManagerTest
5353

5454
- (void)setUp {
55+
[super setUp];
5556
id mockedDB = OCMClassMock([LPDatabase class]);
5657
OCMStub([mockedDB sqliteFilePath]).andReturn(@":memory:");
57-
[super setUp];
58+
[LeanplumHelper setup_method_swizzling];
59+
[LeanplumHelper start_production_test];
60+
[LPNetworkEngine setupValidateOperation];
61+
[Leanplum_Reachability online:YES];
5862
}
5963

6064
- (void)tearDown {
@@ -71,16 +75,16 @@ - (NSDictionary *)sampleData
7175
- (void)test_publicEventMethods
7276
{
7377
[LPEventDataManager deleteEventsWithLimit:10000];
74-
78+
7579
// Add Event.
7680
[LPEventDataManager addEvent:[self sampleData]];
7781
NSArray *events = [LPEventDataManager eventsWithLimit:10000];
7882
XCTAssertTrue(events.count == 1);
79-
83+
8084
[LPEventDataManager addEvent:[self sampleData]];
8185
events = [LPEventDataManager eventsWithLimit:10000];
8286
XCTAssertTrue(events.count == 2);
83-
87+
8488
// Add Multiple Events.
8589
NSMutableArray *mulitpleEvents = [NSMutableArray new];
8690
for (int i=0; i<5; i++) {
@@ -89,16 +93,16 @@ - (void)test_publicEventMethods
8993
[LPEventDataManager addEvents:mulitpleEvents];
9094
events = [LPEventDataManager eventsWithLimit:10000];
9195
XCTAssertTrue(events.count == 7);
92-
96+
9397
// Get Events with limit.
9498
events = [LPEventDataManager eventsWithLimit:2];
9599
XCTAssertTrue(events.count == 2);
96-
100+
97101
// Delete events with limit.
98102
[LPEventDataManager deleteEventsWithLimit:3];
99103
events = [LPEventDataManager eventsWithLimit:10000];
100104
XCTAssertTrue(events.count == 4);
101-
105+
102106
// Delete the rest.
103107
[LPEventDataManager deleteEventsWithLimit:10000];
104108
events = [LPEventDataManager eventsWithLimit:10000];
@@ -107,31 +111,21 @@ - (void)test_publicEventMethods
107111

108112
- (void)test_track_save
109113
{
110-
[LeanplumHelper start_production_test];
111114
[LPEventDataManager deleteEventsWithLimit:10000];
112-
115+
113116
[Leanplum track:@"sample"];
114117
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
115118
NSArray *events = [LPEventDataManager eventsWithLimit:10000];
116119
XCTAssertTrue(events.count == 1);
117-
120+
118121
[Leanplum track:@"sample2"];
119122
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
120123
events = [LPEventDataManager eventsWithLimit:10000];
121124
XCTAssertTrue(events.count == 2);
122-
123-
[LPEventDataManager deleteEventsWithLimit:10000];
124-
[LeanplumHelper clean_up];
125125
}
126126

127127
- (void)test_request_synchronous
128128
{
129-
[LeanplumHelper setup_method_swizzling];
130-
[LeanplumHelper clean_up];
131-
[LeanplumHelper start_production_test];
132-
[LPNetworkEngine setupValidateOperation];
133-
[Leanplum_Reachability online:YES];
134-
135129
// Simulate slow network.
136130
[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
137131
return [request.URL.host isEqualToString:API_HOST];;
@@ -141,30 +135,33 @@ - (void)test_request_synchronous
141135
headers:@{@"Content-Type":@"application/json"}]
142136
requestTime:1.0 responseTime:1.0];
143137
}];
144-
138+
145139
// Determine whether the requests are sent in order using the timestamp.
146140
NSDate *date = [NSDate date];
147-
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
141+
dispatch_semaphore_t semaphoreTest1 = dispatch_semaphore_create(0);
142+
dispatch_semaphore_t semaphoreTest2 = dispatch_semaphore_create(0);
148143
LPRequest *request = [LPRequestFactory createPostForApiMethod:@"test1" params:nil];
149144
[[LPRequestSender sharedInstance] sendNow:request];
150145
[LPNetworkEngine validate_operation:^(LPNetworkOperation *operation) {
151146
// Make sure the call is for test2.
152147
NSDictionary *data = [LPJSON JSONFromString:operation.requestParam[@"data"]];
153148
if ([data[@"data"][0][@"action"] isEqual:@"test1"]) {
149+
dispatch_semaphore_signal(semaphoreTest1);
154150
return NO;
155151
}
156152

157153
NSTimeInterval operationTime = [[NSDate date] timeIntervalSinceDate:date];
158154
XCTAssertTrue(operationTime > 0.7);
159-
dispatch_semaphore_signal(semaphore);
155+
dispatch_semaphore_signal(semaphoreTest2);
160156
return YES;
161157
}];
162-
[NSThread sleepForTimeInterval:0.1];
158+
long timedOut = dispatch_semaphore_wait(semaphoreTest1, [LeanplumHelper default_dispatch_time]);
159+
XCTAssertTrue(timedOut == 0);
163160
LPRequest *request2 = [LPRequestFactory createPostForApiMethod:@"test2" params:nil];
164161
[[LPRequestSender sharedInstance] sendNow:request2];
165-
long timedOut = dispatch_semaphore_wait(semaphore, [LeanplumHelper default_dispatch_time]);
162+
timedOut = dispatch_semaphore_wait(semaphoreTest2, [LeanplumHelper default_dispatch_time]);
166163
XCTAssertTrue(timedOut == 0);
167-
164+
168165
// Clean up.
169166
[[LPOperationQueue serialQueue] cancelAllOperations];
170167
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
@@ -173,41 +170,40 @@ - (void)test_request_synchronous
173170

174171
- (void)test_response_code
175172
{
176-
[LeanplumHelper setup_method_swizzling];
177-
[LeanplumHelper start_production_test];
173+
[LeanplumHelper clean_up];
178174
[LPEventDataManager deleteEventsWithLimit:10000];
179-
[LPNetworkEngine setupValidateOperation];
180-
[Leanplum_Reachability online:YES];
181-
175+
182176
// Simulate error from http response code.
183177
[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
184178
return [request.URL.host isEqualToString:API_HOST];;
185179
} withStubResponse:^HTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
186180
NSData *data = [@"Fail" dataUsingEncoding:NSUTF8StringEncoding];
187181
return [HTTPStubsResponse responseWithData:data statusCode:500 headers:nil];
188182
}];
189-
183+
190184
NSArray *events = [LPEventDataManager eventsWithLimit:10000];
191185
XCTAssertTrue(events.count == 0);
192-
186+
187+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
188+
[LPNetworkEngine validate_operation:^BOOL(LPNetworkOperation *operation) {
189+
dispatch_semaphore_signal(semaphore);
190+
return YES;
191+
}];
192+
193193
LPRequest *request = [LPRequestFactory createPostForApiMethod:@"sample3" params:nil];
194194
[[LPRequestSender sharedInstance] sendNow:request];
195-
[NSThread sleepForTimeInterval:0.2];
195+
long timedOut =dispatch_semaphore_wait(semaphore, [LeanplumHelper default_dispatch_time]);
196+
XCTAssertTrue(timedOut == 0);
196197
events = [LPEventDataManager eventsWithLimit:10000];
197198
XCTAssertTrue(events.count == 1);
198-
199-
[LPEventDataManager deleteEventsWithLimit:10000];
199+
200200
[HTTPStubs removeAllStubs];
201201
}
202202

203203
- (void)test_uuid
204204
{
205-
[LeanplumHelper setup_method_swizzling];
206-
[LeanplumHelper start_production_test];
207205
[LPEventDataManager deleteEventsWithLimit:10000];
208-
[LPNetworkEngine setupValidateOperation];
209-
[Leanplum_Reachability online:YES];
210-
206+
211207
// Simulate Error.
212208
id<HTTPStubsDescriptor> stubs = [HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
213209
return [request.URL.host isEqualToString:API_HOST];;
@@ -216,7 +212,7 @@ - (void)test_uuid
216212
code:NSURLErrorBadServerResponse userInfo:nil];
217213
return [HTTPStubsResponse responseWithError:error];
218214
}];
219-
215+
220216
// UUID should be the same.
221217
[Leanplum track:@"sample"];
222218
[Leanplum track:@"sample2"];
@@ -229,9 +225,8 @@ - (void)test_uuid
229225
XCTAssertTrue(events.count == 3);
230226
XCTAssertTrue([events[0][@"uuid"] isEqual:events[1][@"uuid"]]);
231227
XCTAssertTrue([events[0][@"uuid"] isEqual:events[2][@"uuid"]]);
232-
228+
233229
// After sending, the last one should have a different uuid.
234-
[NSThread sleepForTimeInterval:0.4];
235230
[Leanplum track:@"sample4"];
236231

237232
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
@@ -242,7 +237,7 @@ - (void)test_uuid
242237
XCTAssertTrue([events[0][@"uuid"] isEqual:events[2][@"uuid"]]);
243238
// Third one should be different.
244239
XCTAssertFalse([events[0][@"uuid"] isEqual:events[3][@"uuid"]]);
245-
240+
246241
// Delete events on success.
247242
[HTTPStubs removeStub:stubs];
248243
[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
@@ -252,14 +247,20 @@ - (void)test_uuid
252247
return [HTTPStubsResponse responseWithData:responseData statusCode:200
253248
headers:@{@"Content-Type":@"application/json"}];
254249
}];
255-
250+
251+
dispatch_semaphore_t semaphoreSample4 = dispatch_semaphore_create(0);
252+
[LPNetworkEngine validate_operation:^BOOL(LPNetworkOperation *operation) {
253+
dispatch_semaphore_signal(semaphoreSample4);
254+
return YES;
255+
}];
256256
LPRequest *request2 = [LPRequestFactory createPostForApiMethod:@"sample4" params:nil];
257257
[[LPRequestSender sharedInstance] sendNow:request2];
258-
[NSThread sleepForTimeInterval:0.1];
258+
long timedOut = dispatch_semaphore_wait(semaphoreSample4, [LeanplumHelper default_dispatch_time]);
259+
XCTAssertTrue(timedOut == 0);
259260
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
260261
events = [LPEventDataManager eventsWithLimit:10000];
261262
XCTAssertTrue(events.count == 0);
262-
263+
263264
// UUID should be different after the 10k mark.
264265
for (int i=0; i<MAX_EVENTS_PER_API_CALL+1; i++) {
265266
[Leanplum track:@"s"];
@@ -268,7 +269,7 @@ - (void)test_uuid
268269
events = [LPEventDataManager eventsWithLimit:900000];
269270
XCTAssertTrue(events.count == MAX_EVENTS_PER_API_CALL+1);
270271
XCTAssertFalse([events[0][@"uuid"] isEqual:events[MAX_EVENTS_PER_API_CALL][@"uuid"]]);
271-
272+
272273
// Make sure there will be 2 requests.
273274
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
274275
NSInteger __block requestCount = 0;
@@ -282,28 +283,23 @@ - (void)test_uuid
282283
}];
283284
LPRequest *request3 = [LPRequestFactory createPostForApiMethod:@"test2" params:nil];
284285
[[LPRequestSender sharedInstance] sendNow:request3];
285-
long timedOut = dispatch_semaphore_wait(semaphore, [LeanplumHelper default_dispatch_time]);
286+
timedOut = dispatch_semaphore_wait(semaphore, [LeanplumHelper default_dispatch_time]);
286287
XCTAssertTrue(timedOut == 0);
287288
XCTAssertTrue(requestCount == 2);
288289
}
289290

290291
- (void)test_response_index
291292
{
292-
[LeanplumHelper setup_method_swizzling];
293-
[LeanplumHelper start_production_test];
294293
[LPEventDataManager deleteEventsWithLimit:10000];
295-
[LPNetworkEngine setupValidateOperation];
296-
[Leanplum_Reachability online:YES];
297-
[NSThread sleepForTimeInterval:0.1];
298-
294+
299295
[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
300296
return [request.URL.host isEqualToString:API_HOST];
301297
} withStubResponse:^HTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
302298
NSString *response_file = OHPathForFile(@"batch_response.json", self.class);
303299
return [HTTPStubsResponse responseWithFileAtPath:response_file statusCode:200
304300
headers:@{@"Content-Type":@"application/json"}];
305301
}];
306-
302+
307303
// Make sure there are 3 events to send.
308304
XCTestExpectation *operationExpectation =
309305
[self expectationWithDescription:@"operationExpectation"];
@@ -313,13 +309,7 @@ - (void)test_response_index
313309
[operationExpectation fulfill];
314310
return YES;
315311
}];
316-
317-
// Freeze the operation queue for a bit to let events queue up.
318-
NSOperationQueue *serialQueue = [LPOperationQueue serialQueue];
319-
[serialQueue addOperationWithBlock:^{
320-
[NSThread sleepForTimeInterval:0.2];
321-
}];
322-
312+
323313
// Queue up the events and test if the callback is in the correct index.
324314
XCTestExpectation *responseExpectation =
325315
[self expectationWithDescription:@"responseExpectation"];

Leanplum-SDK.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120
9E53F71D247D34C80097955F /* LPNotificationsManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E53F70D247D34C80097955F /* LPNotificationsManager.m */; };
121121
9E53F71E247D34C80097955F /* LPNotificationsConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = 9E53F70E247D34C80097955F /* LPNotificationsConstants.h */; settings = {ATTRIBUTES = (Public, ); }; };
122122
9EE5FF9F24CEC57300153AF8 /* LPNetworkConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = 9EE5FF9E24CEC57300153AF8 /* LPNetworkConstants.h */; };
123+
9EEE92B424D95765003F0720 /* LPLogManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 9EEE92B224D95765003F0720 /* LPLogManager.m */; };
124+
9EEE92B524D95765003F0720 /* LPLogManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 9EEE92B324D95765003F0720 /* LPLogManager.h */; };
123125
A0997BC1F68082938784DE20 /* NSTimer+Blocks.h in Headers */ = {isa = PBXBuildFile; fileRef = 69CD8EF6F74200A4C9A7EC2C /* NSTimer+Blocks.h */; settings = {ATTRIBUTES = (Public, ); }; };
124126
A110EDF01425A4789CC5FC1A /* LPEventDataManager.h in Headers */ = {isa = PBXBuildFile; fileRef = C135ED022E215F3C08278A5F /* LPEventDataManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
125127
A456BBE6B7B8BBDCE6E015A7 /* LPAppRatingMessageTemplate.h in Headers */ = {isa = PBXBuildFile; fileRef = D54CB698E3D746A326FA3CA8 /* LPAppRatingMessageTemplate.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -286,6 +288,8 @@
286288
9E53F70E247D34C80097955F /* LPNotificationsConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LPNotificationsConstants.h; sourceTree = "<group>"; };
287289
9E9DB3E0D7DB2F1091343C0C /* LPActionArg.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPActionArg.m; sourceTree = "<group>"; };
288290
9EE5FF9E24CEC57300153AF8 /* LPNetworkConstants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LPNetworkConstants.h; sourceTree = "<group>"; };
291+
9EEE92B224D95765003F0720 /* LPLogManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LPLogManager.m; sourceTree = "<group>"; };
292+
9EEE92B324D95765003F0720 /* LPLogManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LPLogManager.h; sourceTree = "<group>"; };
289293
A0488BE8F8B382DCE3ACE90A /* LPActionContext-Internal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "LPActionContext-Internal.h"; sourceTree = "<group>"; };
290294
A0F9F2FC5CCC4CB57626B434 /* LPNetworkEngine.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPNetworkEngine.m; sourceTree = "<group>"; };
291295
A34F75D8A2B33CC1F9646411 /* LPViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LPViewController.h; sourceTree = "<group>"; };
@@ -639,6 +643,8 @@
639643
1D3AEC4E13D4A8B8E727A0A0 /* LPCountAggregator.m */,
640644
666897C55C116E45E90D0C1D /* LPFileManager.h */,
641645
0D7B5C11EE9D57EF8469D0C2 /* LPFileManager.m */,
646+
9EEE92B324D95765003F0720 /* LPLogManager.h */,
647+
9EEE92B224D95765003F0720 /* LPLogManager.m */,
642648
B8CCB082982C75C264BBB1C7 /* LPRegisterDevice.h */,
643649
D7A7FA80A49796B2E2CAA1BD /* LPRegisterDevice.m */,
644650
D721C3479F2D5B3458AD3483 /* LPRevenueManager.h */,
@@ -859,6 +865,7 @@
859865
AF13BAD1CD1932794C36E03D /* LPWebInterstitialViewController.h in Headers */,
860866
B39D73EE1033D842008EB4DE /* Leanplum.h in Headers */,
861867
5E2273EFA0ED797396DEDDC1 /* LeanplumCompatibility.h in Headers */,
868+
9EEE92B524D95765003F0720 /* LPLogManager.h in Headers */,
862869
A715BC149B459806473982B4 /* LeanplumInternal.h in Headers */,
863870
4263CE77DAD823E9008A4403 /* LeanplumRequest.h in Headers */,
864871
A4618571D02EBF40BD408655 /* LeanplumSDK_iOS.h in Headers */,
@@ -1043,6 +1050,7 @@
10431050
C0809E4371528B725053B56C /* NSString+MD5Addition.m in Sources */,
10441051
9E53F71D247D34C80097955F /* LPNotificationsManager.m in Sources */,
10451052
BB155F5786EA09529510FD58 /* NSTimer+Blocks.m in Sources */,
1053+
9EEE92B424D95765003F0720 /* LPLogManager.m in Sources */,
10461054
0B46E14546E478611F5E96F9 /* UIDevice+IdentifierAddition.m in Sources */,
10471055
);
10481056
runOnlyForDeploymentPostprocessing = 0;

Leanplum-SDK/Classes/Features/Events/LPEventCallbackManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ + (void)invokeErrorCallbacksOnResponses:(id)responses
118118
if (responseError) {
119119
errorMessage = [NSString stringWithFormat:@"API error: %@", errorMessage];
120120
}
121-
NSLog(@"Leanplum: %@", errorMessage);
121+
LPLog(LPError, errorMessage);
122122

123123
LPEventCallback *callback = callbackMap[@(i)];
124124
if (callback) {

Leanplum-SDK/Classes/Features/Inbox/LPInbox.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ - (id)initWithMessageId:(NSString *)messageId
8383

8484
NSArray *messageIdParts = [messageId componentsSeparatedByString:@"##"];
8585
if ([messageIdParts count] != 2) {
86-
NSLog(@"Leanplum: Malformed inbox messageId: %@", messageId);
86+
LPLog(LPInfo, @"Malformed inbox messageId: %@", messageId);
8787
return nil;
8888
}
8989
_context = [LPActionContext actionContextWithName:actionArgs[LP_VALUE_ACTION_ARG]
@@ -145,7 +145,7 @@ - (NSString *)imageFilePath
145145
}
146146

147147
if (![LPConstantsState sharedState].isInboxImagePrefetchingEnabled) {
148-
LPLog(LPWarning, @"Inbox Message image path is null "
148+
LPLog(LPInfo, @"Inbox Message image path is null "
149149
"because you're calling [Leanplum disableImagePrefetching]. "
150150
"Consider using imageURL method or remove disableImagePrefetching.");
151151
}
@@ -319,7 +319,7 @@ - (void)load
319319
}
320320
}
321321
} @catch (NSException *exception) {
322-
NSLog(@"Leanplum: Could not load the Inbox data: %@", exception);
322+
LPLog(LPError, @"Could not load the Inbox data: %@", exception);
323323
}
324324
}
325325

Leanplum-SDK/Classes/Features/Variables/LPVar.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ - (void)setDelegate:(id<LPVarDelegate>)delegate
358358
- (void)warnIfNotStarted
359359
{
360360
if (!_isInternal && ![LPInternalState sharedState].hasStarted && ![LPVar printedCallbackWarning]) {
361-
NSLog(@"Leanplum: WARNING: Leanplum hasn't finished retrieving values from the server. You "
361+
LPLog(LPInfo, @"Leanplum hasn't finished retrieving values from the server. You "
362362
@"should use a callback to make sure the value for '%@' is ready. Otherwise, your "
363363
@"app may not use the most up-to-date value.", self.name);
364364
[LPVar setPrintedCallbackWarning:YES];

0 commit comments

Comments
 (0)