diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDebugJSEngine.java b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDebugJSEngine.java index dde27193..da60b685 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDebugJSEngine.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/DoricDebugJSEngine.java @@ -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); + } } diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/IDevKit.java b/doric-android/devkit/src/main/java/pub/doric/devkit/IDevKit.java index 3d1f422a..c02ac42a 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/IDevKit.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/IDevKit.java @@ -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);