2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTLog.h"
|
|
|
|
|
2015-07-13 16:38:53 +00:00
|
|
|
#include <asl.h>
|
2016-11-14 12:49:19 +00:00
|
|
|
#include <cxxabi.h>
|
2015-07-13 16:38:53 +00:00
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
#import "RCTAssert.h"
|
2015-12-15 13:39:30 +00:00
|
|
|
#import "RCTBridge+Private.h"
|
2017-01-13 11:37:15 +00:00
|
|
|
#import "RCTBridge.h"
|
2015-04-21 12:26:51 +00:00
|
|
|
#import "RCTDefines.h"
|
2015-04-07 14:36:26 +00:00
|
|
|
#import "RCTRedBox.h"
|
2016-03-01 17:44:05 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-07-29 12:54:59 +00:00
|
|
|
static NSString *const RCTLogFunctionStack = @"RCTLogFunctionStack";
|
2015-04-07 14:36:26 +00:00
|
|
|
|
|
|
|
const char *RCTLogLevels[] = {
|
2015-11-05 20:20:08 +00:00
|
|
|
"trace",
|
2015-04-07 14:36:26 +00:00
|
|
|
"info",
|
|
|
|
"warn",
|
|
|
|
"error",
|
2015-11-05 20:20:08 +00:00
|
|
|
"fatal",
|
2015-04-07 14:36:26 +00:00
|
|
|
};
|
|
|
|
|
2015-09-28 11:20:20 +00:00
|
|
|
#if RCT_DEBUG
|
2016-11-14 12:49:19 +00:00
|
|
|
static const RCTLogLevel RCTDefaultLogThreshold = (RCTLogLevel)(RCTLogLevelInfo - 1);
|
2015-09-28 11:20:20 +00:00
|
|
|
#else
|
|
|
|
static const RCTLogLevel RCTDefaultLogThreshold = RCTLogLevelError;
|
|
|
|
#endif
|
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
static RCTLogFunction RCTCurrentLogFunction;
|
2015-09-28 11:20:20 +00:00
|
|
|
static RCTLogLevel RCTCurrentLogThreshold = RCTDefaultLogThreshold;
|
2015-04-07 14:36:26 +00:00
|
|
|
|
2015-09-13 18:08:44 +00:00
|
|
|
RCTLogLevel RCTGetLogThreshold()
|
2015-04-07 14:36:26 +00:00
|
|
|
{
|
2015-09-13 18:08:44 +00:00
|
|
|
return RCTCurrentLogThreshold;
|
2015-04-07 14:36:26 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 11:20:20 +00:00
|
|
|
void RCTSetLogThreshold(RCTLogLevel threshold) {
|
|
|
|
RCTCurrentLogThreshold = threshold;
|
|
|
|
}
|
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
RCTLogFunction RCTDefaultLogFunction = ^(
|
|
|
|
RCTLogLevel level,
|
2015-11-14 18:25:00 +00:00
|
|
|
__unused RCTLogSource source,
|
2015-04-07 14:36:26 +00:00
|
|
|
NSString *fileName,
|
|
|
|
NSNumber *lineNumber,
|
|
|
|
NSString *message
|
2015-08-19 23:50:39 +00:00
|
|
|
)
|
|
|
|
{
|
2015-11-11 14:42:27 +00:00
|
|
|
NSString *log = RCTFormatLog([NSDate date], level, fileName, lineNumber, message);
|
2015-04-07 14:36:26 +00:00
|
|
|
fprintf(stderr, "%s\n", log.UTF8String);
|
2015-04-09 23:44:15 +00:00
|
|
|
fflush(stderr);
|
2015-07-13 16:38:53 +00:00
|
|
|
|
2015-11-05 20:19:56 +00:00
|
|
|
int aslLevel;
|
2015-07-13 16:38:53 +00:00
|
|
|
switch(level) {
|
2015-11-05 20:20:08 +00:00
|
|
|
case RCTLogLevelTrace:
|
|
|
|
aslLevel = ASL_LEVEL_DEBUG;
|
|
|
|
break;
|
2015-07-13 16:38:53 +00:00
|
|
|
case RCTLogLevelInfo:
|
|
|
|
aslLevel = ASL_LEVEL_NOTICE;
|
|
|
|
break;
|
|
|
|
case RCTLogLevelWarning:
|
|
|
|
aslLevel = ASL_LEVEL_WARNING;
|
|
|
|
break;
|
|
|
|
case RCTLogLevelError:
|
|
|
|
aslLevel = ASL_LEVEL_ERR;
|
|
|
|
break;
|
2015-11-05 20:19:56 +00:00
|
|
|
case RCTLogLevelFatal:
|
|
|
|
aslLevel = ASL_LEVEL_CRIT;
|
2015-07-13 16:38:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
asl_log(NULL, NULL, aslLevel, "%s", message.UTF8String);
|
2015-03-01 23:33:55 +00:00
|
|
|
};
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
void RCTSetLogFunction(RCTLogFunction logFunction)
|
|
|
|
{
|
|
|
|
RCTCurrentLogFunction = logFunction;
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
RCTLogFunction RCTGetLogFunction()
|
|
|
|
{
|
2015-09-13 18:08:44 +00:00
|
|
|
if (!RCTCurrentLogFunction) {
|
|
|
|
RCTCurrentLogFunction = RCTDefaultLogFunction;
|
|
|
|
}
|
2015-04-07 14:36:26 +00:00
|
|
|
return RCTCurrentLogFunction;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
void RCTAddLogFunction(RCTLogFunction logFunction)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-09-13 18:08:44 +00:00
|
|
|
RCTLogFunction existing = RCTGetLogFunction();
|
2015-04-07 14:36:26 +00:00
|
|
|
if (existing) {
|
2015-11-11 14:42:27 +00:00
|
|
|
RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
|
|
|
|
existing(level, source, fileName, lineNumber, message);
|
|
|
|
logFunction(level, source, fileName, lineNumber, message);
|
2015-09-13 18:08:44 +00:00
|
|
|
});
|
2015-04-07 14:36:26 +00:00
|
|
|
} else {
|
2015-09-13 18:08:44 +00:00
|
|
|
RCTSetLogFunction(logFunction);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 12:54:59 +00:00
|
|
|
/**
|
|
|
|
* returns the topmost stacked log function for the current thread, which
|
|
|
|
* may not be the same as the current value of RCTCurrentLogFunction.
|
|
|
|
*/
|
|
|
|
static RCTLogFunction RCTGetLocalLogFunction()
|
|
|
|
{
|
|
|
|
NSMutableDictionary *threadDictionary = [NSThread currentThread].threadDictionary;
|
2015-11-03 22:45:46 +00:00
|
|
|
NSArray<RCTLogFunction> *functionStack = threadDictionary[RCTLogFunctionStack];
|
2015-08-24 10:14:33 +00:00
|
|
|
RCTLogFunction logFunction = functionStack.lastObject;
|
2015-07-29 12:54:59 +00:00
|
|
|
if (logFunction) {
|
|
|
|
return logFunction;
|
|
|
|
}
|
2015-09-13 18:08:44 +00:00
|
|
|
return RCTGetLogFunction();
|
2015-07-29 12:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RCTPerformBlockWithLogFunction(void (^block)(void), RCTLogFunction logFunction)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-07 14:36:26 +00:00
|
|
|
NSMutableDictionary *threadDictionary = [NSThread currentThread].threadDictionary;
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<RCTLogFunction> *functionStack = threadDictionary[RCTLogFunctionStack];
|
2015-07-29 12:54:59 +00:00
|
|
|
if (!functionStack) {
|
2015-08-17 14:35:34 +00:00
|
|
|
functionStack = [NSMutableArray new];
|
2015-07-29 12:54:59 +00:00
|
|
|
threadDictionary[RCTLogFunctionStack] = functionStack;
|
2015-04-07 14:36:26 +00:00
|
|
|
}
|
2015-07-29 12:54:59 +00:00
|
|
|
[functionStack addObject:logFunction];
|
2015-04-07 14:36:26 +00:00
|
|
|
block();
|
2015-07-29 12:54:59 +00:00
|
|
|
[functionStack removeLastObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
void RCTPerformBlockWithLogPrefix(void (^block)(void), NSString *prefix)
|
|
|
|
{
|
|
|
|
RCTLogFunction logFunction = RCTGetLocalLogFunction();
|
|
|
|
if (logFunction) {
|
2015-11-11 14:42:27 +00:00
|
|
|
RCTPerformBlockWithLogFunction(block, ^(RCTLogLevel level, RCTLogSource source,
|
|
|
|
NSString *fileName, NSNumber *lineNumber,
|
|
|
|
NSString *message) {
|
|
|
|
logFunction(level, source, fileName, lineNumber, [prefix stringByAppendingString:message]);
|
2015-07-29 12:54:59 +00:00
|
|
|
});
|
|
|
|
}
|
2015-04-07 14:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NSString *RCTFormatLog(
|
|
|
|
NSDate *timestamp,
|
|
|
|
RCTLogLevel level,
|
|
|
|
NSString *fileName,
|
|
|
|
NSNumber *lineNumber,
|
|
|
|
NSString *message
|
2015-08-19 23:50:39 +00:00
|
|
|
)
|
|
|
|
{
|
2015-08-17 14:35:34 +00:00
|
|
|
NSMutableString *log = [NSMutableString new];
|
2015-04-07 14:36:26 +00:00
|
|
|
if (timestamp) {
|
|
|
|
static NSDateFormatter *formatter;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2015-08-17 14:35:34 +00:00
|
|
|
formatter = [NSDateFormatter new];
|
2015-04-07 14:36:26 +00:00
|
|
|
formatter.dateFormat = formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS ";
|
|
|
|
});
|
|
|
|
[log appendString:[formatter stringFromDate:timestamp]];
|
|
|
|
}
|
|
|
|
if (level) {
|
2016-01-20 18:27:34 +00:00
|
|
|
[log appendFormat:@"[%s]", RCTLogLevels[level]];
|
2015-04-07 14:36:26 +00:00
|
|
|
}
|
2015-05-25 12:19:53 +00:00
|
|
|
|
|
|
|
[log appendFormat:@"[tid:%@]", RCTCurrentThreadName()];
|
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
if (fileName) {
|
2015-08-24 10:14:33 +00:00
|
|
|
fileName = fileName.lastPathComponent;
|
2015-04-07 14:36:26 +00:00
|
|
|
if (lineNumber) {
|
|
|
|
[log appendFormat:@"[%@:%@]", fileName, lineNumber];
|
2015-02-20 04:10:52 +00:00
|
|
|
} else {
|
2015-04-07 14:36:26 +00:00
|
|
|
[log appendFormat:@"[%@]", fileName];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-07 14:36:26 +00:00
|
|
|
if (message) {
|
|
|
|
[log appendString:@" "];
|
|
|
|
[log appendString:message];
|
|
|
|
}
|
|
|
|
return log;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 12:49:19 +00:00
|
|
|
static NSRegularExpression *nativeStackFrameRegex()
|
|
|
|
{
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
static NSRegularExpression *_regex;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
NSError *regexError;
|
|
|
|
_regex = [NSRegularExpression regularExpressionWithPattern:@"0x[0-9a-f]+ (.*) \\+ (\\d+)$" options:0 error:®exError];
|
|
|
|
if (regexError) {
|
|
|
|
RCTLogError(@"Failed to build regex: %@", [regexError localizedDescription]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return _regex;
|
|
|
|
}
|
|
|
|
|
2015-11-11 14:42:27 +00:00
|
|
|
void _RCTLogNativeInternal(RCTLogLevel level, const char *fileName, int lineNumber, NSString *format, ...)
|
2015-08-19 23:50:39 +00:00
|
|
|
{
|
2015-07-29 12:54:59 +00:00
|
|
|
RCTLogFunction logFunction = RCTGetLocalLogFunction();
|
|
|
|
BOOL log = RCT_DEBUG || (logFunction != nil);
|
2015-09-13 18:08:44 +00:00
|
|
|
if (log && level >= RCTGetLogThreshold()) {
|
2015-04-07 14:36:26 +00:00
|
|
|
// Get message
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2015-04-11 22:08:00 +00:00
|
|
|
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
|
2015-04-07 14:36:26 +00:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
// Call log function
|
2015-07-29 12:54:59 +00:00
|
|
|
if (logFunction) {
|
2015-11-11 14:42:27 +00:00
|
|
|
logFunction(level, RCTLogSourceNative, fileName ? @(fileName) : nil, lineNumber > 0 ? @(lineNumber) : nil, message);
|
2015-07-29 12:54:59 +00:00
|
|
|
}
|
2015-08-19 23:50:39 +00:00
|
|
|
|
2015-11-05 20:20:08 +00:00
|
|
|
#if RCT_DEBUG
|
2015-12-08 11:29:08 +00:00
|
|
|
|
2015-11-05 20:20:08 +00:00
|
|
|
// Log to red box in debug mode.
|
2015-08-19 23:50:39 +00:00
|
|
|
if ([UIApplication sharedApplication] && level >= RCTLOG_REDBOX_LEVEL) {
|
2015-11-03 22:45:46 +00:00
|
|
|
NSArray<NSString *> *stackSymbols = [NSThread callStackSymbols];
|
|
|
|
NSMutableArray<NSDictionary *> *stack =
|
|
|
|
[NSMutableArray arrayWithCapacity:(stackSymbols.count - 1)];
|
2015-08-19 23:50:39 +00:00
|
|
|
[stackSymbols enumerateObjectsUsingBlock:^(NSString *frameSymbols, NSUInteger idx, __unused BOOL *stop) {
|
2016-11-14 12:49:19 +00:00
|
|
|
if (idx == 0) {
|
|
|
|
// don't include the current frame
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSRange range = NSMakeRange(0, frameSymbols.length);
|
2017-01-13 11:37:15 +00:00
|
|
|
NSTextCheckingResult *match = [nativeStackFrameRegex() firstMatchInString:frameSymbols options:0 range:range];
|
2016-11-14 12:49:19 +00:00
|
|
|
if (!match) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *methodName = [frameSymbols substringWithRange:[match rangeAtIndex:1]];
|
|
|
|
char *demangledName = abi::__cxa_demangle([methodName UTF8String], NULL, NULL, NULL);
|
|
|
|
if (demangledName) {
|
|
|
|
methodName = @(demangledName);
|
|
|
|
free(demangledName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx == 1 && fileName) {
|
|
|
|
NSString *file = [@(fileName) componentsSeparatedByString:@"/"].lastObject;
|
|
|
|
[stack addObject:@{@"methodName": methodName, @"file": file, @"lineNumber": @(lineNumber)}];
|
|
|
|
} else {
|
|
|
|
[stack addObject:@{@"methodName": methodName}];
|
2015-08-19 23:50:39 +00:00
|
|
|
}
|
|
|
|
}];
|
2016-11-14 12:49:19 +00:00
|
|
|
|
2015-08-26 16:28:14 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
// red box is thread safe, but by deferring to main queue we avoid a startup
|
|
|
|
// race condition that causes the module to be accessed before it has loaded
|
|
|
|
[[RCTBridge currentBridge].redBox showErrorMessage:message withStack:stack];
|
|
|
|
});
|
2015-08-19 23:50:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 17:44:05 +00:00
|
|
|
if (!RCTRunningInTestEnvironment()) {
|
|
|
|
// Log to JS executor
|
|
|
|
[[RCTBridge currentBridge] logMessage:message level:level ? @(RCTLogLevels[level]) : @"info"];
|
|
|
|
}
|
2015-12-08 11:29:08 +00:00
|
|
|
|
2015-08-19 23:50:39 +00:00
|
|
|
#endif
|
2015-12-08 11:29:08 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-04-07 14:36:26 +00:00
|
|
|
}
|
2015-11-11 14:42:27 +00:00
|
|
|
|
|
|
|
void _RCTLogJavaScriptInternal(RCTLogLevel level, NSString *message)
|
|
|
|
{
|
|
|
|
RCTLogFunction logFunction = RCTGetLocalLogFunction();
|
|
|
|
BOOL log = RCT_DEBUG || (logFunction != nil);
|
|
|
|
if (log && level >= RCTGetLogThreshold()) {
|
|
|
|
if (logFunction) {
|
|
|
|
logFunction(level, RCTLogSourceJavaScript, nil, nil, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|