android:add monitor

This commit is contained in:
pengfei.zhou 2020-01-10 16:41:59 +08:00 committed by osborn
parent 048b8414d8
commit da6afad30d
6 changed files with 141 additions and 29 deletions

View File

@ -17,6 +17,7 @@ package pub.doric;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.util.Log;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
@ -76,7 +77,8 @@ public class DoricNativeDriver implements IDoricDriver {
try { try {
return doricJSEngine.invokeDoricMethod(method, args); return doricJSEngine.invokeDoricMethod(method, args);
} catch (Exception e) { } catch (Exception e) {
DoricLog.e("invokeDoricMethod(%s,...),error is %s", method, e.getLocalizedMessage()); doricJSEngine.onException(e);
doricJSEngine.onLogout(Log.ERROR, String.format("invokeDoricMethod(%s,...),error is %s", method, e.getLocalizedMessage()));
return new JSDecoder(null); return new JSDecoder(null);
} }
} }
@ -105,7 +107,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) {
DoricLog.e("createContext %s error is %s", source, e.getLocalizedMessage()); doricJSEngine.onException(e);
doricJSEngine.onLogout(Log.ERROR, String.format("createContext %s error is %s", source, e.getLocalizedMessage()));
return false; return false;
} }
} }
@ -121,7 +124,8 @@ public class DoricNativeDriver implements IDoricDriver {
doricJSEngine.destroyContext(contextId); doricJSEngine.destroyContext(contextId);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
DoricLog.e("destroyContext %s error is %s", contextId, e.getLocalizedMessage()); doricJSEngine.onException(e);
doricJSEngine.onLogout(Log.ERROR, String.format("destroyContext %s error is %s", contextId, e.getLocalizedMessage()));
return false; return false;
} }
} }

View File

