Skip to content

Commit ff4a0d9

Browse files
committed
✨ Add feature to support JS console logs with bug reports
1 parent 1e735d6 commit ff4a0d9

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

ios/RNInstabug/InstabugReactBridge.m

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
#import "InstabugReactBridge.h"
88
#import <Instabug/Instabug.h>
9+
#import <asl.h>
10+
#import <React/RCTLog.h>
11+
#import <os/log.h>
912

1013
@implementation InstabugReactBridge
1114

@@ -28,6 +31,8 @@ - (dispatch_queue_t)methodQueue {
2831

2932
RCT_EXPORT_METHOD(startWithToken:(NSString *)token invocationEvent:(IBGInvocationEvent)invocationEvent) {
3033
[Instabug startWithToken:token invocationEvent:invocationEvent];
34+
RCTAddLogFunction(InstabugReactLogFunction);
35+
RCTSetLogThreshold(RCTLogLevelInfo);
3136
[Instabug setCrashReportingEnabled:NO];
3237
[Instabug setNetworkLoggingEnabled:NO];
3338
}
@@ -433,4 +438,62 @@ - (NSDictionary *)constantsToExport
433438
};
434439
};
435440

441+
+ (BOOL)iOSVersionIsLessThan:(NSString *)iOSVersion {
442+
return [iOSVersion compare:[UIDevice currentDevice].systemVersion options:NSNumericSearch] == NSOrderedDescending;
443+
};
444+
445+
RCTLogFunction InstabugReactLogFunction = ^(
446+
RCTLogLevel level,
447+
__unused RCTLogSource source,
448+
NSString *fileName,
449+
NSNumber *lineNumber,
450+
NSString *message
451+
)
452+
{
453+
NSString *log = RCTFormatLog([NSDate date], level, fileName, lineNumber, message);
454+
455+
456+
NSLog(@"Instabug - REACT LOG: %s", log.UTF8String);
457+
458+
if([InstabugReactBridge iOSVersionIsLessThan:@"10.0"]) {
459+
int aslLevel;
460+
switch(level) {
461+
case RCTLogLevelTrace:
462+
aslLevel = ASL_LEVEL_DEBUG;
463+
break;
464+
case RCTLogLevelInfo:
465+
aslLevel = ASL_LEVEL_NOTICE;
466+
break;
467+
case RCTLogLevelWarning:
468+
aslLevel = ASL_LEVEL_WARNING;
469+
break;
470+
case RCTLogLevelError:
471+
aslLevel = ASL_LEVEL_ERR;
472+
break;
473+
case RCTLogLevelFatal:
474+
aslLevel = ASL_LEVEL_CRIT;
475+
break;
476+
}
477+
asl_log(NULL, NULL, aslLevel, "%s", message.UTF8String);
478+
} else {
479+
switch(level) {
480+
case RCTLogLevelTrace:
481+
os_log(OS_LOG_DEFAULT, "%s", [message UTF8String]);
482+
break;
483+
case RCTLogLevelInfo:
484+
os_log_with_type(OS_LOG_DEFAULT, OS_LOG_TYPE_INFO, "%s", [message UTF8String]);
485+
break;
486+
case RCTLogLevelWarning:
487+
os_log(OS_LOG_DEFAULT, "%s", [message UTF8String]);
488+
break;
489+
case RCTLogLevelError:
490+
os_log_error(OS_LOG_DEFAULT, "%s", [message UTF8String]);
491+
break;
492+
case RCTLogLevelFatal:
493+
os_log_fault(OS_LOG_DEFAULT, "%s", [message UTF8String]);
494+
break;
495+
}
496+
}
497+
};
498+
436499
@end

0 commit comments

Comments
 (0)