[React Native] Bring back console.error redboxes with disable flag

This commit is contained in:
Spencer Ahrens 2015-04-24 18:00:20 -07:00
parent 68eb3e4906
commit 1cc0c96508
2 changed files with 49 additions and 10 deletions

View File

@ -0,0 +1,42 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule stringifySafe
* @flow
*/
'use strict';
/**
* Tries to stringify with JSON.stringify and toString, but catches exceptions
* (e.g. from circular objects) and always returns a string and never throws.
*/
function stringifySafe(arg: any): string {
var ret;
if (arg === undefined) {
ret = 'undefined';
} else if (arg === null) {
ret = 'null';
} else if (typeof arg === 'string') {
ret = '"' + arg + '"';
} else {
// Perform a try catch, just in case the object has a circular
// reference or stringify throws for some other reason.
try {
ret = JSON.stringify(arg);
} catch (e) {
if (typeof arg.toString === 'function') {
try {
ret = arg.toString();
} catch (E) {}
}
}
}
return ret || '["' + typeof arg + '" failed to stringify]';
}
module.exports = stringifySafe;

View File

@ -74,12 +74,12 @@
static JSValueRef RCTNativeLoggingHook(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception)
{
if (argumentCount > 0) {
JSStringRef string = JSValueToStringCopy(context, arguments[0], exception);
if (!string) {
JSStringRef messageRef = JSValueToStringCopy(context, arguments[0], exception);
if (!messageRef) {
return JSValueMakeUndefined(context);
}
NSString *message = (__bridge_transfer NSString *)JSStringCopyCFString(kCFAllocatorDefault, string);
JSStringRelease(string);
NSString *message = (__bridge_transfer NSString *)JSStringCopyCFString(kCFAllocatorDefault, messageRef);
JSStringRelease(messageRef);
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:
@"( stack: )?([_a-z0-9]*)@?(http://|file:///)[a-z.0-9:/_-]+/([a-z0-9_]+).includeRequire.runModule.bundle(:[0-9]+:[0-9]+)"
options:NSRegularExpressionCaseInsensitive
@ -89,14 +89,11 @@ static JSValueRef RCTNativeLoggingHook(JSContextRef context, JSObjectRef object,
range:(NSRange){0, message.length}
withTemplate:@"[$4$5] \t$2"];
// TODO: it would be good if log level was sent as a param, instead of this hack
RCTLogLevel level = RCTLogLevelInfo;
if ([message rangeOfString:@"error" options:NSCaseInsensitiveSearch].length) {
level = RCTLogLevelError;
} else if ([message rangeOfString:@"warning" options:NSCaseInsensitiveSearch].length) {
level = RCTLogLevelWarning;
if (argumentCount > 1) {
level = MAX(level, JSValueToNumber(context, arguments[1], exception) - 1);
}
_RCTLogFormat(level, NULL, -1, @"%@", message);
RCTGetLogFunction()(level, nil, nil, message);
}
return JSValueMakeUndefined(context);