android: devkit log

This commit is contained in:
王劲鹏 2020-03-18 14:14:58 +08:00 committed by osborn
parent 281d3d7425
commit 8a5443b46a
2 changed files with 49 additions and 1 deletions

View File

@ -1,7 +1,18 @@
package pub.doric.devkit;
import android.util.Log;
import androidx.annotation.Nullable;
import com.google.gson.JsonObject;
import java.io.PrintWriter;
import java.io.StringWriter;
import pub.doric.DoricContext;
import pub.doric.devkit.remote.DoricRemoteJSExecutor;
import pub.doric.engine.DoricJSEngine;
import pub.doric.utils.DoricLog;
public class DoricDebugJSEngine extends DoricJSEngine {
@ -16,4 +27,41 @@ public class DoricDebugJSEngine extends DoricJSEngine {
protected void initJSEngine() {
mDoricJSE = new DoricRemoteJSExecutor(statusCallback);
}
@Override
public void onException(@Nullable DoricContext context, Exception e) {
Log.e(DoricJSEngine.class.getSimpleName(), "In source file: " + (context != null ? context.getSource() : "Unknown"));
e.printStackTrace();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("source", "In source file: " + (context != null ? context.getSource() : "Unknown"));
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
jsonObject.addProperty("exception", stringWriter.toString());
DevKit.getInstance().sendDevCommand(IDevKit.Command.EXCEPTION, jsonObject);
}
@Override
public void onLog(int type, String message) {
String typeString = "DEFAULT";
switch (type) {
case Log.ERROR:
DoricLog.suffix_e("_js", message);
typeString = "ERROR";
break;
case Log.WARN:
DoricLog.suffix_w("_js", message);
typeString = "WARN";
break;
default:
DoricLog.suffix_d("_js", message);
break;
}
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("type", typeString);
jsonObject.addProperty("message", message);
DevKit.getInstance().sendDevCommand(IDevKit.Command.LOG, jsonObject);
}
}

View File

@ -5,7 +5,7 @@ import com.google.gson.JsonObject;
public interface IDevKit {
enum Command {
DEBUG, HOT_RELOAD
DEBUG, HOT_RELOAD, EXCEPTION, LOG
}
void connectDevKit(String url);