[react-native] Revert D2346396 to bring back JS stack traces

This commit is contained in:
Jing Chen 2015-08-19 16:50:39 -07:00
parent ec82ffa52b
commit cc96962e3d
1 changed files with 35 additions and 50 deletions

View File

@ -37,7 +37,7 @@ static RCTLogLevel RCTCurrentLogThreshold;
__attribute__((constructor)) __attribute__((constructor))
static void RCTLogSetup() static void RCTLogSetup()
{ {
RCTSetLogFunction(RCTDefaultLogFunction); RCTCurrentLogFunction = RCTDefaultLogFunction;
#if RCT_DEBUG #if RCT_DEBUG
RCTCurrentLogThreshold = RCTLogLevelInfo - 1; RCTCurrentLogThreshold = RCTLogLevelInfo - 1;
@ -52,7 +52,8 @@ RCTLogFunction RCTDefaultLogFunction = ^(
NSString *fileName, NSString *fileName,
NSNumber *lineNumber, NSNumber *lineNumber,
NSString *message NSString *message
) { )
{
NSString *log = RCTFormatLog( NSString *log = RCTFormatLog(
[NSDate date], level, fileName, lineNumber, message [NSDate date], level, fileName, lineNumber, message
); );
@ -81,52 +82,7 @@ RCTLogFunction RCTDefaultLogFunction = ^(
void RCTSetLogFunction(RCTLogFunction logFunction) 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; RCTCurrentLogFunction = logFunction;
#endif
} }
RCTLogFunction RCTGetLogFunction() RCTLogFunction RCTGetLogFunction()
@ -195,7 +151,8 @@ NSString *RCTFormatLog(
NSString *fileName, NSString *fileName,
NSNumber *lineNumber, NSNumber *lineNumber,
NSString *message NSString *message
) { )
{
NSMutableString *log = [NSMutableString new]; NSMutableString *log = [NSMutableString new];
if (timestamp) { if (timestamp) {
static NSDateFormatter *formatter; static NSDateFormatter *formatter;
@ -231,8 +188,8 @@ void _RCTLogFormat(
RCTLogLevel level, RCTLogLevel level,
const char *fileName, const char *fileName,
int lineNumber, int lineNumber,
NSString *format, ... NSString *format, ...)
) { {
RCTLogFunction logFunction = RCTGetLocalLogFunction(); RCTLogFunction logFunction = RCTGetLocalLogFunction();
BOOL log = RCT_DEBUG || (logFunction != nil); BOOL log = RCT_DEBUG || (logFunction != nil);
if (log && level >= RCTCurrentLogThreshold) { if (log && level >= RCTCurrentLogThreshold) {
@ -247,5 +204,33 @@ void _RCTLogFormat(
if (logFunction) { if (logFunction) {
logFunction(level, fileName ? @(fileName) : nil, (lineNumber >= 0) ? @(lineNumber) : nil, message); 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
} }
} }