Skip to content

Commit b9a27c3

Browse files
committed
Created BranchLogger
1 parent 18cc5aa commit b9a27c3

File tree

9 files changed

+279
-28
lines changed

9 files changed

+279
-28
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//
2+
// BranchLoggerTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Nipun Singh on 2/5/24.
6+
// Copyright © 2024 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "BranchLogger.h"
11+
#import "Branch.h"
12+
13+
@interface BranchLoggerTests : XCTestCase
14+
@end
15+
16+
@implementation BranchLoggerTests
17+
18+
- (void)setUp {
19+
[super setUp];
20+
[BranchLogger shared].loggingEnabled = NO;
21+
[BranchLogger shared].logCallback = nil;
22+
}
23+
24+
- (void)tearDown {
25+
[BranchLogger shared].loggingEnabled = NO;
26+
[BranchLogger shared].logCallback = nil;
27+
[super tearDown];
28+
}
29+
30+
- (void)testEnableLoggingSetsCorrectDefaultLevel {
31+
[[Branch getInstance] enableLogging];
32+
XCTAssertEqual([BranchLogger shared].logLevelThreshold, BranchLogLevelDebug, "Default log level should be Debug.");
33+
}
34+
35+
- (void)testLogLevelThresholdBlocksLowerLevels {
36+
[[Branch getInstance] enableLoggingAtLevel:BranchLogLevelDebug];
37+
XCTestExpectation *expectation = [self expectationWithDescription:@"Log callback expectation for message that should pass the threshold"];
38+
39+
[BranchLogger shared].logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
40+
if ([message isEqualToString:@"[BranchSDK][Debug] This message should trigger the log callback."] && logLevel >= BranchLogLevelDebug) {
41+
[expectation fulfill];
42+
}
43+
};
44+
45+
[[BranchLogger shared] logVerbose:@"This verbose message should not trigger the log callback."];
46+
[[BranchLogger shared] logDebug:@"This message should trigger the log callback."];
47+
48+
[self waitForExpectationsWithTimeout:1 handler:nil];
49+
}
50+
51+
- (void)testLogCallbackExecutesWithCorrectParameters {
52+
XCTestExpectation *expectation = [self expectationWithDescription:@"Log callback expectation"];
53+
NSString *expectedMessage = @"[BranchSDK][Info] Test message";
54+
BranchLogLevel expectedLevel = BranchLogLevelInfo;
55+
56+
[BranchLogger shared].logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
57+
XCTAssertEqualObjects(message, expectedMessage, "Logged message does not match expected message.");
58+
XCTAssertEqual(logLevel, expectedLevel, "Logged level does not match expected level.");
59+
XCTAssertNil(error, "Error should be nil.");
60+
[expectation fulfill];
61+
};
62+
63+
[[Branch getInstance] enableLoggingAtLevel:BranchLogLevelInfo];
64+
[[BranchLogger shared] logInfo:@"Test message"];
65+
66+
[self waitForExpectationsWithTimeout:1 handler:nil];
67+
}
68+
69+
- (void)testLogLevelSpecificityFiltersLowerLevels {
70+
[[Branch getInstance] enableLoggingAtLevel:BranchLogLevelWarning];
71+
72+
XCTestExpectation *verboseExpectation = [self expectationWithDescription:@"Verbose log callback"];
73+
verboseExpectation.inverted = YES;
74+
XCTestExpectation *errorExpectation = [self expectationWithDescription:@"Error log callback"];
75+
76+
__block NSUInteger callbackCount = 0;
77+
[BranchLogger shared].logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
78+
if (logLevel == BranchLogLevelVerbose) {
79+
[verboseExpectation fulfill];
80+
} else if (logLevel == BranchLogLevelError) {
81+
[errorExpectation fulfill];
82+
}
83+
callbackCount++;
84+
};
85+
86+
[[BranchLogger shared] logVerbose:@"This should not be logged due to log level threshold."];
87+
[[BranchLogger shared] logError:@"This should be logged" error:nil];
88+
89+
[self waitForExpectations:@[verboseExpectation, errorExpectation] timeout:2];
90+
XCTAssertEqual(callbackCount, 1, "Only one log callback should have been invoked.");
91+
}
92+
93+
- (void)testErrorLoggingIncludesErrorDetails {
94+
[[Branch getInstance] enableLogging];
95+
XCTestExpectation *expectation = [self expectationWithDescription:@"Error log includes error details"];
96+
97+
NSError *testError = [NSError errorWithDomain:@"TestDomain" code:42 userInfo:@{NSLocalizedDescriptionKey: @"Test error description"}];
98+
[BranchLogger shared].logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
99+
if ([message containsString:@"Test error description"] && error == testError) {
100+
[expectation fulfill];
101+
}
102+
};
103+
104+
[[BranchLogger shared] logError:@"Testing error logging" error:testError];
105+
106+
[self waitForExpectationsWithTimeout:1 handler:nil];
107+
}
108+
109+
110+
@end

