Skip to content

Commit b17ea2b

Browse files
committed
Added callingClass helper method
1 parent b9a27c3 commit b17ea2b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Branch-TestBed/Branch-SDK-Tests/BranchLoggerTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ - (void)testLogLevelThresholdBlocksLowerLevels {
3737
XCTestExpectation *expectation = [self expectationWithDescription:@"Log callback expectation for message that should pass the threshold"];
3838

3939
[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) {
40+
if ([message isEqualToString:@"[BranchSDK][Debug][BranchLoggerTests testLogLevelThresholdBlocksLowerLevels] This message should trigger the log callback."] && logLevel >= BranchLogLevelDebug) {
4141
[expectation fulfill];
4242
}
4343
};
@@ -50,7 +50,7 @@ - (void)testLogLevelThresholdBlocksLowerLevels {
5050

5151
- (void)testLogCallbackExecutesWithCorrectParameters {
5252
XCTestExpectation *expectation = [self expectationWithDescription:@"Log callback expectation"];
53-
NSString *expectedMessage = @"[BranchSDK][Info] Test message";
53+
NSString *expectedMessage = @"[BranchSDK][Info][BranchLoggerTests testLogCallbackExecutesWithCorrectParameters] Test message";
5454
BranchLogLevel expectedLevel = BranchLogLevelInfo;
5555

5656
[BranchLogger shared].logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {

BranchSDK/BranchLogger.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ - (void)logMessage:(NSString *)message withLevel:(BranchLogLevel)level error:(NS
4949
return;
5050
}
5151

52+
NSString *callerDetails = [self callingClass];
5253
NSString *logLevelString = [self stringForLogLevel:level];
5354
NSString *logTag = [NSString stringWithFormat:@"[BranchSDK][%@]", logLevelString];
54-
NSMutableString *fullMessage = [NSMutableString stringWithFormat:@"%@ %@", logTag, message];
55+
NSMutableString *fullMessage = [NSMutableString stringWithFormat:@"%@%@ %@", logTag, callerDetails, message];
5556

5657
if (error) {
5758
[fullMessage appendFormat:@", Error: %@ (Domain: %@, Code: %ld)", error.localizedDescription, error.domain, (long)error.code];
@@ -66,6 +67,7 @@ - (void)logMessage:(NSString *)message withLevel:(BranchLogLevel)level error:(NS
6667
}
6768
}
6869

70+
//MARK: Helper Methods
6971
- (os_log_type_t)osLogTypeForBranchLogLevel:(BranchLogLevel)level {
7072
switch (level) {
7173
case BranchLogLevelError: return OS_LOG_TYPE_FAULT;
@@ -88,4 +90,18 @@ - (NSString *)stringForLogLevel:(BranchLogLevel)level {
8890
}
8991
}
9092

93+
- (NSString *)callingClass {
94+
NSArray<NSString *> *stackSymbols = [NSThread callStackSymbols];
95+
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\[([^\\]]+)\\]" options:0 error:nil];
96+
if (stackSymbols.count > 3 && regex) {
97+
NSString *callSite = stackSymbols[3];
98+
NSTextCheckingResult *match = [regex firstMatchInString:callSite options:0 range:NSMakeRange(0, [callSite length])];
99+
if (match && match.range.location != NSNotFound) {
100+
NSString *callerDetails = [callSite substringWithRange:[match rangeAtIndex:0]];
101+
return callerDetails;
102+
}
103+
}
104+
return @"";
105+
}
106+
91107
@end

0 commit comments

Comments
 (0)