@ -66,6 +66,8 @@ public class DoricRegistry {
private Map<String, DoricMetaInfo<DoricJavaPlugin>> pluginInfoMap = new HashMap<>(); private Map<String, DoricMetaInfo<DoricJavaPlugin>> pluginInfoMap = new HashMap<>();
private Map<String, DoricMetaInfo<ViewNode>> nodeInfoMap = new HashMap<>(); private Map<String, DoricMetaInfo<ViewNode>> nodeInfoMap = new HashMap<>();
private Set<IDoricMonitor> monitors = new HashSet<>();
private static void initRegistry(DoricRegistry doricRegistry) { private static void initRegistry(DoricRegistry doricRegistry) {
for (DoricLibrary library : doricLibraries) { for (DoricLibrary library : doricLibraries) {
library.load(doricRegistry); library.load(doricRegistry);
@ -146,4 +148,20 @@ public class DoricRegistry {
public Map<String, Object> getEnvironmentVariables() { public Map<String, Object> getEnvironmentVariables() {
return extendedEnvValues; return extendedEnvValues;
} }
public void registerMonitor(IDoricMonitor monitor) {
this.monitors.add(monitor);
}
public void onException(Exception e) {
for (IDoricMonitor monitor : this.monitors) {
monitor.onException(e);
}
}
public void onLogout(int type, String message) {
for (IDoricMonitor monitor : this.monitors) {
monitor.onLogout(type, message);
}
}
} }

View File

@ -0,0 +1,40 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric;
/**
* @Description: Monitor status of doric engine
* @Author: pengfei.zhou
* @CreateDate: 2020-01-10
*/
public interface IDoricMonitor {
/**
* Called when native or js exception occurred in doric
*
* @param e exception which is thrown within doric sdk
* @see com.github.pengfeizhou.jscore.JSRuntimeException
*/
void onException(Exception e);
/**
* @param type The priority/type of this log message.
* @param message The message you would like logged.
* @see android.util.Log#ERROR
* @see android.util.Log#WARN
* @see android.util.Log#DEBUG
*/
void onLogout(int type, String message);
}

View File

@ -23,6 +23,7 @@ import android.os.HandlerThread;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSONBuilder;
@ -34,6 +35,7 @@ import java.util.Map;
import pub.doric.Doric; import pub.doric.Doric;
import pub.doric.DoricRegistry; import pub.doric.DoricRegistry;
import pub.doric.IDoricMonitor;
import pub.doric.extension.bridge.DoricBridgeExtension; import pub.doric.extension.bridge.DoricBridgeExtension;
import pub.doric.extension.timer.DoricTimerExtension; import pub.doric.extension.timer.DoricTimerExtension;
import pub.doric.utils.DoricConstant; import pub.doric.utils.DoricConstant;
@ -45,7 +47,7 @@ import pub.doric.utils.DoricUtils;
* @Author: pengfei.zhou * @Author: pengfei.zhou
* @CreateDate: 2019-07-18 * @CreateDate: 2019-07-18
*/ */
public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback { public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback, IDoricMonitor {
private HandlerThread handlerThread; private HandlerThread handlerThread;
private final Handler mJSHandler; private final Handler mJSHandler;
@ -68,6 +70,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
} }
}); });
mTimerExtension = new DoricTimerExtension(looper, this); mTimerExtension = new DoricTimerExtension(looper, this);
mDoricRegistry.registerMonitor(this);
} }
public Handler getJSHandler() { public Handler getJSHandler() {
@ -116,17 +119,17 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
String message = args[1].string(); String message = args[1].string();
switch (type) { switch (type) {
case "w": case "w":
DoricLog.suffix_w("_js", message); mDoricRegistry.onLogout(Log.WARN, message);
break; break;
case "e": case "e":
DoricLog.suffix_e("_js", message); mDoricRegistry.onLogout(Log.ERROR, message);
break; break;
default: default:
DoricLog.suffix_d("_js", message); mDoricRegistry.onLogout(Log.INFO, message);
break; break;
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); mDoricRegistry.onException(e);
} }
return null; return null;
} }
@ -144,13 +147,13 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
String name = args[0].string(); String name = args[0].string();
String content = mDoricRegistry.acquireJSBundle(name); String content = mDoricRegistry.acquireJSBundle(name);
if (TextUtils.isEmpty(content)) { if (TextUtils.isEmpty(content)) {
DoricLog.e("require js bundle:%s is empty", name); mDoricRegistry.onLogout(Log.ERROR, String.format("require js bundle:%s is empty", name));
return new JavaValue(false); return new JavaValue(false);
} }
mDoricJSE.loadJS(packageModuleScript(name, content), "Module://" + name); mDoricJSE.loadJS(packageModuleScript(name, content), "Module://" + name);
return new JavaValue(true); return new JavaValue(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); mDoricRegistry.onException(e);
return new JavaValue(false); return new JavaValue(false);
} }
} }
@ -164,7 +167,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
args[1].number().longValue(), args[1].number().longValue(),
args[2].bool()); args[2].bool());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); mDoricRegistry.onException(e);
} }
return null; return null;
} }
@ -175,7 +178,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
try { try {
mTimerExtension.clearTimer(args[0].number().longValue()); mTimerExtension.clearTimer(args[0].number().longValue());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); mDoricRegistry.onException(e);
} }
return null; return null;
} }
@ -191,7 +194,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
JSDecoder jsDecoder = args[4]; JSDecoder jsDecoder = args[4];
return mDoricBridgeExtension.callNative(contextId, module, method, callbackId, jsDecoder); return mDoricBridgeExtension.callNative(contextId, module, method, callbackId, jsDecoder);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); mDoricRegistry.onException(e);
} }
return null; return null;
} }
@ -199,10 +202,14 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
} }
private void initDoricRuntime() { private void initDoricRuntime() {
try {
loadBuiltinJS(DoricConstant.DORIC_BUNDLE_SANDBOX); loadBuiltinJS(DoricConstant.DORIC_BUNDLE_SANDBOX);
String libName = DoricConstant.DORIC_MODULE_LIB; String libName = DoricConstant.DORIC_MODULE_LIB;
String libJS = DoricUtils.readAssetFile(DoricConstant.DORIC_BUNDLE_LIB); String libJS = DoricUtils.readAssetFile(DoricConstant.DORIC_BUNDLE_LIB);
mDoricJSE.loadJS(packageModuleScript(libName, libJS), "Module://" + libName); mDoricJSE.loadJS(packageModuleScript(libName, libJS), "Module://" + libName);
} catch (Exception e) {
mDoricRegistry.onException(e);
}
} }
@Override @Override
@ -252,12 +259,34 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
try { try {
invokeDoricMethod(DoricConstant.DORIC_TIMER_CALLBACK, timerId); invokeDoricMethod(DoricConstant.DORIC_TIMER_CALLBACK, timerId);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); mDoricRegistry.onException(e);
DoricLog.e("Timer Callback error:%s", e.getLocalizedMessage()); mDoricRegistry.onLogout(
Log.ERROR,
String.format("Timer Callback error:%s", e.getLocalizedMessage()));
} }
} }
public DoricRegistry getRegistry() { public DoricRegistry getRegistry() {
return mDoricRegistry; return mDoricRegistry;
} }
@Override
public void onException(Exception e) {
e.printStackTrace();
}
@Override
public void onLogout(int type, String message) {
switch (type) {
case Log.ERROR:
DoricLog.suffix_e("_js", message);
break;
case Log.WARN:
DoricLog.suffix_w("_js", message);
break;
default:
DoricLog.suffix_d("_js", message);
break;
}
}
} }

