Skip to content

Commit

Permalink
Add trace logging
Browse files Browse the repository at this point in the history
  • Loading branch information
gdeluna-branch committed Jul 24, 2024
1 parent c480ccc commit 82b8633
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Sources/BranchSDK/BNCPreferenceHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,11 @@ - (NSString *)sessionParams {

- (void)setSessionParams:(NSString *)sessionParams {
@synchronized (self) {
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Setting session params %@", sessionParams] error:nil];
if (sessionParams == nil || ![_sessionParams isEqualToString:sessionParams]) {
_sessionParams = sessionParams;
[self writeObjectToDefaults:BRANCH_PREFS_KEY_SESSION_PARAMS value:sessionParams];
[[BranchLogger shared] logVerbose:@"Params set" error:nil];
}
}
}
Expand Down Expand Up @@ -824,6 +826,8 @@ - (void) setAdUserDataUsageConsent:(BOOL)hasConsent {

- (void) clearTrackingInformation {
@synchronized(self) {
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"clearTrackingInformation"] error:nil];

/*
// Don't clear these
self.randomizedDeviceToken = nil;
Expand Down
2 changes: 2 additions & 0 deletions Sources/BranchSDK/BNCReferringURLUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ - (void)dealloc {


- (void)parseReferringURL:(NSURL *)url {
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Parsing URL %@", url] error:nil];

NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
for (NSURLQueryItem *item in components.queryItems) {
if ([self isSupportedQueryParameter:item.name]) {
Expand Down
9 changes: 8 additions & 1 deletion Sources/BranchSDK/BNCServerInterface.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ - (void)postRequest:(NSDictionary *)post url:(NSString *)url key:(NSString *)key

- (void)postRequest:(NSDictionary *)post url:(NSString *)url retryNumber:(NSInteger)retryNumber key:(NSString *)key callback:(BNCServerCallback)callback {

[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"retryNumber %ld", retryNumber] error:nil];

// TODO: confirm it's ok to send full URL instead of with the domain trimmed off
self.requestEndpoint = url;
NSURLRequest *request = [self preparePostRequest:post url:url key:key retryNumber:retryNumber];
Expand Down Expand Up @@ -139,7 +141,8 @@ - (void)genericHTTPRequest:(NSURLRequest *)request retryNumber:(NSInteger)retryN
// Drops non-linking requests when tracking is disabled
if (Branch.trackingDisabled) {
NSString *endpoint = request.URL.absoluteString;

[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Tracking is disabled, checking if %@ is linking request.", endpoint] error:nil];

if (![self isLinkingRelatedRequest:endpoint]) {
[[BNCPreferenceHelper sharedInstance] clearTrackingInformation];
NSError *error = [NSError branchErrorWithCode:BNCTrackingDisabledError];
Expand Down Expand Up @@ -290,6 +293,10 @@ - (BNCServerResponse *)processServerResponse:(NSURLResponse *)response data:(NSD
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSString *requestId = httpResponse.allHeaderFields[@"X-Branch-Request-Id"];

if ([[BranchLogger shared] shouldLog:BranchLogLevelVerbose]) {
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Processing response %@", requestId] error:nil];
}

if (!error) {
serverResponse.statusCode = @([httpResponse statusCode]);
serverResponse.data = [BNCEncodingUtils decodeJsonDataToDictionary:data];
Expand Down
27 changes: 27 additions & 0 deletions Sources/BranchSDK/Branch.m
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ + (BOOL)trackingDisabled {

+ (void)setTrackingDisabled:(BOOL)disabled {
@synchronized(self) {
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"setTrackingDisabled to %d", disabled] error:nil];

BOOL currentSetting = self.trackingDisabled;
if (!!currentSetting == !!disabled)
return;
Expand All @@ -515,6 +517,8 @@ + (void)setTrackingDisabled:(BOOL)disabled {
Branch *branch = Branch.getInstance;
[branch clearNetworkQueue];
branch.initializationStatus = BNCInitStatusUninitialized;
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"initializationStatus %ld", branch.initializationStatus] error:nil];

[branch.linkCache clear];
// Release the lock in case it's locked:
[BranchOpenRequest releaseOpenResponseLock];
Expand Down Expand Up @@ -668,10 +672,12 @@ - (BOOL)handleDeepLink:(NSURL *)url {
}

- (BOOL)handleDeepLink:(NSURL *)url sceneIdentifier:(NSString *)sceneIdentifier {
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Handle deep link %@", url] error:nil];

// we've been resetting the session on all deeplinks for quite some time
// this allows foreground links to callback
self.initializationStatus = BNCInitStatusUninitialized;
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"initializationStatus %ld", self.initializationStatus] error:nil];

//Check the referring url/uri for query parameters and save them
BNCReferringURLUtility *utility = [BNCReferringURLUtility new];
Expand Down Expand Up @@ -702,6 +708,8 @@ - (BOOL)handleDeepLink:(NSURL *)url sceneIdentifier:(NSString *)sceneIdentifier
- (BOOL)handleSchemeDeepLink_private:(NSURL*)url sceneIdentifier:(NSString *)sceneIdentifier {
BOOL handled = NO;
self.preferenceHelper.referringURL = nil;
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Set referringURL to %@", self.preferenceHelper.referringURL] error:nil];

if (url && ![url isEqual:[NSNull null]]) {

NSString *urlScheme = [url scheme];
Expand All @@ -712,12 +720,14 @@ - (BOOL)handleSchemeDeepLink_private:(NSURL*)url sceneIdentifier:(NSString *)sce
if (urlScheme && [scheme isEqualToString:urlScheme]) {
self.preferenceHelper.externalIntentURI = [url absoluteString];
self.preferenceHelper.referringURL = [url absoluteString];
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Allowed scheme list, set externalIntentURI and referringURL to %@", [url absoluteString]] error:nil];
break;
}
}
} else {
self.preferenceHelper.externalIntentURI = [url absoluteString];
self.preferenceHelper.referringURL = [url absoluteString];
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Set externalIntentURI and referringURL to %@", [url absoluteString]] error:nil];
}

NSString *query = [url fragment];
Expand Down Expand Up @@ -762,6 +772,7 @@ - (BOOL)handleUniversalDeepLink_private:(NSString*)urlString sceneIdentifier:(NS
if (urlString.length) {
self.preferenceHelper.universalLinkUrl = urlString;
self.preferenceHelper.referringURL = urlString;
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Set universalLinkUrl and referringURL to %@", urlString] error:nil];
}

[self initUserSessionAndCallCallback:YES sceneIdentifier:sceneIdentifier urlString:urlString];
Expand Down Expand Up @@ -1699,16 +1710,25 @@ - (void)applicationDidBecomeActive {
dispatch_async(self.isolationQueue, ^(){
// if necessary, creates a new organic open
BOOL installOrOpenInQueue = [self.requestQueue containsInstallOrOpen];

[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"applicationDidBecomeActive installOrOpenInQueue %d", installOrOpenInQueue] error:nil];

if (!Branch.trackingDisabled && self.initializationStatus != BNCInitStatusInitialized && !installOrOpenInQueue) {
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"applicationDidBecomeActive trackingDisabled %d initializationStatus %d installOrOpenInQueue %d", Branch.trackingDisabled, self.initializationStatus, installOrOpenInQueue] error:nil];

[self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlString:nil];
}
});
}

- (void)applicationWillResignActive {
[[BranchLogger shared] logVerbose:@"applicationWillResignActive" error:nil];

dispatch_async(self.isolationQueue, ^(){
if (!Branch.trackingDisabled) {
self.initializationStatus = BNCInitStatusUninitialized;
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"applicationWillResignActive initializationStatus %ld", self.initializationStatus] error:nil];

[self.requestQueue persistImmediately];
[BranchOpenRequest setWaitNeededForOpenResponseLock];
}
Expand Down Expand Up @@ -1842,6 +1862,8 @@ - (void)processNextQueueItem {
self.networkCount = 1;
dispatch_semaphore_signal(self.processing_sema);
BNCServerRequest *req = [self.requestQueue peek];

[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Processing %@", req]error:nil];

if (req) {

Expand Down Expand Up @@ -2035,6 +2057,8 @@ - (void)initializeSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSS
}

self.initializationStatus = BNCInitStatusInitializing;
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"initializationStatus %ld", self.initializationStatus] error:nil];

[self processNextQueueItem];
});
}
Expand All @@ -2044,6 +2068,8 @@ - (void)initializeSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSS
- (void)handleInitSuccessAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier {

self.initializationStatus = BNCInitStatusInitialized;
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"initializationStatus %ld", self.initializationStatus] error:nil];

NSDictionary *latestReferringParams = [self getLatestReferringParams];

if ([latestReferringParams[@"_branch_validate"] isEqualToString:@"060514"]) {
Expand Down Expand Up @@ -2260,6 +2286,7 @@ - (void)presentSharingViewController:(UIViewController <BranchDeepLinkingControl

- (void)handleInitFailure:(NSError *)error callCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier {
self.initializationStatus = BNCInitStatusUninitialized;
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"initializationStatus %ld", self.initializationStatus] error:nil];

if (callCallback) {
if (self.sceneSessionInitWithCallback) {
Expand Down

0 comments on commit 82b8633

Please sign in to comment.