Branch-TestBed/Branch-TestBed.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@
228228
C1614D56285BC8A00098946B /* LinkPresentation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C1614D55285BC8A00098946B /* LinkPresentation.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
229229
C1614D5C285BD4AF0098946B /* BranchPluginSupport.h in Headers */ = {isa = PBXBuildFile; fileRef = C1614D5A285BD4AF0098946B /* BranchPluginSupport.h */; };
230230
C1614D5D285BD4AF0098946B /* BranchPluginSupport.m in Sources */ = {isa = PBXBuildFile; fileRef = C1614D5B285BD4AF0098946B /* BranchPluginSupport.m */; };
231+
C16B975B2B6C21C900FB0631 /* BranchLogger.h in Headers */ = {isa = PBXBuildFile; fileRef = C16B975A2B6C21C900FB0631 /* BranchLogger.h */; };
232+
C16B975D2B6C21DC00FB0631 /* BranchLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = C16B975C2B6C21DC00FB0631 /* BranchLogger.m */; };
233+
C16B975F2B716C4700FB0631 /* BranchLoggerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C16B975E2B716C4700FB0631 /* BranchLoggerTests.m */; };
231234
C17394602A8AEE0E006068F2 /* BNCProductCategory.m in Sources */ = {isa = PBXBuildFile; fileRef = C173945F2A8AEE0E006068F2 /* BNCProductCategory.m */; };
232235
C17394612A8C20FD006068F2 /* BNCProductCategory.h in Headers */ = {isa = PBXBuildFile; fileRef = C173945E2A8AEDFB006068F2 /* BNCProductCategory.h */; settings = {ATTRIBUTES = (Public, ); }; };
233236
C17394642A8C228D006068F2 /* BNCCurrency.m in Sources */ = {isa = PBXBuildFile; fileRef = C17394632A8C228D006068F2 /* BNCCurrency.m */; };
@@ -541,6 +544,9 @@
541544
C1614D55285BC8A00098946B /* LinkPresentation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = LinkPresentation.framework; path = System/Library/Frameworks/LinkPresentation.framework; sourceTree = SDKROOT; };
542545
C1614D5A285BD4AF0098946B /* BranchPluginSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BranchPluginSupport.h; sourceTree = "<group>"; };
543546
C1614D5B285BD4AF0098946B /* BranchPluginSupport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BranchPluginSupport.m; sourceTree = "<group>"; };
547+
C16B975A2B6C21C900FB0631 /* BranchLogger.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BranchLogger.h; sourceTree = "<group>"; };
548+
C16B975C2B6C21DC00FB0631 /* BranchLogger.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BranchLogger.m; sourceTree = "<group>"; };
549+
C16B975E2B716C4700FB0631 /* BranchLoggerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BranchLoggerTests.m; sourceTree = "<group>"; };
544550
C173945E2A8AEDFB006068F2 /* BNCProductCategory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BNCProductCategory.h; sourceTree = "<group>"; };
545551
C173945F2A8AEE0E006068F2 /* BNCProductCategory.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCProductCategory.m; sourceTree = "<group>"; };
546552
C17394622A8C2282006068F2 /* BNCCurrency.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BNCCurrency.h; sourceTree = "<group>"; };
@@ -685,6 +691,7 @@
685691
C15CC9DF2ABCF8C8003CC339 /* BranchActivityItemTests.m */,
686692
C17DAF7A2AC20C2000B16B1A /* BranchClassTests.m */,
687693
C15CC9DD2ABCB549003CC339 /* BNCCurrencyTests.m */,
694+
C16B975E2B716C4700FB0631 /* BranchLoggerTests.m */,
688695
);
689696
path = "Branch-SDK-Tests";
690697
sourceTree = "<group>";
@@ -861,6 +868,8 @@
861868
7E30BCF31A71EEEE00AC7402 /* BNCLinkData.m */,
862869
4DA577151E67B1D600A43BDD /* BNCLog.h */,
863870
4DA577161E67B1D600A43BDD /* BNCLog.m */,
871+
C16B975A2B6C21C900FB0631 /* BranchLogger.h */,
872+
C16B975C2B6C21DC00FB0631 /* BranchLogger.m */,
864873
5F92B23D238486E200CA909B /* BNCNetworkInterface.h */,
865874
5F92B23E238486E200CA909B /* BNCNetworkInterface.m */,
866875
5F38020224DCC2E600E6FAFD /* BNCNetworkService.h */,
@@ -1030,6 +1039,7 @@
10301039
5F38022524DCC2E800E6FAFD /* BranchLATDRequest.h in Headers */,
10311040
4DCAC8051F426F7C00405D1D /* BNCDeviceInfo.h in Headers */,
10321041
4DCAC8061F426F7C00405D1D /* BNCEncodingUtils.h in Headers */,
1042+
C16B975B2B6C21C900FB0631 /* BranchLogger.h in Headers */,
10331043
5F92B23123834AFD00CA909B /* BNCReachability.h in Headers */,
10341044
4DCAC80E1F426F7C00405D1D /* BNCSystemObserver.h in Headers */,
10351045
C10C61AD28248E5A00761D7E /* BNCQRCodeCache.h in Headers */,
@@ -1344,6 +1354,7 @@
13441354
466B586A1B17779C00A69EDE /* BNCLinkCache.m in Sources */,
13451355
4D7881F7209CF28F002B750F /* BNCThreads.m in Sources */,
13461356
5F892EBE2361157E0023AEC1 /* NSError+Branch.m in Sources */,
1357+
C16B975D2B6C21DC00FB0631 /* BranchLogger.m in Sources */,
13471358
9A2B7DD61FEC3BAF00CD188B /* Branch+Validator.m in Sources */,
13481359
5FE693F82405E91500E3AEE2 /* BNCCallbackMap.m in Sources */,
13491360
5F92B240238486E200CA909B /* BNCNetworkInterface.m in Sources */,
@@ -1433,6 +1444,7 @@
14331444
4D1683AC2098C902008819E3 /* BranchInstallRequestTests.m in Sources */,
14341445
4D1683C72098C902008819E3 /* BNCCrashlyticsWrapperTests.m in Sources */,
14351446
5FDF91592581CDF4009BE5A3 /* BNCPartnerParametersTests.m in Sources */,
1447+
C16B975F2B716C4700FB0631 /* BranchLoggerTests.m in Sources */,
14361448
);
14371449
runOnlyForDeploymentPostprocessing = 0;
14381450
};