View File

@ -15,6 +15,8 @@
*/ */
package pub.doric.extension.bridge; package pub.doric.extension.bridge;
import android.util.Log;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
@ -44,22 +46,30 @@ public class DoricBridgeExtension {
final DoricContext context = DoricContextManager.getContext(contextId); final DoricContext context = DoricContextManager.getContext(contextId);
DoricMetaInfo<DoricJavaPlugin> pluginInfo = context.getDriver().getRegistry().acquirePluginInfo(module); DoricMetaInfo<DoricJavaPlugin> pluginInfo = context.getDriver().getRegistry().acquirePluginInfo(module);
if (pluginInfo == null) { if (pluginInfo == null) {
DoricLog.e("Cannot find plugin class:%s", module); context.getDriver().getRegistry().onLogout(
Log.ERROR,
String.format("Cannot find plugin class:%s", module));
return new JavaValue(false); return new JavaValue(false);
} }
final DoricJavaPlugin doricJavaPlugin = context.obtainPlugin(pluginInfo); final DoricJavaPlugin doricJavaPlugin = context.obtainPlugin(pluginInfo);
if (doricJavaPlugin == null) { if (doricJavaPlugin == null) {
DoricLog.e("Cannot obtain plugin instance:%s,method:%", module); context.getDriver().getRegistry().onLogout(
Log.ERROR,
String.format("Cannot obtain plugin instance:%s,method:%s", module, methodName));
return new JavaValue(false); return new JavaValue(false);
} }
final Method method = pluginInfo.getMethod(methodName); final Method method = pluginInfo.getMethod(methodName);
if (method == null) { if (method == null) {
DoricLog.e("Cannot find plugin method in class:%s,method:%s", module, methodName); context.getDriver().getRegistry().onLogout(
Log.ERROR,
String.format("Cannot find plugin method in class:%s,method:%s", module, methodName));
return new JavaValue(false); return new JavaValue(false);
} }
DoricMethod doricMethod = method.getAnnotation(DoricMethod.class); DoricMethod doricMethod = method.getAnnotation(DoricMethod.class);
if (doricMethod == null) { if (doricMethod == null) {
DoricLog.e("Cannot find DoricMethod annotation in class:%s,method:%s", module, methodName); context.getDriver().getRegistry().onLogout(
Log.ERROR,
String.format("Cannot find DoricMethod annotation in class:%s,method:%s", module, methodName));
return new JavaValue(false); return new JavaValue(false);
} }
Callable<JavaValue> callable = new Callable<JavaValue>() { Callable<JavaValue> callable = new Callable<JavaValue>() {
@ -93,7 +103,10 @@ public class DoricBridgeExtension {
try { try {
return DoricUtils.toJavaObject(clz, jsDecoder); return DoricUtils.toJavaObject(clz, jsDecoder);
} catch (Exception e) { } catch (Exception e) {
DoricLog.e("createParam error:%s", e.getLocalizedMessage()); context.getDriver().getRegistry().onException(e);
context.getDriver().getRegistry().onLogout(
Log.ERROR,
String.format("createParam error:%s", e.getLocalizedMessage()));
} }
return null; return null;
} }

View File

@ -16,6 +16,7 @@
package pub.doric.plugin; package pub.doric.plugin;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
@ -78,8 +79,12 @@ public class ShaderPlugin extends DoricJavaPlugin {
@Override @Override
public void onError(Throwable t) { public void onError(Throwable t) {
t.printStackTrace(); if (t instanceof Exception) {
DoricLog.e("Shader.render:error%s", t.getLocalizedMessage()); getDoricContext().getDriver().getRegistry().onException((Exception) t);
}
getDoricContext().getDriver().getRegistry().onLogout(
Log.ERROR,
String.format("Shader.render:error%s", t.getLocalizedMessage()));
} }
@Override @Override
@ -88,8 +93,11 @@ public class ShaderPlugin extends DoricJavaPlugin {
} }
}); });
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); getDoricContext().getDriver().getRegistry().onException(e);
DoricLog.e("Shader.render:error%s", e.getLocalizedMessage()); getDoricContext().getDriver().getRegistry().onLogout(
Log.ERROR,
String.format("Shader.render:error%s", e.getLocalizedMessage())
);
} }
} }
@ -166,7 +174,7 @@ public class ShaderPlugin extends DoricJavaPlugin {
} }
} }
} catch (ArchiveException e) { } catch (ArchiveException e) {
e.printStackTrace(); getDoricContext().getDriver().getRegistry().onException(e);
} }
return new JavaValue(true); return new JavaValue(true);
} }