From d250eedd2109fb455d935c7aa41dd88298f6933b Mon Sep 17 00:00:00 2001 From: nsingh-branch Date: Mon, 9 Dec 2024 12:35:32 -0800 Subject: [PATCH 1/4] Updated callback --- Branch-TestBed/Branch-TestBed/AppDelegate.m | 12 +++++++++-- Sources/BranchSDK/BNCServerInterface.m | 4 ++-- Sources/BranchSDK/BranchLogger.m | 23 ++++++++++++++------- Sources/BranchSDK/Public/BranchLogger.h | 4 +++- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Branch-TestBed/Branch-TestBed/AppDelegate.m b/Branch-TestBed/Branch-TestBed/AppDelegate.m index aedf3557b..8df77b62e 100644 --- a/Branch-TestBed/Branch-TestBed/AppDelegate.m +++ b/Branch-TestBed/Branch-TestBed/AppDelegate.m @@ -41,8 +41,7 @@ - (BOOL)application:(UIApplication *)application // test pre init support //[self testDispatchToIsolationQueue:branch] - - [Branch enableLoggingAtLevel:BranchLogLevelVerbose withCallback:^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) { + [Branch enableLoggingAtLevel:BranchLogLevelVerbose withCallback:^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response) { // Handle the log message and error here. For example, printing to the console: if (error) { NSLog(@"[BranchLog] Level: %lu, Message: %@, Error: %@", (unsigned long)logLevel, message, error.localizedDescription); @@ -50,6 +49,15 @@ - (BOOL)application:(UIApplication *)application NSLog(@"[BranchLog] Level: %lu, Message: %@", (unsigned long)logLevel, message); } + if (request) { + NSString *jsonString = [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]; + NSLog(@"[BranchLog] Got %@ Request: %@", request.URL , jsonString); + } + + if (response) { + NSLog(@"[BranchLog] Got %@ Response: %@", response, response.data); + } + NSString *logEntry = error ? [NSString stringWithFormat:@"Level: %lu, Message: %@, Error: %@", (unsigned long)logLevel, message, error.localizedDescription] : [NSString stringWithFormat:@"Level: %lu, Message: %@", (unsigned long)logLevel, message]; APPLogHookFunction([NSDate date], logLevel, logEntry); diff --git a/Sources/BranchSDK/BNCServerInterface.m b/Sources/BranchSDK/BNCServerInterface.m index 2fc2e29f8..37cfcfd1d 100644 --- a/Sources/BranchSDK/BNCServerInterface.m +++ b/Sources/BranchSDK/BNCServerInterface.m @@ -289,7 +289,7 @@ - (NSURLRequest *)preparePostRequest:(NSDictionary *)params url:(NSString *)url [request setHTTPBody:postData]; if ([[BranchLogger shared] shouldLog:BranchLogLevelDebug]) { - [[BranchLogger shared] logDebug:[NSString stringWithFormat:@"%@\nHeaders %@\nBody %@", request, [request allHTTPHeaderFields], [BNCEncodingUtils prettyPrintJSON:updatedParams]] error:nil]; + [[BranchLogger shared] logDebug:[NSString stringWithFormat:@"%@\nHeaders %@\nBody %@", request, [request allHTTPHeaderFields], [BNCEncodingUtils prettyPrintJSON:updatedParams]] error:nil request:request response:nil]; } return request; @@ -310,7 +310,7 @@ - (BNCServerResponse *)processServerResponse:(NSURLResponse *)response data:(NSD serverResponse.requestId = requestId; if ([[BranchLogger shared] shouldLog:BranchLogLevelDebug]) { - [[BranchLogger shared] logDebug:[NSString stringWithFormat:@"%@\nBody %@", response, [BNCEncodingUtils prettyPrintJSON:serverResponse.data]] error:nil]; + [[BranchLogger shared] logDebug:[NSString stringWithFormat:@"%@\nBody %@", response, [BNCEncodingUtils prettyPrintJSON:serverResponse.data]] error:nil request:nil response:serverResponse]; } } else { diff --git a/Sources/BranchSDK/BranchLogger.m b/Sources/BranchSDK/BranchLogger.m index d64a65568..ce89b97c6 100644 --- a/Sources/BranchSDK/BranchLogger.m +++ b/Sources/BranchSDK/BranchLogger.m @@ -18,7 +18,7 @@ - (instancetype)init { _includeCallerDetails = YES; // default callback sends logs to os_log - _logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) { + _logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response) { NSString *formattedMessage = [BranchLogger formatMessage:message logLevel:logLevel error:error]; os_log_t log = os_log_create("io.branch.sdk", "BranchSDK"); @@ -53,22 +53,29 @@ - (void)disableCallerDetails { } - (void)logError:(NSString *)message error:(NSError *_Nullable)error { - [self logMessage:message withLevel:BranchLogLevelError error:error]; + [self logMessage:message withLevel:BranchLogLevelError error:error request:nil response:nil]; } - (void)logWarning:(NSString *)message error:(NSError *_Nullable)error { - [self logMessage:message withLevel:BranchLogLevelWarning error:error]; + [self logMessage:message withLevel:BranchLogLevelWarning error:error request:nil response:nil]; } -- (void)logDebug:(NSString *)message error:(NSError *_Nullable)error { - [self logMessage:message withLevel:BranchLogLevelDebug error:error]; +- (void)logDebug:(NSString * _Nonnull)message error:(NSError * _Nullable)error { + [self logDebug:message error:error request:nil response:nil]; +} + +- (void)logDebug:(NSString * _Nonnull)message + error:(NSError * _Nullable)error + request:(NSMutableURLRequest * _Nullable)request + response:(BNCServerResponse * _Nullable)response { + [self logMessage:message withLevel:BranchLogLevelDebug error:error request:request response:response]; } - (void)logVerbose:(NSString *)message error:(NSError *_Nullable)error { - [self logMessage:message withLevel:BranchLogLevelVerbose error:error]; + [self logMessage:message withLevel:BranchLogLevelVerbose error:error request:nil response:nil]; } -- (void)logMessage:(NSString *)message withLevel:(BranchLogLevel)level error:(NSError *_Nullable)error { +- (void)logMessage:(NSString *)message withLevel:(BranchLogLevel)level error:(NSError *_Nullable)error request:(NSMutableURLRequest * _Nullable)request response:(BNCServerResponse * _Nullable)response { if (!self.loggingEnabled || level < self.logLevelThreshold || message.length == 0) { return; } @@ -79,7 +86,7 @@ - (void)logMessage:(NSString *)message withLevel:(BranchLogLevel)level error:(NS } if (self.logCallback) { - self.logCallback(formattedMessage, level, error); + self.logCallback(formattedMessage, level, error, request, response); } } diff --git a/Sources/BranchSDK/Public/BranchLogger.h b/Sources/BranchSDK/Public/BranchLogger.h index d0354b387..2079f4fe9 100644 --- a/Sources/BranchSDK/Public/BranchLogger.h +++ b/Sources/BranchSDK/Public/BranchLogger.h @@ -7,6 +7,7 @@ // #import +#import "BNCServerResponse.h" typedef NS_ENUM(NSUInteger, BranchLogLevel) { BranchLogLevelVerbose, // development @@ -15,7 +16,7 @@ typedef NS_ENUM(NSUInteger, BranchLogLevel) { BranchLogLevelError, // severe errors. SDK is probably in a bad state. }; -typedef void(^BranchLogCallback)(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error); +typedef void(^BranchLogCallback)(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response); NS_ASSUME_NONNULL_BEGIN @@ -37,6 +38,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)logError:(NSString * _Nonnull)message error:(NSError * _Nullable)error; - (void)logWarning:(NSString * _Nonnull)message error:(NSError * _Nullable)error; - (void)logDebug:(NSString * _Nonnull)message error:(NSError * _Nullable)error; +- (void)logDebug:(NSString * _Nonnull)message error:(NSError * _Nullable)error request:(NSMutableURLRequest * _Nullable)request response:(BNCServerResponse * _Nullable)response; - (void)logVerbose:(NSString * _Nonnull)message error:(NSError * _Nullable)error; // default Branch log format From 6e914747366af121b4d9a2782cf73ff504ac922e Mon Sep 17 00:00:00 2001 From: nsingh-branch Date: Mon, 9 Dec 2024 13:03:09 -0800 Subject: [PATCH 2/4] Update AppDelegate.m --- Branch-TestBed/Branch-TestBed/AppDelegate.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Branch-TestBed/Branch-TestBed/AppDelegate.m b/Branch-TestBed/Branch-TestBed/AppDelegate.m index 8df77b62e..01d8c33d7 100644 --- a/Branch-TestBed/Branch-TestBed/AppDelegate.m +++ b/Branch-TestBed/Branch-TestBed/AppDelegate.m @@ -55,7 +55,7 @@ - (BOOL)application:(UIApplication *)application } if (response) { - NSLog(@"[BranchLog] Got %@ Response: %@", response, response.data); + NSLog(@"[BranchLog] Got Response for request(%@): %@", response.requestId, response.data); } NSString *logEntry = error ? [NSString stringWithFormat:@"Level: %lu, Message: %@, Error: %@", (unsigned long)logLevel, message, error.localizedDescription] From bb02891baf80cb1450af86e423f44d0c96792b86 Mon Sep 17 00:00:00 2001 From: nsingh-branch Date: Tue, 10 Dec 2024 11:31:53 -0800 Subject: [PATCH 3/4] Created BranchAdvancedLogCallback --- Branch-TestBed/Branch-TestBed/AppDelegate.m | 4 ++-- Sources/BranchSDK/Branch.m | 9 +++++++++ Sources/BranchSDK/BranchLogger.m | 19 +++++++++++++++---- Sources/BranchSDK/Public/Branch.h | 1 + Sources/BranchSDK/Public/BranchLogger.h | 4 +++- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Branch-TestBed/Branch-TestBed/AppDelegate.m b/Branch-TestBed/Branch-TestBed/AppDelegate.m index 01d8c33d7..1639425e7 100644 --- a/Branch-TestBed/Branch-TestBed/AppDelegate.m +++ b/Branch-TestBed/Branch-TestBed/AppDelegate.m @@ -41,7 +41,7 @@ - (BOOL)application:(UIApplication *)application // test pre init support //[self testDispatchToIsolationQueue:branch] - [Branch enableLoggingAtLevel:BranchLogLevelVerbose withCallback:^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response) { + [Branch enableLoggingAtLevel:BranchLogLevelVerbose withAdvancedCallback:^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response) { // Handle the log message and error here. For example, printing to the console: if (error) { NSLog(@"[BranchLog] Level: %lu, Message: %@, Error: %@", (unsigned long)logLevel, message, error.localizedDescription); @@ -55,7 +55,7 @@ - (BOOL)application:(UIApplication *)application } if (response) { - NSLog(@"[BranchLog] Got Response for request(%@): %@", response.requestId, response.data); + NSLog(@"[BranchLog] Got Response for request (%@): %@", response.requestId, response.data); } NSString *logEntry = error ? [NSString stringWithFormat:@"Level: %lu, Message: %@, Error: %@", (unsigned long)logLevel, message, error.localizedDescription] diff --git a/Sources/BranchSDK/Branch.m b/Sources/BranchSDK/Branch.m index f1178f45c..a0036f74c 100644 --- a/Sources/BranchSDK/Branch.m +++ b/Sources/BranchSDK/Branch.m @@ -455,6 +455,15 @@ + (void)enableLoggingAtLevel:(BranchLogLevel)logLevel withCallback:(nullable Bra } } ++ (void)enableLoggingAtLevel:(BranchLogLevel)logLevel withAdvancedCallback:(nullable BranchAdvancedLogCallback)callback { + BranchLogger *logger = [BranchLogger shared]; + logger.loggingEnabled = YES; + logger.logLevelThreshold = logLevel; + if (callback) { + logger.advancedLogCallback = callback; + } +} + - (void)useEUEndpoints { [BNCServerAPI sharedInstance].useEUServers = YES; } diff --git a/Sources/BranchSDK/BranchLogger.m b/Sources/BranchSDK/BranchLogger.m index ce89b97c6..952faea1f 100644 --- a/Sources/BranchSDK/BranchLogger.m +++ b/Sources/BranchSDK/BranchLogger.m @@ -18,7 +18,16 @@ - (instancetype)init { _includeCallerDetails = YES; // default callback sends logs to os_log - _logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response) { + _logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) { + NSString *formattedMessage = [BranchLogger formatMessage:message logLevel:logLevel error:error]; + + os_log_t log = os_log_create("io.branch.sdk", "BranchSDK"); + os_log_type_t osLogType = [BranchLogger osLogTypeForBranchLogLevel:logLevel]; + os_log_with_type(log, osLogType, "%{private}@", formattedMessage); + }; + + // default advanced callback sends logs to os_log + _advancedLogCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response) { NSString *formattedMessage = [BranchLogger formatMessage:message logLevel:logLevel error:error]; os_log_t log = os_log_create("io.branch.sdk", "BranchSDK"); @@ -84,9 +93,11 @@ - (void)logMessage:(NSString *)message withLevel:(BranchLogLevel)level error:(NS if (self.includeCallerDetails) { formattedMessage = [NSString stringWithFormat:@"%@ %@", [self callingClass], message]; } - - if (self.logCallback) { - self.logCallback(formattedMessage, level, error, request, response); + + if (self.advancedLogCallback) { + self.advancedLogCallback(formattedMessage, level, error, request, response); + } else if (self.logCallback) { + self.logCallback(formattedMessage, level, error); } } diff --git a/Sources/BranchSDK/Public/Branch.h b/Sources/BranchSDK/Public/Branch.h index 3f381efeb..f82e60dc5 100644 --- a/Sources/BranchSDK/Public/Branch.h +++ b/Sources/BranchSDK/Public/Branch.h @@ -568,6 +568,7 @@ extern NSString * __nonnull const BNCSpotlightFeature; */ + (void)enableLogging; + (void)enableLoggingAtLevel:(BranchLogLevel)logLevel withCallback:(nullable BranchLogCallback)callback; ++ (void)enableLoggingAtLevel:(BranchLogLevel)logLevel withAdvancedCallback:(nullable BranchAdvancedLogCallback)callback; // The new logging system is independent of the Branch singleton and can be called earlier. - (void)enableLogging __attribute__((deprecated(("This API is deprecated. Please use the static version.")))); diff --git a/Sources/BranchSDK/Public/BranchLogger.h b/Sources/BranchSDK/Public/BranchLogger.h index 2079f4fe9..1d05cd88b 100644 --- a/Sources/BranchSDK/Public/BranchLogger.h +++ b/Sources/BranchSDK/Public/BranchLogger.h @@ -16,7 +16,8 @@ typedef NS_ENUM(NSUInteger, BranchLogLevel) { BranchLogLevelError, // severe errors. SDK is probably in a bad state. }; -typedef void(^BranchLogCallback)(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response); +typedef void(^BranchLogCallback)(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error); +typedef void(^BranchAdvancedLogCallback)(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response); NS_ASSUME_NONNULL_BEGIN @@ -25,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, assign) BOOL loggingEnabled; @property (nonatomic, assign) BOOL includeCallerDetails; @property (nonatomic, copy, nullable) BranchLogCallback logCallback; +@property (nonatomic, copy, nullable) BranchAdvancedLogCallback advancedLogCallback; @property (nonatomic, assign) BranchLogLevel logLevelThreshold; + (instancetype _Nonnull)shared; From cfba09191a57f53e1ab9b7a4bc7433a701618351 Mon Sep 17 00:00:00 2001 From: nsingh-branch Date: Tue, 10 Dec 2024 16:12:44 -0800 Subject: [PATCH 4/4] Update BranchLogger.m --- Sources/BranchSDK/BranchLogger.m | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Sources/BranchSDK/BranchLogger.m b/Sources/BranchSDK/BranchLogger.m index 952faea1f..1cbd86cb2 100644 --- a/Sources/BranchSDK/BranchLogger.m +++ b/Sources/BranchSDK/BranchLogger.m @@ -25,15 +25,6 @@ - (instancetype)init { os_log_type_t osLogType = [BranchLogger osLogTypeForBranchLogLevel:logLevel]; os_log_with_type(log, osLogType, "%{private}@", formattedMessage); }; - - // default advanced callback sends logs to os_log - _advancedLogCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error, NSMutableURLRequest * _Nullable request, BNCServerResponse * _Nullable response) { - NSString *formattedMessage = [BranchLogger formatMessage:message logLevel:logLevel error:error]; - - os_log_t log = os_log_create("io.branch.sdk", "BranchSDK"); - os_log_type_t osLogType = [BranchLogger osLogTypeForBranchLogLevel:logLevel]; - os_log_with_type(log, osLogType, "%{private}@", formattedMessage); - }; } return self; }