diff --git a/React/Base/RCTLog.m b/React/Base/RCTLog.m index 0cb095494..3fa343303 100644 --- a/React/Base/RCTLog.m +++ b/React/Base/RCTLog.m @@ -37,7 +37,7 @@ static RCTLogLevel RCTCurrentLogThreshold; __attribute__((constructor)) static void RCTLogSetup() { - RCTCurrentLogFunction = RCTDefaultLogFunction; + RCTSetLogFunction(RCTDefaultLogFunction); #if RCT_DEBUG RCTCurrentLogThreshold = RCTLogLevelInfo - 1; @@ -52,8 +52,7 @@ RCTLogFunction RCTDefaultLogFunction = ^( NSString *fileName, NSNumber *lineNumber, NSString *message -) -{ +) { NSString *log = RCTFormatLog( [NSDate date], level, fileName, lineNumber, message ); @@ -82,7 +81,52 @@ RCTLogFunction RCTDefaultLogFunction = ^( void RCTSetLogFunction(RCTLogFunction logFunction) { + +#if RCT_DEBUG // Red box is only available in debug mode + + RCTCurrentLogFunction = ^( + RCTLogLevel level, + NSString *fileName, + NSNumber *lineNumber, + NSString *message + ) { + // Log to red box + if ([UIApplication sharedApplication] && level >= RCTLOG_REDBOX_LEVEL) { + NSArray *stackSymbols = [NSThread callStackSymbols]; + NSMutableArray *stack = [NSMutableArray arrayWithCapacity:(stackSymbols.count - 1)]; + [stackSymbols enumerateObjectsUsingBlock:^(NSString *frameSymbols, NSUInteger idx, __unused BOOL *stop) { + if (idx > 0) { // don't include the current frame + NSString *address = [[frameSymbols componentsSeparatedByString:@"0x"][1] componentsSeparatedByString:@" "][0]; + NSRange addressRange = [frameSymbols rangeOfString:address]; + NSString *methodName = [frameSymbols substringFromIndex:(addressRange.location + addressRange.length + 1)]; + if (idx == 1 && fileName && lineNumber) { + [stack addObject:@{ + @"methodName": methodName, + @"file": fileName.lastPathComponent, + @"lineNumber": lineNumber + }]; + } else { + [stack addObject:@{@"methodName": methodName}]; + } + } + }]; + [[RCTRedBox sharedInstance] showErrorMessage:message withStack:stack]; + } + + // Log to JS executor + [RCTBridge logMessage:message level:level ? @(RCTLogLevels[level - 1]) : @"info"]; + + if (logFunction) { + logFunction(level, fileName, lineNumber, message); + } + }; + +#else + RCTCurrentLogFunction = logFunction; + +#endif + } RCTLogFunction RCTGetLogFunction() @@ -151,8 +195,7 @@ NSString *RCTFormatLog( NSString *fileName, NSNumber *lineNumber, NSString *message -) -{ +) { NSMutableString *log = [[NSMutableString alloc] init]; if (timestamp) { static NSDateFormatter *formatter; @@ -188,8 +231,8 @@ void _RCTLogFormat( RCTLogLevel level, const char *fileName, int lineNumber, - NSString *format, ...) -{ + NSString *format, ... +) { RCTLogFunction logFunction = RCTGetLocalLogFunction(); BOOL log = RCT_DEBUG || (logFunction != nil); if (log && level >= RCTCurrentLogThreshold) { @@ -204,33 +247,5 @@ void _RCTLogFormat( if (logFunction) { logFunction(level, fileName ? @(fileName) : nil, (lineNumber >= 0) ? @(lineNumber) : nil, message); } - -#if RCT_DEBUG // Red box is only available in debug mode - - // Log to red box - if ([UIApplication sharedApplication] && level >= RCTLOG_REDBOX_LEVEL) { - NSArray *stackSymbols = [NSThread callStackSymbols]; - NSMutableArray *stack = [NSMutableArray arrayWithCapacity:(stackSymbols.count - 1)]; - [stackSymbols enumerateObjectsUsingBlock:^(NSString *frameSymbols, NSUInteger idx, __unused BOOL *stop) { - if (idx > 0) { // don't include the current frame - NSString *address = [[frameSymbols componentsSeparatedByString:@"0x"][1] componentsSeparatedByString:@" "][0]; - NSRange addressRange = [frameSymbols rangeOfString:address]; - NSString *methodName = [frameSymbols substringFromIndex:(addressRange.location + addressRange.length + 1)]; - if (idx == 1) { - NSString *file = [[@(fileName) componentsSeparatedByString:@"/"] lastObject]; - [stack addObject:@{@"methodName": methodName, @"file": file, @"lineNumber": @(lineNumber)}]; - } else { - [stack addObject:@{@"methodName": methodName}]; - } - } - }]; - [[RCTRedBox sharedInstance] showErrorMessage:message withStack:stack]; - } - - // Log to JS executor - [RCTBridge logMessage:message level:level ? @(RCTLogLevels[level - 1]) : @"info"]; - -#endif - } }