iOS:add monitor

This commit is contained in:
pengfei.zhou 2020-01-11 11:01:38 +08:00 committed by osborn
parent 0aaf105f53
commit caea154377
9 changed files with 96 additions and 19 deletions

View File

@ -76,8 +76,8 @@ public class DoricNativeDriver implements IDoricDriver {
try { try {
return doricJSEngine.invokeDoricMethod(method, args); return doricJSEngine.invokeDoricMethod(method, args);
} catch (Exception e) { } catch (Exception e) {
doricJSEngine.onException(e); doricJSEngine.getRegistry().onException(e);
doricJSEngine.onLog(Log.ERROR, String.format("invokeDoricMethod(%s,...),error is %s", method, e.getLocalizedMessage())); doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("invokeDoricMethod(%s,...),error is %s", method, e.getLocalizedMessage()));
return new JSDecoder(null); return new JSDecoder(null);
} }
} }
@ -106,8 +106,8 @@ public class DoricNativeDriver implements IDoricDriver {
doricJSEngine.prepareContext(contextId, script, source); doricJSEngine.prepareContext(contextId, script, source);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
doricJSEngine.onException(e); doricJSEngine.getRegistry().onException(e);
doricJSEngine.onLog(Log.ERROR, String.format("createContext %s error is %s", source, e.getLocalizedMessage())); doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("createContext %s error is %s", source, e.getLocalizedMessage()));
return false; return false;
} }
} }
@ -123,8 +123,8 @@ public class DoricNativeDriver implements IDoricDriver {
doricJSEngine.destroyContext(contextId); doricJSEngine.destroyContext(contextId);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
doricJSEngine.onException(e); doricJSEngine.getRegistry().onException(e);
doricJSEngine.onLog(Log.ERROR, String.format("destroyContext %s error is %s", contextId, e.getLocalizedMessage())); doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("destroyContext %s error is %s", contextId, e.getLocalizedMessage()));
return false; return false;
} }
} }

View File

@ -27,4 +27,5 @@
#import "DoricViewController.h" #import "DoricViewController.h"
#import "DoricPromise.h" #import "DoricPromise.h"
#import "DoricLibrary.h" #import "DoricLibrary.h"
#import "DoricNativePlugin.h" #import "DoricNativePlugin.h"
#import "DoricMonitorProtocol.h"

View File

@ -78,6 +78,7 @@ + (instancetype)instance {
[ret setupResult:jsValue]; [ret setupResult:jsValue];
} @catch (NSException *exception) { } @catch (NSException *exception) {
[ret setupError:exception]; [ret setupError:exception];
[self.jsExecutor.registry onException:exception];
} }
}); });
return ret; return ret;
@ -110,6 +111,7 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
[ret setupResult:jsValue]; [ret setupResult:jsValue];
} @catch (NSException *exception) { } @catch (NSException *exception) {
[ret setupError:exception]; [ret setupError:exception];
[self.jsExecutor.registry onException:exception];
} }
}); });
return ret; return ret;
@ -132,6 +134,7 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
[ret setupResult:jsValue]; [ret setupResult:jsValue];
} @catch (NSException *exception) { } @catch (NSException *exception) {
[ret setupError:exception]; [ret setupError:exception];
[self.jsExecutor.registry onException:exception];
} }
}); });
return ret; return ret;
@ -145,9 +148,10 @@ - (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)scr
if (!self) return; if (!self) return;
@try { @try {
[self.jsExecutor prepareContext:contextId script:script source:source]; [self.jsExecutor prepareContext:contextId script:script source:source];
[ret setupResult:[NSNumber numberWithBool:YES]]; [ret setupResult:@YES];
} @catch (NSException *exception) { } @catch (NSException *exception) {
[ret setupError:exception]; [ret setupError:exception];
[self.jsExecutor.registry onException:exception];
} }
}); });
return ret; return ret;
@ -161,9 +165,10 @@ - (DoricAsyncResult *)destroyContext:(NSString *)contextId {
if (!self) return; if (!self) return;
@try { @try {
[self.jsExecutor destroyContext:contextId]; [self.jsExecutor destroyContext:contextId];
[ret setupResult:[NSNumber numberWithBool:YES]]; [ret setupResult:@YES];
} @catch (NSException *exception) { } @catch (NSException *exception) {
[ret setupError:exception]; [ret setupError:exception];
[self.jsExecutor.registry onException:exception];
} }
}); });
return ret; return ret;

View File

