Provide log source to handler

Reviewed By: majak

Differential Revision: D2615567

fb-gh-sync-id: a245813220436f2d83ae2ae28bde916cecb3f416
This commit is contained in:
Pieter De Baets 2015-11-11 06:42:27 -08:00 committed by facebook-github-bot-0
parent 86c3e385ef
commit 215872c2eb
6 changed files with 38 additions and 25 deletions

View File

@ -23,6 +23,7 @@ static BOOL RCTLogsError(void (^block)(void))
{
__block BOOL loggedError = NO;
RCTPerformBlockWithLogFunction(block, ^(RCTLogLevel level,
__unused RCTLogSource source,
__unused NSString *fileName,
__unused NSNumber *lineNumber,
__unused NSString *message) {

View File

@ -86,7 +86,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
@autoreleasepool {
__block NSString *error = nil;
RCTSetLogFunction(^(RCTLogLevel level, NSString *fileName, NSNumber *lineNumber, NSString *message) {
RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
if (level >= RCTLogLevelError) {
error = message;
}

View File

@ -119,7 +119,7 @@ void _RCTAssertFormat(
void RCTFatal(NSError *error)
{
_RCTLogInternal(RCTLogLevelFatal, NULL, 0, @"%@", [error localizedDescription]);
_RCTLogNativeInternal(RCTLogLevelFatal, NULL, 0, @"%@", [error localizedDescription]);
RCTFatalHandler fatalHandler = RCTGetFatalHandler();
if (fatalHandler) {

View File

@ -45,6 +45,14 @@ typedef NS_ENUM(NSInteger, RCTLogLevel) {
RCTLogLevelFatal = 4
};
/**
* An enum representing the source of a log message.
*/
typedef NS_ENUM(NSInteger, RCTLogSource) {
RCTLogSourceNative = 1,
RCTLogSourceJavaScript = 2
};
/**
* A block signature to be used for custom logging functions. In most cases you
* will want to pass these arguments to the RCTFormatLog function in order to
@ -52,6 +60,7 @@ typedef NS_ENUM(NSInteger, RCTLogLevel) {
*/
typedef void (^RCTLogFunction)(
RCTLogLevel level,
RCTLogSource source,
NSString *fileName,
NSNumber *lineNumber,
NSString *message
@ -115,9 +124,10 @@ RCT_EXTERN void RCTPerformBlockWithLogPrefix(void (^block)(void), NSString *pref
* Private logging function - ignore this.
*/
#if RCTLOG_ENABLED
#define _RCTLog(lvl, ...) _RCTLogInternal(lvl, __FILE__, __LINE__, __VA_ARGS__);
#define _RCTLog(lvl, ...) _RCTLogNativeInternal(lvl, __FILE__, __LINE__, __VA_ARGS__);
#else
#define _RCTLog(lvl, ...) do { } while (0)
#endif
RCT_EXTERN void _RCTLogInternal(RCTLogLevel, const char *, int, NSString *, ...) NS_FORMAT_FUNCTION(4,5);
RCT_EXTERN void _RCTLogNativeInternal(RCTLogLevel, const char *, int, NSString *, ...) NS_FORMAT_FUNCTION(4,5);
RCT_EXTERN void _RCTLogJavaScriptInternal(RCTLogLevel, NSString *);

View File

@ -53,14 +53,13 @@ void RCTSetLogThreshold(RCTLogLevel threshold) {
RCTLogFunction RCTDefaultLogFunction = ^(
RCTLogLevel level,
RCTLogSource source,
NSString *fileName,
NSNumber *lineNumber,
NSString *message
)
{
NSString *log = RCTFormatLog(
[NSDate date], level, fileName, lineNumber, message
);
NSString *log = RCTFormatLog([NSDate date], level, fileName, lineNumber, message);
fprintf(stderr, "%s\n", log.UTF8String);
fflush(stderr);
@ -102,13 +101,9 @@ void RCTAddLogFunction(RCTLogFunction logFunction)
{
RCTLogFunction existing = RCTGetLogFunction();
if (existing) {
RCTSetLogFunction(^(RCTLogLevel level,
NSString *fileName,
NSNumber *lineNumber,
NSString *message) {
existing(level, fileName, lineNumber, message);
logFunction(level, fileName, lineNumber, message);
RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
existing(level, source, fileName, lineNumber, message);
logFunction(level, source, fileName, lineNumber, message);
});
} else {
RCTSetLogFunction(logFunction);
@ -147,8 +142,10 @@ void RCTPerformBlockWithLogPrefix(void (^block)(void), NSString *prefix)
{
RCTLogFunction logFunction = RCTGetLocalLogFunction();
if (logFunction) {
RCTPerformBlockWithLogFunction(block, ^(RCTLogLevel level, NSString *fileName, NSNumber *lineNumber, NSString *message) {
logFunction(level, fileName, lineNumber, [prefix stringByAppendingString:message]);
RCTPerformBlockWithLogFunction(block, ^(RCTLogLevel level, RCTLogSource source,
NSString *fileName, NSNumber *lineNumber,
NSString *message) {
logFunction(level, source, fileName, lineNumber, [prefix stringByAppendingString:message]);
});
}
}
@ -192,17 +189,11 @@ NSString *RCTFormatLog(
return log;
}
void _RCTLogInternal(
RCTLogLevel level,
const char *fileName,
int lineNumber,
NSString *format, ...
)
void _RCTLogNativeInternal(RCTLogLevel level, const char *fileName, int lineNumber, NSString *format, ...)
{
RCTLogFunction logFunction = RCTGetLocalLogFunction();
BOOL log = RCT_DEBUG || (logFunction != nil);
if (log && level >= RCTGetLogThreshold()) {
// Get message
va_list args;
va_start(args, format);
@ -211,7 +202,7 @@ void _RCTLogInternal(
// Call log function
if (logFunction) {
logFunction(level, fileName ? @(fileName) : nil, (lineNumber >= 0) ? @(lineNumber) : nil, message);
logFunction(level, RCTLogSourceNative, fileName ? @(fileName) : nil, lineNumber > 0 ? @(lineNumber) : nil, message);
}
#if RCT_DEBUG
@ -245,3 +236,14 @@ void _RCTLogInternal(
#endif
}
}
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);
}
}
}

View File

@ -126,7 +126,7 @@ static JSValueRef RCTNativeLoggingHook(JSContextRef context, __unused JSObjectRe
level = MAX(level, JSValueToNumber(context, arguments[1], exception));
}
_RCTLog(level, @"%@", message);
_RCTLogJavaScriptInternal(level, message);
}
return JSValueMakeUndefined(context);