1. Console 实现原理

1.1. JS

  • [console.js] global.console > getNativeLogFunction > global.nativeLoggingHook

1.2. iOS

  • [JSCExecutorFactory.cpp] createJSExecutor > bindNativeLogger
  • [RCTLog.mm] _RCTLogJavaScriptInternal -- 判断日志等级 RCTGetLogThreshold() --> RCTGetLocalLogFunction > RCTGetLogFunction

1.3. Android

  • [OnLoad.cpp] installBindings > bindNativeLogger > [JSCLogging.cpp] reactAndroidLoggingHook

1.3.1. RCTLog.mm

打印日志函数

RCTLogFunction RCTDefaultLogFunction = ^(
  RCTLogLevel level,
  __unused RCTLogSource source,
  NSString *fileName,
  NSNumber *lineNumber,
  NSString *message
)
{
  NSString *log = RCTFormatLog([NSDate date], level, fileName, lineNumber, message);
  fprintf(stderr, "%s\n", log.UTF8String);
  fflush(stderr);
};

1.3.2. RCTAssert.m

assert 会抛异常

void RCTFatal(NSError *error)
{
  @throw [[NSException alloc]  initWithName:name reason:message userInfo:userInfo];
}

1.4. JS

JS

iOS

results matching ""

    No results matching ""