@ -0,0 +1,17 @@
//
// Created by pengfei.zhou on 2020/1/10.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, DoricLogType) {
DoricLogTypeDebug = 0,
DoricLogTypeWarning = 1,
DoricLogTypeError = 2,
};
@protocol DoricMonitorProtocol <NSObject>
- (void)onException:(NSException *)exception;
- (void)onLog:(DoricLogType)type message:(NSString *)message;
@end

View File

@ -21,11 +21,12 @@
// //
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "DoricMonitorProtocol.h"
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@class DoricLibrary; @class DoricLibrary;
@interface DoricRegistry : NSObject @interface DoricRegistry : NSObject <DoricMonitorProtocol>
- (NSString *)acquireJSBundle:(NSString *)name; - (NSString *)acquireJSBundle:(NSString *)name;
@ -44,6 +45,8 @@ NS_ASSUME_NONNULL_BEGIN
- (NSDictionary *)environmentVariables; - (NSDictionary *)environmentVariables;
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor;
+ (void)register:(DoricLibrary *)library; + (void)register:(DoricLibrary *)library;
@end @end

View File

@ -47,6 +47,7 @@
#import "DoricDraggableNode.h" #import "DoricDraggableNode.h"
#import "DoricLibrary.h" #import "DoricLibrary.h"
#import "DoricNotificationPlugin.h" #import "DoricNotificationPlugin.h"
#import "DoricUtil.h"
@interface DoricLibraries : NSObject @interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries; @property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@ -73,12 +74,26 @@ + (instancetype)instance {
@end @end
@interface DoricDefaultMonitor : NSObject <DoricMonitorProtocol>
@end
@implementation DoricDefaultMonitor
- (void)onException:(NSException *)exception {
DoricLog(@"DefaultMonitor - onException - %@", exception.reason);
}
- (void)onLog:(DoricLogType)type message:(NSString *)message {
DoricLog(message);
}
@end
@interface DoricRegistry () @interface DoricRegistry ()
@property(nonatomic, strong) NSMutableDictionary *bundles; @property(nonatomic, strong) NSMutableDictionary *bundles;
@property(nonatomic, strong) NSMutableDictionary *plugins; @property(nonatomic, strong) NSMutableDictionary *plugins;
@property(nonatomic, strong) NSMutableDictionary *nodes; @property(nonatomic, strong) NSMutableDictionary *nodes;
@property(nonatomic, strong) NSMutableDictionary <NSString *, id> *envVariables; @property(nonatomic, strong) NSMutableDictionary <NSString *, id> *envVariables;
@property(nonatomic, strong) NSMutableSet <id <DoricMonitorProtocol>> *monitors;
@end @end
@implementation DoricRegistry @implementation DoricRegistry
@ -97,6 +112,8 @@ - (instancetype)init {
[DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) { [DoricLibraries.instance.libraries enumerateObjectsUsingBlock:^(DoricLibrary *obj, BOOL *stop) {
[obj load:self]; [obj load:self];
}]; }];
_monitors = [NSMutableSet new];
[self registerMonitor:[DoricDefaultMonitor new]];
} }
return self; return self;
} }
@ -161,4 +178,20 @@ - (void)setEnvironment:(NSString *)key variable:(id)value {
- (NSDictionary *)environmentVariables { - (NSDictionary *)environmentVariables {
return self.envVariables; return self.envVariables;
} }
- (void)registerMonitor:(id <DoricMonitorProtocol>)monitor {
[self.monitors addObject:monitor];
}
- (void)onException:(NSException *)exception {
for (id <DoricMonitorProtocol> monitor in self.monitors) {
[monitor onException:exception];
}
}
- (void)onLog:(DoricLogType)type message:(NSString *)message {
for (id <DoricMonitorProtocol> monitor in self.monitors) {
[monitor onLog:type message:message];
}
}
@end @end

View File

@ -70,7 +70,13 @@ - (void)initJSExecutor {
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[envDic copy]]; [self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[envDic copy]];
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString *type, NSString *message) { [self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString *type, NSString *message) {
DoricLog(@"JS:%@", message); if ([type isEqualToString:@"e"]) {
[self.registry onLog:DoricLogTypeError message:message];
} else if ([type isEqualToString:@"w"]) {
[self.registry onLog:DoricLogTypeWarning message:message];
} else {
[self.registry onLog:DoricLogTypeDebug message:message];
}
}]; }];
[self.jsExecutor injectGlobalJSObject:INJECT_EMPTY obj:^() { [self.jsExecutor injectGlobalJSObject:INJECT_EMPTY obj:^() {
@ -80,14 +86,15 @@ - (void)initJSExecutor {
if (!self) return NO; if (!self) return NO;
NSString *content = [self.registry acquireJSBundle:name]; NSString *content = [self.registry acquireJSBundle:name];
if (!content) { if (!content) {
DoricLog(@"require js bundle:%@ is empty", name); [self.registry onLog:DoricLogTypeError message:[NSString stringWithFormat:@"require js bundle:%@ is empty", name]];
return NO; return NO;
} }
@try { @try {
[self.jsExecutor loadJSScript:[self packageModuleScript:name content:content] [self.jsExecutor loadJSScript:[self packageModuleScript:name content:content]
source:[@"Module://" stringByAppendingString:name]]; source:[@"Module://" stringByAppendingString:name]];
} @catch (NSException *e) { } @catch (NSException *e) {
DoricLog(@"require js bundle:%@ error,for %@", name, e.reason); [self.registry onLog:DoricLogTypeError
message:[NSString stringWithFormat:@"require js bundle:%@ error,for %@", name, e.reason]];
} }
return YES; return YES;
}]; }];
@ -119,11 +126,16 @@ - (void)initJSExecutor {
} }
- (void)initDoricEnvironment { - (void)initDoricEnvironment {
[self loadBuiltinJS:DORIC_BUNDLE_SANDBOX]; @try {
NSString *path = [DoricBundle() pathForResource:DORIC_BUNDLE_LIB ofType:@"js"];
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; [self loadBuiltinJS:DORIC_BUNDLE_SANDBOX];
[self.jsExecutor loadJSScript:[self packageModuleScript:DORIC_MODULE_LIB content:jsContent] NSString *path = [DoricBundle() pathForResource:DORIC_BUNDLE_LIB ofType:@"js"];
source:[@"Module://" stringByAppendingString:DORIC_MODULE_LIB]]; NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.jsExecutor loadJSScript:[self packageModuleScript:DORIC_MODULE_LIB content:jsContent]
source:[@"Module://" stringByAppendingString:DORIC_MODULE_LIB]];
} @catch (NSException *exception) {
[self.registry onException:exception];
}
} }
- (void)loadBuiltinJS:(NSString *)fileName { - (void)loadBuiltinJS:(NSString *)fileName {
@ -184,7 +196,9 @@ - (void)callbackTimer:(NSTimer *)timer {
@try { @try {
[self invokeDoricMethod:DORIC_TIMER_CALLBACK, timerId, nil]; [self invokeDoricMethod:DORIC_TIMER_CALLBACK, timerId, nil];
} @catch (NSException *exception) { } @catch (NSException *exception) {
DoricLog(@"Timer Callback error:%@", exception.reason); [self.registry onException:exception];
[self.registry onLog:DoricLogTypeError
message:[NSString stringWithFormat:@"Timer Callback error:%@", exception.reason]];
} }
if (![repeat boolValue]) { if (![repeat boolValue]) {
[self.timers removeObjectForKey:[timerId stringValue]]; [self.timers removeObjectForKey:[timerId stringValue]];

View File

@ -87,6 +87,7 @@ - (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context met
[invocation invoke]; [invocation invoke];
} @catch (NSException *exception) { } @catch (NSException *exception) {
DoricLog(@"CallNative Error:%@", exception.reason); DoricLog(@"CallNative Error:%@", exception.reason);
[context.driver.registry onException:exception];
} }
}; };
@ -102,6 +103,8 @@ - (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context met
ret = [JSValue valueWithObject:[returnValue copy] inContext:[JSContext currentContext]]; ret = [JSValue valueWithObject:[returnValue copy] inContext:[JSContext currentContext]];
} else { } else {
DoricLog(@"CallNative Error:%@", @"Must return object type"); DoricLog(@"CallNative Error:%@", @"Must return object type");
[context.driver.registry onLog:DoricLogTypeError
message:[NSString stringWithFormat:@"CallNative Error:%@", @"Must return object type"]];
ret = nil; ret = nil;
} }
} }

View File

@ -108,6 +108,7 @@ - (id)findClass:(Class)clz target:(id)target method:(NSString *)name promise:(Do
[invocation invoke]; [invocation invoke];
} @catch (NSException *exception) { } @catch (NSException *exception) {
DoricLog(@"CallNative Error:%@", exception.reason); DoricLog(@"CallNative Error:%@", exception.reason);
[self.doricContext.driver.registry onException:exception];
} }
}; };
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{