2021-06-03 06:16:02 -06:00
|
|
|
//
|
|
|
|
// JSConsoleHelper.mm
|
|
|
|
// VisionCamera
|
|
|
|
//
|
|
|
|
// Created by Marc Rousavy on 02.06.21.
|
|
|
|
// Copyright © 2021 mrousavy. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import "JSConsoleHelper.h"
|
|
|
|
|
|
|
|
#import <React/RCTBridge.h>
|
|
|
|
#import <ReactCommon/RCTTurboModule.h>
|
|
|
|
#import <React/RCTBridge+Private.h>
|
|
|
|
#import <jsi/jsi.h>
|
|
|
|
#import "RCTBridge+runOnJS.h"
|
|
|
|
|
|
|
|
@implementation JSConsoleHelper
|
|
|
|
|
|
|
|
+ (const char *) getLogFunctionNameForLogLevel:(RCTLogLevel)level {
|
|
|
|
switch (level) {
|
|
|
|
case RCTLogLevelTrace:
|
|
|
|
return "trace";
|
|
|
|
case RCTLogLevelInfo:
|
|
|
|
return "log";
|
|
|
|
case RCTLogLevelWarning:
|
|
|
|
return "warn";
|
|
|
|
case RCTLogLevelError:
|
|
|
|
case RCTLogLevelFatal:
|
|
|
|
return "error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (ConsoleLogFunction) getLogFunctionForBridge:(RCTBridge*)bridge {
|
|
|
|
RCTCxxBridge *cxxBridge = (RCTCxxBridge *)bridge;
|
|
|
|
if (!cxxBridge.runtime) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2022-07-05 02:51:24 -06:00
|
|
|
facebook::jsi::Runtime* jsiRuntime = (facebook::jsi::Runtime*)cxxBridge.runtime;
|
2021-06-03 06:16:02 -06:00
|
|
|
|
|
|
|
return ^(RCTLogLevel level, NSString* message) {
|
|
|
|
[bridge runOnJS:^{
|
|
|
|
if (jsiRuntime != nullptr) {
|
2022-07-05 02:51:24 -06:00
|
|
|
facebook::jsi::Runtime& runtime = *jsiRuntime;
|
2021-06-03 06:16:02 -06:00
|
|
|
auto logFunctionName = [JSConsoleHelper getLogFunctionNameForLogLevel:level];
|
|
|
|
try {
|
|
|
|
auto console = runtime.global().getPropertyAsObject(runtime, "console");
|
|
|
|
auto log = console.getPropertyAsFunction(runtime, logFunctionName);
|
2022-07-05 02:51:24 -06:00
|
|
|
log.call(runtime, facebook::jsi::String::createFromAscii(runtime, [message UTF8String]));
|
|
|
|
} catch (facebook::jsi::JSError& jsError) {
|
2021-06-03 06:16:02 -06:00
|
|
|
NSLog(@"%@", message);
|
|
|
|
NSLog(@"Failed to call `console.%s`: %s", logFunctionName, jsError.getMessage().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|