Branch-TestBed/Branch-TestBed/AppDelegate.m

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ - (BOOL)application:(UIApplication *)application
2323
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2424

2525
appDelegate = self;
26-
BNCLogSetOutputFunction(APPLogHookFunction);
27-
BNCLogSetDisplayLevel(BNCLogLevelAll);
2826

2927
/*
3028
Set Branch.useTestBranchKey = YES; to have Branch use the test key that's in the app's
@@ -38,10 +36,15 @@ - (BOOL)application:(UIApplication *)application
3836
// test pre init support
3937
//[self testDispatchToIsolationQueue:branch]
4038

41-
// Comment out (for match guarantee testing) / or un-comment to toggle debugging:
42-
// Note: Unit tests will fail if 'setDebug' is set.
43-
// [branch setDebug];
44-
[branch enableLogging];
39+
[branch enableLoggingAtLevel:BranchLogLevelVerbose];
40+
// [BranchLogger shared].logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
41+
// // Handle the log message and error here. For example, printing to the console:
42+
// if (error) {
43+
// NSLog(@"[BranchLog] Level: %lu, Message: %@, Error: %@", (unsigned long)logLevel, message, error.localizedDescription);
44+
// } else {
45+
// NSLog(@"[BranchLog] Level: %lu, Message: %@", (unsigned long)logLevel, message);
46+
// }
47+
// };
4548

4649
// Comment out in production. Un-comment to test your Branch SDK Integration:
4750
//[branch validateSDKIntegration];

BranchSDK/BNCNetworkService.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "BNCEncodingUtils.h"
1111
#import "BNCLog.h"
1212
#import "NSError+Branch.h"
13+
#import "BranchLogger.h"
1314

1415
#pragma mark BNCNetworkOperation
1516

@@ -204,7 +205,9 @@ - (void) startOperation:(BNCNetworkOperation*)operation {
204205
if (operation.completionBlock)
205206
operation.completionBlock(operation);
206207
}];
207-
BNCLogDebug([NSString stringWithFormat:@"Network start operation %@.", operation.request.URL]);
208+
209+
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"Network start operation %@.", operation.request.URL]];
210+
208211
[operation.sessionTask resume];
209212
}
210213

BranchSDK/BNCServerInterface.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "Branch.h"
1717
#import "BNCSKAdNetwork.h"
1818
#import "BNCReferringURLUtility.h"
19+
#import "BranchLogger.h"
1920

2021
@interface BNCServerInterface ()
2122
@property (copy, nonatomic) NSString *requestEndpoint;
@@ -329,8 +330,9 @@ - (BNCServerResponse *)processServerResponse:(NSURLResponse *)response data:(NSD
329330
serverResponse.data = error.userInfo;
330331
serverResponse.requestId = requestId;
331332
}
333+
334+
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"Server returned: %@.", serverResponse]];
332335

333-
BNCLogDebug([NSString stringWithFormat:@"Server returned: %@.", serverResponse]);
334336
return serverResponse;
335337
}
336338

BranchSDK/Branch.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#import "BNCServerInterface.h"
4040
#import "BNCServerRequestQueue.h"
4141

42+
#import "BranchLogger.h"
4243
// Not used by Branch singleton public API
4344
//#import "BranchEvent.h"
4445
//#import "BranchScene.h"
@@ -564,9 +565,10 @@ extern NSString * __nonnull const BNCSpotlightFeature;
564565
///--------------------
565566

566567
/**
567-
Enable debug messages to NSLog.
568+
Enable debug messages to os_log.
568569
*/
569570
- (void)enableLogging;
571+
- (void)enableLoggingAtLevel:(BranchLogLevel)logLevel;
570572

