Sometimes console.log shows in log-ios sometimes it doesn't

試著忘記壹切 提交于 2019-11-29 20:04:18

问题


I have run react-native start in one terminal, and then react-native ios-run in another. My initial console.log rarely show, sometimes they do.

Lot's of times, randomly I do see:

LOG MESSAGE QUOTA EXCEEDED - SOME MESSAGES FROM THIS PROCESS HAVE BEEN DISCARDED

Are my console.log's being discarded? I tried clearing the console to see it more clearly but I can't find a way to clear console either.

On Android, I wouldn't have issue with missing console.log.


回答1:


react-native logs information using syslog daemon. This daemon attempts to prevent spamming to the log (DoS attack). These limits are set on per process basis.

The simple solution is to stop/start simulator and you will be obtain new process that is not limited by the previous behaviour.

The other solution is to disable syslogd limits what will be heavilly depends on your operation system.




回答2:


i found that the JavascriptCore engine won't automatically redirect the console.log to either XCode output panel or the system builtin Console.App, not to mention the self-broken log-ios command.

the only way to see console.log without remote debugging in browser is redirect(bind) it ourselves:

//Add this headers
#import <JavaScriptCore/JavaScriptCore.h>
#import <jschelpers/JavaScriptCore. h>
#import <React/RCTBridge+Private.h>
...
...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

  NSURL *jsCodeLocation;
  ...
  ...
  [self.window makeKeyAndVisible];


  ::sleep(2); //<---!!!!!see below

  JSGlobalContextRef globalContext = rootView.bridge.jsContextRef;
  JSContext *context = [JSC_JSContext(globalContext) contextWithJSGlobalContextRef:globalContext];
  context[@"console"][@"log"] = ^(NSString *message) {
    NSLog(@"Javascript log: %@",message);
  };
  return YES;
}

Caution: the JSContext within reactInstance is created in another thread, I don't know how to get the loaded event(in only my project, since i don't like to modify the react-native engine), just wait sometime here for testing purpose.



来源:https://stackoverflow.com/questions/43288281/sometimes-console-log-shows-in-log-ios-sometimes-it-doesnt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!