Skip to content

Commit

Permalink
Created BranchAdvancedLogCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
nsingh-branch committed Dec 10, 2024
1 parent 6e91474 commit bb02891
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Branch-TestBed/Branch-TestBed/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions Sources/BranchSDK/Branch.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
19 changes: 15 additions & 4 deletions Sources/BranchSDK/BranchLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions Sources/BranchSDK/Public/Branch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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."))));
Expand Down
4 changes: 3 additions & 1 deletion Sources/BranchSDK/Public/BranchLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
Expand Down

0 comments on commit bb02891

Please sign in to comment.