571573
/**
572574
Send requests to EU endpoints.
@@ -575,15 +577,6 @@ extern NSString * __nonnull const BNCSpotlightFeature;
575577
*/
576578
- (void)useEUEndpoints;
577579

578-
/**
579-
setDebug is deprecated and all functionality has been disabled.
580-
581-
If you wish to enable logging, please invoke enableLogging.
582-
583-
If you wish to simulate installs, please see add a Test Device (https://help.branch.io/using-branch/docs/adding-test-devices) then reset your test device's data (https://help.branch.io/using-branch/docs/adding-test-devices#section-resetting-your-test-device-data).
584-
*/
585-
- (void)setDebug __attribute__((deprecated(("setDebug is replaced by enableLogging and test devices. https://help.branch.io/using-branch/docs/adding-test-devices"))));
586-
587580
/**
588581
@brief Use the `validateSDKIntegration` method as a debugging aid to assure that you've
589582
integrated the Branch SDK correctly.

BranchSDK/Branch.m

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#import "UIViewController+Branch.h"
4444
#import "BNCReferringURLUtility.h"
4545
#import "BNCServerAPI.h"
46+
#import "BranchLogger.h"
4647

4748
#if !TARGET_OS_TV
4849
#import "BNCUserAgentCollector.h"
@@ -419,20 +420,17 @@ + (BOOL)branchKeyIsSet {
419420
}
420421

421422
- (void)enableLogging {
422-
BNCLogSetDisplayLevel(BNCLogLevelDebug);
423+
[self enableLoggingAtLevel:BranchLogLevelDebug];
423424
}
424425

425-
- (void)useEUEndpoints {
426-
[BNCServerAPI sharedInstance].useEUServers = YES;
426+
- (void)enableLoggingAtLevel:(BranchLogLevel)logLevel {
427+
BranchLogger *logger = [BranchLogger shared];
428+
logger.loggingEnabled = YES;
429+
logger.logLevelThreshold = logLevel;
427430
}
428431

429-
- (void)setDebug {
430-
NSLog(@"Branch setDebug is deprecated and all functionality has been disabled. "
431-
"If you wish to enable logging, please invoke enableLogging. "
432-
"If you wish to simulate installs, please see add a Test Device "
433-
"(https://help.branch.io/using-branch/docs/adding-test-devices) "
434-
"then reset your test device's data "
435-
"(https://help.branch.io/using-branch/docs/adding-test-devices#section-resetting-your-test-device-data).");
432+
- (void)useEUEndpoints {
433+
[BNCServerAPI sharedInstance].useEUServers = YES;
436434
}
437435

438436
- (void)validateSDKIntegration {

BranchSDK/BranchLogger.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// BranchLogger.h
3+
// Branch
4+
//
5+
// Created by Nipun Singh on 2/1/24.
6+
// Copyright © 2024 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
typedef NS_ENUM(NSUInteger, BranchLogLevel) {
12+
BranchLogLevelVerbose,
13+
BranchLogLevelDebug,
14+
BranchLogLevelInfo,
15+
BranchLogLevelWarning,
16+
BranchLogLevelError,
17+
};
18+
19+
typedef void(^BranchLogCallback)(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error);
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
@interface BranchLogger : NSObject
24+
25+
@property (nonatomic, assign) BOOL loggingEnabled;
26+
@property (nonatomic, copy, nullable) BranchLogCallback logCallback;
27+
@property (nonatomic, assign) BranchLogLevel logLevelThreshold;
28+
29+
+ (instancetype _Nonnull)shared;
30+
31+
- (void)logError:(NSString * _Nonnull)message error:(NSError * _Nullable)error;
32+
- (void)logWarning:(NSString * _Nonnull)message;
33+
- (void)logInfo:(NSString * _Nonnull)message;
34+
- (void)logDebug:(NSString * _Nonnull)message;
35+
- (void)logVerbose:(NSString * _Nonnull)message;
36+
37+
@end
38+
39+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)